Skip to content

Comments

Python: Fix structured_output propagation in ClaudeAgent#4137

Open
amitmukh wants to merge 1 commit intomicrosoft:mainfrom
amitmukh:fix/claude-structured-output
Open

Python: Fix structured_output propagation in ClaudeAgent#4137
amitmukh wants to merge 1 commit intomicrosoft:mainfrom
amitmukh:fix/claude-structured-output

Conversation

@amitmukh
Copy link

Summary

  • Fix structured_output from ResultMessage not being propagated to AgentResponse.value
  • When ClaudeAgent is configured with output_format, the CLI returns structured_output in ResultMessage but _get_stream() was silently discarding it
  • This change captures structured_output and makes it available via response.value

Fixes #4095

Changes

  • _agent.py: Capture structured_output from ResultMessage, yield as final AgentResponseUpdate with additional_properties, custom finalizer to set response.value
  • test_claude_agent.py: Add 4 tests for structured output propagation (present, absent, streaming, error case)

Test plan

  • All 49 existing + new unit tests pass
  • New tests cover: propagation, None case, streaming, error case

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
Copilot AI review requested due to automatic review settings February 20, 2026 19:43
@github-actions github-actions bot changed the title Fix structured_output propagation in ClaudeAgent Python: Fix structured_output propagation in ClaudeAgent Feb 20, 2026
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL21262331584% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python Unit Test Overview

Tests Skipped Failures Errors Time
4193 240 💤 0 ❌ 0 🔥 1m 13s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 capture structured_output from ResultMessage and yield it as a final AgentResponseUpdate with additional_properties
  • Added custom _finalize_response() method that extracts structured_output from updates and sets it as response.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)

Comment on lines +796 to +800
@staticmethod
async def _create_async_generator(items: list[Any]) -> Any:
"""Helper to create async generator from list."""
for item in items:
yield item
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

@amitmukh
Copy link
Author

@microsoft-github-policy-service agree company="Microsoft"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants