Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add missing support for 'assistant' Role in Converter.items_to_messages used by Runner.run_sync #93

Merged
merged 2 commits into from
Mar 12, 2025

Conversation

mjunaidca
Copy link
Contributor

Why are these changes needed?

The Agents SDK currently rejects messages with the 'assistant' role when processing conversation histories. Specifically, the _Converter.items_to_messages method raises a UserError with the message "Unexpected role in easy_input_message: assistant". This affects both direct usage of the converter and higher-level APIs like Runner.run_sync.

This is a critical issue because the 'assistant' role is a standard part of the OpenAI Chat Completions API and must be supported for building conversational agents that maintain context across multiple turns. Currently, any attempt to process a conversation history containing assistant responses fails, breaking functionality for many common use cases.

[Reproducible Example Code - Failing Case]

Minimal Reproducible Example 1

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?"},
    ]
)

# Results in: agents.exceptions.UserError: Unexpected role in easy_input_message: assistant

Minimal Reproducible Example 2

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)

Changes Made:

  • Updated the role validation logic in items_to_messages to accept 'assistant' as a valid role
  • Ensured proper conversion of assistant messages to maintain format consistency
  • Added test case for the failing case and implemented above fix.

There are no breaking changes @rm-openai . Now the method successfully processes message lists containing the 'assistant' role, enabling proper handling of multi-turn conversations with context preservation.

  • Testing:
    Unit tests have been added to verify correct handling of various message formats:
    • Messages with only user roles (existing functionality)
    • Messages with mixed user and assistant roles (new functionality)
    • Messages with invalid roles (error case)

Related issue number

#92 #64

Checks

  • No breaking changes
  • I've added tests corresponding to the changes introduced in this PR.
  • I've made sure all auto checks have passed.

- The _Converter.items_to_messages method was incorrectly rejecting 'assistant'
as a valid role in conversation messages, causing runtime errors when processing
standard chat completion message formats.
- This fix enables proper handling of
complete conversation contexts that include both user and assistant messages.
@benjaminbascary97
Copy link

Waiting on this to be merged! Hopefully

@rm-openai
Copy link
Collaborator

Looks like lint is failing

- Enhanced the readability of the test case by reformatting the expected output and input message structures.
- This change maintains the same functionality while making the test code cleaner and easier to understand.
@mjunaidca
Copy link
Contributor Author

Looks like lint is failing

@rm-openai Fixed the lint errors

@rm-openai rm-openai merged commit 96913b8 into openai:main Mar 12, 2025
5 checks passed
nakasy000 pushed a commit to nakasy000/openai-agents-python that referenced this pull request Mar 27, 2025
…t-role

Fix: Add missing support for 'assistant' Role in Converter.items_to_messages used by Runner.run_sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants