diff --git a/python/packages/core/agent_framework/_types.py b/python/packages/core/agent_framework/_types.py index 736474de60..87799d0848 100644 --- a/python/packages/core/agent_framework/_types.py +++ b/python/packages/core/agent_framework/_types.py @@ -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): diff --git a/python/packages/core/tests/core/test_types.py b/python/packages/core/tests/core/test_types.py index 23c7dd8cd6..cf945dae0e 100644 --- a/python/packages/core/tests/core/test_types.py +++ b/python/packages/core/tests/core/test_types.py @@ -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, @@ -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."""