Skip to content

Feature: Add OpenAIAgent backed by OpenAI Response API #6418

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

jay-thakur
Copy link
Contributor

Why are these changes needed?

This PR introduces a new OpenAIAgent implementation that uses the OpenAI Response API as its backend. The OpenAI Assistant API will be deprecated in 2026, and the Response API is its successor. This change ensures our codebase is future-proof and aligned with OpenAI’s latest platform direction.

Motivation

  • Deprecation Notice: The OpenAI Assistant API will be deprecated in 2026.
  • Future-Proofing: The Response API is the recommended replacement and offers improved capabilities for stateful, multi-turn, and tool-augmented conversations.
  • AgentChat Compatibility: The new agent is designed to conform to the behavior and expectations of AssistantAgent in AgentChat, but is implemented directly on top of the OpenAI Response API.

Key Changes

  • New Agent: Adds OpenAIAgent, a stateful agent that interacts with the OpenAI Response API.
  • Stateful Design: The agent maintains conversation state, tool usage, and other metadata as required by the Response API.
  • AssistantAgent Parity: The new agent matches the interface and behavior of AssistantAgent in AgentChat, ensuring a smooth migration path.
  • Direct OpenAI Integration: Uses the official openai Python library for all API interactions.
  • Extensible: Designed to support future enhancements, such as advanced tool use, function calling, and multi-modal capabilities.

Migration Path

  • Existing users of the Assistant API should migrate to the new OpenAIAgent to ensure long-term compatibility.
  • Documentation and examples will be updated to reflect the new agent and its usage patterns.

References


Related issue number

Closes #6032

Checks

Jay Prakash Thakur and others added 2 commits April 27, 2025 18:41
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

Attention: Patch coverage is 88.02817% with 34 lines in your changes missing coverage. Please review.

Project coverage is 79.67%. Comparing base (9bbcfa0) to head (c6febc8).

Files with missing lines Patch % Lines
...ext/src/autogen_ext/agents/openai/_openai_agent.py 87.94% 34 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6418      +/-   ##
==========================================
+ Coverage   79.53%   79.67%   +0.14%     
==========================================
  Files         225      226       +1     
  Lines       16650    16933     +283     
==========================================
+ Hits        13242    13491     +249     
- Misses       3408     3442      +34     
Flag Coverage Δ
unittests 79.67% <88.02%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

if self._tools:
api_params["tools"] = self._tools
if self._json_mode:
api_params["text.format"] = "json" # Responses API uses text.format for structured outputs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_schema is recommended by official doc.

if self._seed is not None:
api_params["seed"] = self._seed
if self._tools:
api_params["tools"] = self._tools
if self._json_mode:
api_params["text.format"] = "json" # Responses API uses text.format for structured outputs
api_params["json_schema"] = {"type": "object"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to turn of JSON mode, this line should be changed to

api_params["text"] = { "format": { "type": "json_object" } }

Please refer https://platform.openai.com/docs/guides/structured-outputs#structured-outputs-vs-json-mode.

Why not considering structured outputs in here?

api_params["text"] = { "format": { "type": "json_schema", "strict": true, "schema": "..." } }

history: List[Dict[str, Any]] = Field(default_factory=list)


class OpenAIAgentConfig(BaseModel):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Default value of temperature parameter is 1, and the range should be between 0 and 2.
  2. Default value of store parameter is True.
  3. Default value of truncation parameter is "disabled".
  4. The OpenAI Response API doesn't have the seed parameter.

if isinstance(part, TextMessage):
content_parts.append({"type": "text", "text": str(part.content)})
elif isinstance(part, ImageMessage):
image_url = str(part.content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self._message_history:
input_messages.extend(self._message_history)

for message in messages:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can optimize 560-578 lines for better code readability.

return "[image]"

def to_text(self) -> str:
return f"[Image: {self.content[:30]}...]" if len(self.content) > 30 else f"[Image: {self.content}]"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the hardcoded 30 lengths? I think it would be useful to test the image functionality and some of these edge cases.

if self._tools:
api_params["tools"] = self._tools
if self._json_mode:
api_params["json_schema"] = {"type": "object"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this should be:
api_params["response_format"] = { "type": "json_object" }
Base on:
https://platform.openai.com/docs/api-reference/responses/create

@jay-thakur
Copy link
Contributor Author

ack all comments.

@bassmang
Copy link
Collaborator

ack all comments.

@jay-thakur are you working on addressing these? If not I'm happy to pick them up

@jay-thakur
Copy link
Contributor Author

yes, I am. will address all these comments and raise updated PR. @bassmang

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.

Introduce OpenAIAgent backed by the Response API in Extensions.
3 participants