Skip to content
Open
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
20 changes: 18 additions & 2 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,20 @@ def _parse_structured_response_value(text: str, response_format: Any | None) ->
return None


def _message_text_for_structured_response(message: Message) -> str:
return "".join((content.text or "") for content in message.contents if content.type == "text")


def _messages_text_for_structured_response(
messages: Sequence[Message],
*,
separator: str,
strip: bool = False,
) -> str:
text = separator.join(_message_text_for_structured_response(message) for message in messages)
return text.strip() if strip else text


class ChatResponse(SerializationMixin, Generic[ResponseModelT]):
"""Represents the response to a chat request.

Expand Down Expand Up @@ -2407,7 +2421,8 @@ def value(self) -> ResponseModelT | None:
if self._value_parsed:
return self._value
if self._response_format is not None:
self._value = cast(ResponseModelT, _parse_structured_response_value(self.text, self._response_format))
text = _messages_text_for_structured_response(self.messages, separator="\n", strip=True)
self._value = cast(ResponseModelT, _parse_structured_response_value(text, self._response_format))
self._value_parsed = True
return self._value

Expand Down Expand Up @@ -2671,7 +2686,8 @@ def value(self) -> ResponseModelT | None:
if self._value_parsed:
return self._value
if self._response_format is not None:
self._value = cast(ResponseModelT, _parse_structured_response_value(self.text, self._response_format))
text = _messages_text_for_structured_response(self.messages, separator="")
self._value = cast(ResponseModelT, _parse_structured_response_value(text, self._response_format))
self._value_parsed = True
return self._value

Expand Down
30 changes: 30 additions & 0 deletions python/packages/core/tests/core/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,36 @@ def test_chat_response_with_mapping_response_format() -> None:
assert response.value["response"] == "Hello"


def test_chat_response_value_parses_split_structured_text_without_changing_message_text() -> None:
"""ChatResponse.value should not use Message.text spacing between structured output chunks."""
message = Message(role="assistant", contents=[Content.from_text('{ "respon'), Content.from_text('se": "Hello" }')])
response = ChatResponse(messages=message, response_format=OutputModel)

assert message.text == '{ "respon se": "Hello" }'
assert response.text == '{ "respon se": "Hello" }'
assert response.value is not None
assert response.value.response == "Hello"


def test_agent_response_value_parses_split_structured_text_without_changing_message_text() -> None:
"""AgentResponse.value should not use Message.text spacing between structured output chunks."""
message = Message(role="assistant", contents=[Content.from_text('{"response": "Hel'), Content.from_text('lo"}')])
response = AgentResponse(messages=message, response_format=OutputModel)

assert message.text == '{"response": "Hel lo"}'
assert response.text == '{"response": "Hel lo"}'
assert response.value is not None
assert response.value.response == "Hello"


def test_chat_response_value_handles_text_content_without_text() -> None:
"""ChatResponse.value should ignore text content with no text value."""
message = Message(role="assistant", contents=[Content.from_dict({"type": "text"})])
response = ChatResponse(messages=message, response_format=OutputModel)

assert response.value is None


def test_parse_structured_response_value_empty_text_with_pydantic_model() -> None:
"""Empty text should return None instead of raising when response_format is a Pydantic model."""
result = _parse_structured_response_value("", OutputModel)
Expand Down
Loading