Skip to content

Commit 2bcc87c

Browse files
committed
feat: add structured outputs support with Pydantic integration
Add support for Anthropic's structured outputs beta feature using the anthropic-beta: structured-outputs-2025-11-13 header. Enables type-safe JSON responses through Pydantic models or raw JSON schemas. Features: - Pydantic v1/v2 automatic detection and schema conversion - Per-query output_format parameter for flexibility - Deep copy schema validation to prevent mutations - Comprehensive examples demonstrating advanced Pydantic features - Full test coverage (34 new tests, 151 total passing) Implementation: - Added schema_utils.py for Pydantic → JSON schema conversion - Modified query() and ClaudeSDKClient.query() to accept output_format - Created examples/structured_outputs.py with 4 production scenarios - Updated README.md with usage examples and documentation Note: Actual functionality blocked pending CLI support for --json-schema flag (see anthropics/claude-code#9058). Infrastructure complete and ready.
1 parent ff425b2 commit 2bcc87c

File tree

10 files changed

+1563
-1
lines changed

10 files changed

+1563
-1
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,58 @@ options = ClaudeAgentOptions(
7676
)
7777
```
7878

79+
### Structured Outputs
80+
81+
Structured outputs allow you to get responses from Claude in a specific JSON schema format, ensuring type-safe and predictable responses.
82+
83+
**Note:** This feature requires Claude Code CLI support for passing schemas (see [anthropics/claude-code#9058](https://github.com/anthropics/claude-code/issues/9058)). The SDK provides the public API and schema conversion utilities, ready for when CLI support is added.
84+
85+
#### Using Raw JSON Schema
86+
87+
```python
88+
from claude_agent_sdk import query
89+
90+
# Define your expected output schema
91+
schema = {
92+
"type": "object",
93+
"properties": {
94+
"name": {"type": "string"},
95+
"email": {"type": "string"},
96+
"plan_interest": {"type": "string"},
97+
"demo_requested": {"type": "boolean"}
98+
},
99+
"required": ["name", "email", "plan_interest", "demo_requested"]
100+
}
101+
102+
async for message in query(
103+
prompt="Extract info: John Smith ([email protected]) wants Enterprise plan demo",
104+
output_format=schema
105+
):
106+
print(message)
107+
```
108+
109+
#### Using Pydantic Models (Recommended)
110+
111+
```python
112+
from pydantic import BaseModel, Field
113+
from claude_agent_sdk import query
114+
115+
class EmailExtraction(BaseModel):
116+
"""Structured data from an email."""
117+
name: str = Field(description="Full name")
118+
email: str = Field(description="Email address")
119+
plan_interest: str = Field(description="Plan they're interested in")
120+
demo_requested: bool = Field(description="Whether they requested a demo")
121+
122+
async for message in query(
123+
prompt="Extract info: Sarah ([email protected]) wants Professional plan demo",
124+
output_format=EmailExtraction # Pass the Pydantic model class
125+
):
126+
print(message)
127+
```
128+
129+
For more examples, see [examples/structured_outputs.py](examples/structured_outputs.py).
130+
79131
## ClaudeSDKClient
80132

81133
`ClaudeSDKClient` supports bidirectional, interactive conversations with Claude
@@ -271,6 +323,8 @@ See [examples/quick_start.py](examples/quick_start.py) for a complete working ex
271323

272324
See [examples/streaming_mode.py](examples/streaming_mode.py) for comprehensive examples involving `ClaudeSDKClient`. You can even run interactive examples in IPython from [examples/streaming_mode_ipython.py](examples/streaming_mode_ipython.py).
273325

326+
See [examples/structured_outputs.py](examples/structured_outputs.py) for structured outputs examples using both Pydantic models and raw JSON schemas.
327+
274328
## Migrating from Claude Code SDK
275329

276330
If you're upgrading from the Claude Code SDK (versions < 0.1.0), please see the [CHANGELOG.md](CHANGELOG.md#010) for details on breaking changes and new features, including:

0 commit comments

Comments
 (0)