Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions python/packages/core/agent_framework/_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,13 @@ def _propagate_conversation_id(update: AgentResponseUpdate) -> AgentResponseUpda
map_chat_to_agent_update,
agent_name=self.name,
),
finalizer=partial(
self._finalize_response_updates, response_format=options.get("response_format") if options else None
finalizer=lambda updates: self._finalize_response_updates(
updates,
response_format=(
ctx_holder["ctx"]["chat_options"].get("response_format")
if ctx_holder["ctx"]
else (options.get("response_format") if options else None)
),
),
)
.with_transform_hook(_propagate_conversation_id)
Expand Down
26 changes: 26 additions & 0 deletions python/packages/core/tests/core/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ async def test_chat_client_agent_run_streaming(client: SupportsChatGetResponse)
assert result.text == "test streaming response another update"


async def test_chat_client_agent_run_streaming_with_default_response_format(client: SupportsChatGetResponse) -> None:
"""Test that response_format set via default_options produces a parsed value when streaming."""
from pydantic import BaseModel

class Greeting(BaseModel):
greeting: str

# Configure streaming responses to return valid JSON for the model
client.streaming_responses = [ # type: ignore[attr-defined]
[
ChatResponseUpdate(contents=[Content.from_text('{"greeting": "Hello"}')], role="assistant"),
]
]

agent = Agent(client=client, default_options={"response_format": Greeting})
stream = agent.run("Hello", stream=True)
async for _ in stream:
pass
result = await stream.get_final_response()

assert result.text == '{"greeting": "Hello"}'
assert result.value is not None
assert isinstance(result.value, Greeting)
assert result.value.greeting == "Hello"


async def test_chat_client_agent_create_session(client: SupportsChatGetResponse) -> None:
agent = Agent(client=client)
session = agent.create_session()
Expand Down
Loading