[Bug]: openai.BadRequestError: String does not match pattern.

### Describe the bug

A GroupChat with agents using OpenAI always throws the following error after 3-7 messages:
```
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\_base_client.py", line 1064, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid 'messages[6].name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9_-]+$'.", 'type': 'invalid_request_error', 'param': 'messages[6].name', 'code': 'invalid_value'}}
```

### Steps to reproduce

```
import autogen
import os

# LLM Config
gpt4_config = {
    "cache_seed": 42,  # change the cache_seed for different trials
    "temperature": 0.1,
    "config_list": [{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"], "api_type": "openai"}],
    "timeout": 120,
}

# Agents
user_proxy = autogen.UserProxyAgent(
    name="Admin",
    system_message="A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin.",
    code_execution_config=False,
)
engineer = autogen.AssistantAgent(
    name="Software Engineer",
    llm_config=gpt4_config,
    system_message="""Software Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor.
Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the executor.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
""",
)
scientist = autogen.AssistantAgent(
    name="Business Analyst",
    llm_config=gpt4_config,
    system_message="""Business Analyst. You follow an approved plan. You are experienced in analyzing companies and stocks. You don't write code.""",
)
planner = autogen.AssistantAgent(
    name="Planner",
    system_message="""Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval.
The plan may involve an software engineer who can write code and a business analyst who doesn't write code.
Explain the plan first. Be clear which step is performed by an software engineer, and which step is performed by a Business Analyst.
""",
    llm_config=gpt4_config,
)
executor = autogen.UserProxyAgent(
    name="Executor",
    system_message="Executor. Execute the code written by the engineer and report the result.",
    human_input_mode="NEVER",
    code_execution_config={
        "last_n_messages": 3,
        "work_dir": "work_dir",
        "use_docker": True,
    },
)
critic = autogen.AssistantAgent(
    name="Critic",
    system_message="Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.",
    llm_config=gpt4_config,
)
groupchat = autogen.GroupChat(
    agents=[user_proxy, engineer, scientist, planner, executor, critic], messages=[], max_round=50
)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_config)

# Start Chat
user_proxy.initiate_chat(
    manager,
    message="""
Create a sophisticated stock scoring system. Test it on the top 20 stocks.
"""
)
```

### Model Used

gpt-4o-mini

### Expected Behavior

No error

### Screenshots and logs

```
PS C:\Code\AutoGen> & C:/Code/AutoGen/.venv/Scripts/python.exe c:/Code/AutoGen/StockRating/main.py
Admin (to chat_manager):


Create a sophisticated stock scoring system. Test it on the top 20 stocks.


--------------------------------------------------------------------------------

Next speaker: Planner

Planner (to chat_manager):

### Initial Plan for Creating a Sophisticated Stock Scoring System

**Objective:** Develop a stock scoring system that evaluates and ranks the top 20 stocks based on various financial metrics and indicators.

#### Step 1: Define Scoring Criteria
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will research and define the key financial metrics and indicators that will be used to score the stocks. This may include metrics such as Price-to-Earnings (P/E) ratio, Earnings Per Share (EPS), Return on Equity (ROE), and other relevant financial ratios.

#### Step 2: Data Collection
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will write code to collect historical and current financial data for the top 20 stocks from reliable financial APIs or databases. This data will be used for scoring.

#### Step 3: Develop Scoring Algorithm
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will develop an algorithm that applies the scoring criteria defined in Step 1 to the collected data. The algorithm will calculate a score for each stock based on the defined metrics.

#### Step 4: Testing the Scoring System
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will test the scoring system by applying it to the top 20 stocks and analyzing the results. They will ensure that the scoring system is functioning as intended and that the scores make sense in the context of the market.

#### Step 5: Review and Adjust Scoring Criteria
- **Responsibility:** Business Analyst
- **Description:** Based on the testing results, the Business Analyst will review the scoring criteria and make adjustments if necessary. This may involve adding new metrics or changing the weight of existing ones.

#### Step 6: Final Implementation
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will implement any changes made to the scoring algorithm based on the feedback from the Business Analyst. They will ensure that the system is robust and can handle real-time data updates.

#### Step 7: Documentation and Reporting
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will document the scoring system, including the criteria used, the scoring algorithm, and the results of the testing. They will prepare a report summarizing the findings and recommendations for stakeholders.

### Summary
- **Business Analyst:** Steps 1, 4, 5, and 7
- **Software Engineer:** Steps 2, 3, and 6

This plan outlines a collaborative approach where the Business Analyst focuses on defining and testing the scoring criteria, while the Software Engineer handles the technical implementation and data processing.

### Next Steps
Please provide feedback on this initial plan, and I will revise it accordingly.

--------------------------------------------------------------------------------

Next speaker: Critic

Critic (to chat_manager):

### Feedback on the Initial Plan for Creating a Sophisticated Stock Scoring System

Overall, the plan is well-structured and outlines clear responsibilities for both the Business Analyst and Software Engineer. However, there are several areas where improvements can be made to enhance the robustness and verifiability of the scoring system. Here are my suggestions:

#### 1. **Define Scoring Criteria**
- **Feedback:** While you mention key financial metrics, it would be beneficial to specify the exact metrics to be used and their respective weightings in the scoring system. Consider including qualitative factors such as market sentiment or news impact.
- **Verifiable Info:** Include sources for the metrics chosen (e.g., academic papers, financial analysis reports) to justify their relevance.

#### 2. **Data Collection**
- **Feedback:** Specify which financial APIs or databases will be used for data collection. Ensure that these sources are reliable and provide up-to-date information.
- **Verifiable Info:** List the URLs of the APIs or databases to be used (e.g., Yahoo Finance API, Alpha Vantage, etc.) to ensure transparency and reproducibility.

#### 3. **Develop Scoring Algorithm**
- **Feedback:** Provide a brief outline of the algorithm's logic. Will it be a weighted scoring system, a ranking system, or a combination of both? Clarifying this will help in understanding how scores are derived.
- **Verifiable Info:** Reference any existing scoring models or frameworks that will inform the algorithm's development.

#### 4. **Testing the Scoring System**
- **Feedback:** Define the criteria for success in the testing phase. What benchmarks will be used to evaluate the effectiveness of the scoring system? Consider backtesting against historical data to validate the scoring system.
- **Verifiable Info:** Document the testing methodology and any historical data sources used for backtesting.

#### 5. **Review and Adjust Scoring Criteria**
- **Feedback:** Establish a timeline for the review process and criteria for making adjustments. This will help in maintaining a systematic approach to refining the scoring system.
- **Verifiable Info:** Consider including a feedback loop from stakeholders or users of the scoring system to gather insights for adjustments.

#### 6. **Final Implementation**
- **Feedback:** Ensure that the implementation phase includes considerations for scalability and performance, especially if real-time data updates are involved.
- **Verifiable Info:** Document the technology stack and architecture that will be used for implementation.

#### 7. **Documentation and Reporting**
- **Feedback:** In addition to documenting the scoring system, consider creating user guides or tutorials for stakeholders who will use the system. This will enhance usability and understanding.
- **Verifiable Info:** Include a section in the documentation that cites all sources used in the development of the scoring system.

### Summary of Recommendations
- Specify metrics and their weightings in the scoring criteria.
- Include URLs for data sources and APIs.
- Outline the scoring algorithm's logic.
- Define success criteria for testing and backtesting methodology.
- Establish a timeline for reviewing and adjusting criteria.
- Document the technology stack for implementation.
- Create user guides for better usability.

### Next Steps
Please consider these suggestions and revise the plan accordingly. Once the plan incorporates these elements, it will be more robust and verifiable, ensuring a higher likelihood of success in developing the stock scoring system.

--------------------------------------------------------------------------------

Next speaker: Planner

Planner (to chat_manager):

:Planner:

### Revised Plan for Creating a Sophisticated Stock Scoring System

**Objective:** Develop a stock scoring system that evaluates and ranks the top 20 stocks based on various financial metrics and indicators.

#### Step 1: Define Scoring Criteria
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will research and define the key financial metrics and indicators that will be used to score the stocks. The following metrics will be included:
  - Price-to-Earnings (P/E) Ratio (Weight: 25%)
  - Earnings Per Share (EPS) Growth (Weight: 25%)
  - Return on Equity (ROE) (Weight: 20%)
  - Debt-to-Equity Ratio (Weight: 15%)
  - Market Sentiment Score (Weight: 15%) - derived from news sentiment analysis.
- **Verifiable Info:** Sources for metrics will include academic papers and financial analysis reports, such as those from Investopedia and financial journals.

#### Step 2: Data Collection
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will write code to collect historical and current financial data for the top 20 stocks from reliable financial APIs. The following sources will be used:
  - Yahoo Finance API (https://www.yahoofinanceapi.com/)
  - Alpha Vantage (https://www.alphavantage.co/)
  - Finnhub (https://finnhub.io/)
- **Verifiable Info:** The URLs of the APIs will be documented for transparency.

#### Step 3: Develop Scoring Algorithm
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will develop a weighted scoring algorithm that combines the defined metrics. The algorithm will calculate a composite score for each stock based on the weighted contributions of each metric.
- **Verifiable Info:** The algorithm will be informed by existing models such as the Z-score model for financial health assessment.

#### Step 4: Testing the Scoring System
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will test the scoring system by applying it to the top 20 stocks and analyzing the results. Success criteria will include:
  - The scoring system should rank stocks in alignment with market performance.
  - Backtesting against historical data to validate the scoring system's effectiveness.
- **Verifiable Info:** The testing methodology will be documented, including historical data sources used for backtesting.

#### Step 5: Review and Adjust Scoring Criteria
- **Responsibility:** Business Analyst
- **Description:** Based on the testing results, the Business Analyst will review the scoring criteria and make adjustments if necessary. A timeline of 2 weeks will be established for this review process, with feedback gathered from stakeholders.
- **Verifiable Info:** A feedback loop will be created to gather insights from users of the scoring system.

#### Step 6: Final Implementation
- **Responsibility:** Software Engineer
- **Description:** The Software Engineer will implement any changes made to the scoring algorithm based on the feedback from the Business Analyst. Considerations for scalability and performance will be included, especially for real-time data updates.
- **Verifiable Info:** The technology stack (e.g., Python, Flask for API integration, PostgreSQL for database) will be documented.

#### Step 7: Documentation and Reporting
- **Responsibility:** Business Analyst
- **Description:** The Business Analyst will document the scoring system, including the criteria used, the scoring algorithm, and the results of the testing. User guides and tutorials will be created for stakeholders.
- **Verifiable Info:** All sources used in the development of the scoring system will be cited in the documentation.

### Summary of Responsibilities
- **Business Analyst:** Steps 1, 4, 5, and 7
- **Software Engineer:** Steps 2, 3, and 6

### Next Steps
Please review this revised plan, which incorporates the feedback provided. Once approved, we can proceed with the implementation of the stock scoring system. 

--------------------------------------------------------------------------------

Next speaker: Admin

Replying as Admin. Provide feedback to chat_manager. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: Sounds good, start with t
he first step, then come back at me.
Admin (to chat_manager):

Sounds good, start with the first step, then come back at me.

--------------------------------------------------------------------------------

Next speaker: Business Analyst

Business Analyst (to chat_manager):

### Step 1: Define Scoring Criteria

**Objective:** Research and define the key financial metrics and indicators that will be used to score the stocks.

#### Selected Metrics and Weightings:
1. **Price-to-Earnings (P/E) Ratio**
   - **Weight:** 25%
   - **Description:** Measures a company's current share price relative to its earnings per share. A lower P/E may indicate that the stock is undervalued.    

2. **Earnings Per Share (EPS) Growth**
   - **Weight:** 25%
   - **Description:** Indicates the company's profitability on a per-share basis. Growth in EPS is a positive sign of a company's financial health.

3. **Return on Equity (ROE)**
   - **Weight:** 20%
   - **Description:** Measures a company's ability to generate profits from its shareholders' equity. A higher ROE indicates efficient management and strong financial performance.

4. **Debt-to-Equity Ratio**
   - **Weight:** 15%
   - **Description:** Compares a company's total liabilities to its shareholder equity. A lower ratio suggests a more financially stable company with less risk.

5. **Market Sentiment Score**
   - **Weight:** 15%
   - **Description:** Derived from news sentiment analysis, this score reflects the overall market perception of the stock. Positive sentiment can drive stock prices higher.

#### Sources for Metrics:
- **P/E Ratio and EPS Growth:** Investopedia, financial analysis reports.
- **ROE and Debt-to-Equity Ratio:** Financial journals and company financial statements.
- **Market Sentiment Score:** News sentiment analysis tools and platforms.

### Next Steps:
- I will compile the necessary data for the selected metrics for the top 20 stocks.
- Once the data is collected, I will proceed to Step 2: Data Collection.

Please let me know if you have any additional input or if you would like to proceed with this plan.

--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Code\AutoGen\StockRating\main.py", line 60, in <module>
    user_proxy.initiate_chat(
    ~~~~~~~~~~~~~~~~~~~~~~~~^
        manager,
        ^^^^^^^^
    ...<2 lines>...
    """
    ^^^
    )
    ^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 1117, in initiate_chat
    self.send(msg2send, recipient, silent=silent)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 807, in send
    recipient.receive(message, self, request_reply, silent)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 917, in receive
    reply = self.generate_reply(messages=self.chat_messages[sender], sender=sender)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 2065, in generate_reply
    final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
                   ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\groupchat.py", line 1179, in run_chat
    speaker = groupchat.select_speaker(speaker, self)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\groupchat.py", line 572, in select_speaker
    return self._auto_select_speaker(last_speaker, selector, messages, agents)
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\groupchat.py", line 750, in _auto_select_speaker
    result = checking_agent.initiate_chat(
        speaker_selection_agent,
    ...<5 lines>...
        silent=not self.select_speaker_auto_verbose,  # Base silence on the verbose attribute
    )
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 1110, in initiate_chat
    self.send(msg2send, recipient, request_reply=True, silent=silent)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 807, in send
    recipient.receive(message, self, request_reply, silent)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 917, in receive
    reply = self.generate_reply(messages=self.chat_messages[sender], sender=sender)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 2065, in generate_reply
    final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
                   ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 1436, in generate_oai_reply
    extracted_response = self._generate_oai_reply_from_client(
        client, self._oai_system_message + messages, self.client_cache
    )
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\agentchat\conversable_agent.py", line 1455, in _generate_oai_reply_from_client
    response = llm_client.create(
        context=messages[-1].pop("context", None),
    ...<2 lines>...
        agent=self,
    )
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\oai\client.py", line 873, in create
    response = client.create(params)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\autogen\oai\client.py", line 418, in create
    response = create_or_parse(**params)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\_utils\_utils.py", line 279, in wrapper
    return func(*args, **kwargs)
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\resources\chat\completions.py", line 859, in create
    return self._post(
           ~~~~~~~~~~^
        "/chat/completions",
        ^^^^^^^^^^^^^^^^^^^^
    ...<40 lines>...
        stream_cls=Stream[ChatCompletionChunk],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\_base_client.py", line 1283, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\_base_client.py", line 960, in request
    return self._request(
           ~~~~~~~~~~~~~^
        cast_to=cast_to,
        ^^^^^^^^^^^^^^^^
    ...<3 lines>...
        retries_taken=retries_taken,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Code\AutoGen\.venv\Lib\site-packages\openai\_base_client.py", line 1064, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid 'messages[6].name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9_-]+$'.", 'type': 'invalid_request_error', 'param': 'messages[6].name', 'code': 'invalid_value'}}
```

### Additional Information

_No response_

Labels: bug
State: closed