Python: Fix structured_output propagation in ClaudeAgent#4137
Python: Fix structured_output propagation in ClaudeAgent#4137amitmukh wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Capture structured_output from ResultMessage in _get_stream() and propagate it to AgentResponse.value via a custom finalizer. Previously structured_output was silently discarded, making output_format unusable. Fixes microsoft#4095
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where structured_output from Claude's CLI was not being propagated to AgentResponse.value, making the output_format feature unusable. When ClaudeAgent is configured with output_format (JSON Schema), the Claude CLI correctly returns a ResultMessage with a structured_output field, but the agent framework was silently discarding it.
Changes:
- Modified
_get_stream()to capturestructured_outputfromResultMessageand yield it as a finalAgentResponseUpdatewithadditional_properties - Added custom
_finalize_response()method that extractsstructured_outputfrom updates and sets it asresponse.value - Added comprehensive test coverage with 4 new tests covering propagation, absence, streaming, and error scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/claude/agent_framework_claude/_agent.py | Adds structured_output capture in _get_stream(), custom _finalize_response() finalizer that propagates structured_output to response.value |
| python/packages/claude/tests/test_claude_agent.py | Adds 4 comprehensive tests for structured output propagation in various scenarios (present, absent, streaming, error) |
| @staticmethod | ||
| async def _create_async_generator(items: list[Any]) -> Any: | ||
| """Helper to create async generator from list.""" | ||
| for item in items: | ||
| yield item |
There was a problem hiding this comment.
The return type annotation 'Any' for this async generator helper is too broad. Consider specifying the actual type being yielded. While this doesn't affect functionality, a more precise type hint like 'AsyncIterator[Any]' would be clearer and more maintainable.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
|
@microsoft-github-policy-service agree company="Microsoft" |
Summary
structured_outputfromResultMessagenot being propagated toAgentResponse.valueClaudeAgentis configured withoutput_format, the CLI returnsstructured_outputinResultMessagebut_get_stream()was silently discarding itstructured_outputand makes it available viaresponse.valueFixes #4095
Changes
_agent.py: Capturestructured_outputfromResultMessage, yield as finalAgentResponseUpdatewithadditional_properties, custom finalizer to setresponse.valuetest_claude_agent.py: Add 4 tests for structured output propagation (present, absent, streaming, error case)Test plan