Skip to content

Conversation

@bogini
Copy link
Collaborator

@bogini bogini commented Nov 18, 2025

Add structured output support to Python SDK.

Usage

from claude_agent_sdk import query, ClaudeAgentOptions

schema = {
    "type": "object",
    "properties": {"count": {"type": "number"}},
    "required": ["count"]
}

async for msg in query(
    prompt="Count files in src/",
    options=ClaudeAgentOptions(
        output_format={"type": "json_schema", "schema": schema}
    )
):
    if hasattr(msg, 'structured_output'):
        print(msg.structured_output)

Documentation

https://docs.claude.com/en/docs/agent-sdk/structured-outputs

Tests

  • Unit tests: tests/test_integration.py::TestIntegration::test_structured_output
  • E2E tests: e2e-tests/test_structured_output.py (4 tests)

Add structured output support to Python SDK, matching TypeScript implementation.

API:
```python
from claude_agent_sdk import query, ClaudeAgentOptions

schema = {
    "type": "object",
    "properties": {"count": {"type": "number"}},
    "required": ["count"]
}

async for msg in query(
    prompt="Count files in src/",
    options=ClaudeAgentOptions(
        output_format={"type": "json_schema", "schema": schema}
    )
):
    if hasattr(msg, 'structured_output'):
        print(msg.structured_output)
```

See: https://docs.claude.com/en/docs/agent-sdk/structured-outputs
@bogini bogini force-pushed the inigo/python-sdk-structured-output branch from cd2e29a to 073d95e Compare November 18, 2025 18:06
@bogini bogini force-pushed the inigo/python-sdk-structured-output branch from 073d95e to ba5eb1f Compare November 18, 2025 18:09
@bogini bogini marked this pull request as ready for review November 18, 2025 18:56
@bogini bogini requested a review from ashwin-ant November 18, 2025 18:56
@bogini bogini merged commit 50d6840 into main Nov 18, 2025
51 of 52 checks passed
@bogini bogini deleted the inigo/python-sdk-structured-output branch November 18, 2025 19:01
@zhammer
Copy link

zhammer commented Dec 1, 2025

heyo, the docs say that the following beta header has to be set - structured-outputs-2025-11-13 - but there's nothing to set that in this PR. it looks like that was added in the not-merged #328.

should we expect this to work without that beta header being set?

@ashwin-ant
Copy link
Collaborator

@zhammer yes, this works in the Agent SDK without any configuration on your part.

@zhammer
Copy link

zhammer commented Dec 1, 2025

@ashwin-ant great, just confirmed on our end. i was just a bit confused by the discrepancy with docs. thank you

@ashwin-ant
Copy link
Collaborator

For the agent sdk there are specific docs on structured output here: https://platform.claude.com/docs/en/agent-sdk/structured-outputs

@Shwapx
Copy link

Shwapx commented Dec 3, 2025

Can you use the structured output with ClaudeSDKClient or it's only for query()

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants