Repository: openai/openai-agents-python

Issue:
Bug: Runner.run_sync and Converter.items_to_messages Rejects 'assistant' Role in Message Conversion Input
### Please read this first

- **Have you read the docs?**[Agents SDK docs](https://openai.github.io/openai-agents-python/) - Yes
- **Have you searched for related issues?** Others may have faced similar issues. - Yes

### Describe the bug
The _Converter.items_to_messages method in the OpenAI chat completions module is currently rejecting messages with the 'assistant' role, raising a UserError with the message "Unexpected role in easy_input_message: assistant". This prevents proper handling of standard conversation formats that include both user and assistant messages, which is a critical issue for building conversational agents.

### Debug information
- Agents SDK version: 0.0.3
- Python version: 3.11, 3.12

### Repro steps
**To Reproduce**
The issue can be reproduced with the following code examples:

#### Example 1:
```python
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel


external_client = AsyncOpenAI()

model = OpenAIChatCompletionsModel(
    model="gpt-4o",
    openai_client=external_client
)

agent: Agent = Agent(name="Assistant", instructions="You are a helpful assistant", model=model)

history = [{"role": "user", "content": "I am Junaid"}, {"role": "assistant", "content": "Hello?"}, {"role": "user", "content": "What was my Name?"}]

result = Runner.run_sync(agent, history)

print("\nCALLING AGENT\n")
print(result.final_output)
```

#### Example 2:
```python
from agents.models.openai_chatcompletions import _Converter

messages = _Converter.items_to_messages(
    [
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hello?"},
        {"role": "user", "content": "What was my Name?"},
    ]
)
```

This results in the following error:
```
Traceback (most recent call last):
  File "/path/to/script.py", line 3, in <module>
    messages = _Converter.items_to_messages(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/agents/models/openai_chatcompletions.py", line 812, in items_to_messages
    raise UserError(f"Unexpected role in easy_input_message: {role}")
agents.exceptions.UserError: Unexpected role in easy_input_message: assistant
```

### Expected behavior
The `items_to_messages` method should accept 'assistant' as a valid role, as it is a standard role in the OpenAI Chat Completions API. The method should successfully convert all message items, including those with the 'assistant' role, into the appropriate message format.

**Additional context**
This bug affects any code that attempts to process conversation histories containing assistant responses. The 'assistant' role is a core part of the OpenAI Chat Completions API specification and must be supported for proper conversation handling. The proposed fix is to update the role validation logic to explicitly accept 'assistant' as a valid role.

Discussion:
@rm-openai: Thank you for fixing via #93!

Solution patch:
diff --git a/docs/agents.md b/docs/agents.md
index 9b6264b5..17589b3d 100644
--- a/docs/agents.md
+++ b/docs/agents.md
@@ -13,6 +13,7 @@ The most common properties of an agent you'll configure are:
 ```python
 from agents import Agent, ModelSettings, function_tool
 
+@function_tool
 def get_weather(city: str) -> str:
     return f"The weather in {city} is sunny"
 
@@ -20,7 +21,7 @@ agent = Agent(
     name="Haiku agent",
     instructions="Always respond in haiku form",
     model="o3-mini",
-    tools=[function_tool(get_weather)],
+    tools=[get_weather],
 )
 ```
 
diff --git a/docs/context.md b/docs/context.md
index 5dcacebe..69c43fbb 100644
--- a/docs/context.md
+++ b/docs/context.md
@@ -36,6 +36,7 @@ class UserInfo:  # (1)!
     name: str
     uid: int
 
+@function_tool
 async def fetch_user_age(wrapper: RunContextWrapper[UserInfo]) -> str:  # (2)!
     return f"User {wrapper.context.name} is 47 years old"
 
@@ -44,7 +45,7 @@ async def main():
 
     agent = Agent[UserInfo](  # (4)!
         name="Assistant",
-        tools=[function_tool(fetch_user_age)],
+        tools=[fetch_user_age],
     )
 
     result = await Runner.run(
diff --git a/src/agents/models/openai_chatcompletions.py b/src/agents/models/openai_chatcompletions.py
index a7340d05..c20f5bb9 100644
--- a/src/agents/models/openai_chatcompletions.py
+++ b/src/agents/models/openai_chatcompletions.py
@@ -808,6 +808,13 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
                         "content": cls.extract_text_content(content),
                     }
                     result.append(msg_developer)
+                elif role == "assistant":
+                    flush_assistant_message()
+                    msg_assistant: ChatCompletionAssistantMessageParam = {
+                        "role": "assistant",
+                        "content": cls.extract_text_content(content),
+                    }
+                    result.append(msg_assistant)
                 else:
                     raise UserError(f"Unexpected role in easy_input_message: {role}")
 


Test patch:
diff --git a/tests/test_openai_chatcompletions_converter.py b/tests/test_openai_chatcompletions_converter.py
index 8cf07d7c..73acb8ab 100644
--- a/tests/test_openai_chatcompletions_converter.py
+++ b/tests/test_openai_chatcompletions_converter.py
@@ -393,3 +393,38 @@ def test_unknown_object_errors():
     with pytest.raises(UserError, match="Unhandled item type or structure"):
         # Purposely ignore the type error
         _Converter.items_to_messages([TestObject()])  # type: ignore
+
+
+def test_assistant_messages_in_history():
+    """
+    Test that assistant messages are added to the history.
+    """
+    messages = _Converter.items_to_messages(
+        [
+            {
+                "role": "user",
+                "content": "Hello",
+            },
+            {
+                "role": "assistant",
+                "content": "Hello?",
+            },
+            {
+                "role": "user",
+                "content": "What was my Name?",
+            },
+        ]
+    )
+
+    assert messages == [
+        {"role": "user", "content": "Hello"},
+        {"role": "assistant", "content": "Hello?"},
+        {"role": "user", "content": "What was my Name?"},
+    ]
+    assert len(messages) == 3
+    assert messages[0]["role"] == "user"
+    assert messages[0]["content"] == "Hello"
+    assert messages[1]["role"] == "assistant"
+    assert messages[1]["content"] == "Hello?"
+    assert messages[2]["role"] == "user"
+    assert messages[2]["content"] == "What was my Name?"