Skip to content

Commit f101469

Browse files
update: openai response api (#6622)
Co-authored-by: Victor Dibia <victordibia@microsoft.com>
1 parent cd15c08 commit f101469

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def _build_api_parameters(self: "OpenAIAgent", messages: List[Dict[str, Any]]) -
505505
if self._tools:
506506
api_params["tools"] = self._tools
507507
if self._json_mode:
508-
api_params["response_format"] = {"type": "json_object"}
508+
api_params["text"] = {"type": "json_object"}
509509
api_params["store"] = self._store
510510
api_params["truncation"] = self._truncation
511511
if self._last_response_id:

python/packages/autogen-ext/tests/test_openai_agent.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import pytest
66
from autogen_agentchat.base import Response
7-
from autogen_agentchat.messages import BaseChatMessage, TextMessage
8-
from autogen_core import CancellationToken, FunctionCall
7+
from autogen_agentchat.messages import BaseChatMessage, MultiModalMessage, TextMessage
8+
from autogen_core import CancellationToken, FunctionCall, Image
99
from autogen_core.models import UserMessage
1010
from autogen_core.tools import Tool, ToolSchema
1111
from autogen_ext.agents.openai import OpenAIAgent
@@ -446,7 +446,7 @@ async def test_build_api_params(agent: OpenAIAgent) -> None:
446446
agent._json_mode = True # type: ignore
447447
params = agent._build_api_parameters([{"role": "user", "content": "hi"}]) # type: ignore
448448
assert "text.format" not in params
449-
assert params.get("response_format") == {"type": "json_object"}
449+
assert params.get("text") == {"type": "json_object"}
450450

451451

452452
@pytest.mark.asyncio
@@ -568,3 +568,29 @@ async def test_from_config(agent: OpenAIAgent) -> None:
568568
assert loaded_agent._max_output_tokens == 1000 # type: ignore
569569
assert loaded_agent._store is True # type: ignore
570570
assert loaded_agent._truncation == "auto" # type: ignore
571+
572+
573+
@pytest.mark.asyncio
574+
async def test_multimodal_message_response(agent: OpenAIAgent, cancellation_token: CancellationToken) -> None:
575+
# Test that the multimodal message is converted to the correct format
576+
img = Image.from_base64(
577+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
578+
)
579+
multimodal_message = MultiModalMessage(content=["Can you describe the content of this image?", img], source="user")
580+
581+
# Patch client.responses.create to simulate image-capable output
582+
async def mock_responses_create(**kwargs: Any) -> Any:
583+
class MockResponse:
584+
def __init__(self) -> None:
585+
self.output_text = "I see a cat in the image."
586+
self.id = "resp-image-001"
587+
588+
return MockResponse()
589+
590+
agent._client.responses.create = AsyncMock(side_effect=mock_responses_create) # type: ignore
591+
592+
response = await agent.on_messages([multimodal_message], cancellation_token)
593+
594+
assert response.chat_message is not None
595+
assert isinstance(response.chat_message, TextMessage)
596+
assert "cat" in response.chat_message.content.lower()

0 commit comments

Comments
 (0)