Skip to content

Bug: Runner.run_sync and Converter.items_to_messages Rejects 'assistant' Role in Message Conversion Input #92

Closed
@mjunaidca

Description

@mjunaidca

Please read this first

  • Have you read the docs?Agents SDK docs - 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:

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions