Skip to content
Merged
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
2 changes: 2 additions & 0 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,8 @@ class ContinuationToken(TypedDict):
def _parse_structured_response_value(text: str, response_format: Any | None) -> Any | None:
if response_format is None:
return None
if not text:
return None
if isinstance(response_format, type) and issubclass(response_format, BaseModel):
return response_format.model_validate_json(text)
if isinstance(response_format, Mapping):
Expand Down
27 changes: 27 additions & 0 deletions python/packages/core/tests/core/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
_get_data_bytes,
_get_data_bytes_as_str,
_parse_content_list,
_parse_structured_response_value,
_validate_uri,
add_usage_details,
validate_tool_mode,
Expand Down Expand Up @@ -813,6 +814,32 @@ def test_chat_response_with_mapping_response_format() -> None:
assert response.value["response"] == "Hello"


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)
assert result is None


def test_parse_structured_response_value_empty_text_with_mapping() -> None:
"""Empty text should return None instead of raising when response_format is a mapping."""
result = _parse_structured_response_value("", {"type": "object"})
assert result is None


def test_chat_response_value_with_empty_text_and_response_format() -> None:
"""ChatResponse.value should return None when text is empty and response_format is set."""
message = Message(role="assistant", contents=[""])
response = ChatResponse(messages=message, response_format=OutputModel)
assert response.value is None


def test_agent_response_value_with_empty_text_and_response_format() -> None:
"""AgentResponse.value should return None when text is empty and response_format is set."""
message = Message(role="assistant", contents=[""])
response = AgentResponse(messages=message, response_format=OutputModel)
assert response.value is None


def test_chat_response_value_raises_on_invalid_schema():
"""Test that value property raises ValidationError with field constraint details."""

Expand Down
Loading