Guaranteed JSON Schema Compliance for Claude Code Output
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
Claude Code cannot guarantee output matches a specific JSON schema. --output-format json returns Claude Code's wrapper structure, with actual data nested inside:
{
"messages": [...],
"result": {
"content": [{"type": "text", "text": "{\"countries\": [...]}"}]
}
}
Users must extract and validate the nested JSON against their schema. Tool calling improves reliability but doesn't guarantee 100% schema compliance, requiring validation and error handling in production pipelines.
Proposed Solution
Add a --json-schema flag that enforces schema compliance via constrained decoding:
claude -p "List countries with capitals" \
--output-format json \
--json-schema countries.schema.json
The nested response content is guaranteed to match the provided JSON schema.
Alternative Solutions
- Tool calling enhancement: Add
--strict-schemaflag to existing tool calling mechanism - New output format:
--output-format strict-jsonthat takes schema as input - Configuration file: Schema definitions in
.claude/schemas/directory
Priority
High - Significant impact on productivity
Feature Category
CLI commands and flags
Use Case Example
Schema file (countries.schema.json):
{
"type": "array",
"items": {
"type": "object",
"required": ["country", "capital"],
"properties": {
"country": {"type": "string"},
"capital": {"type": "string"}
}
}
}
Command:
claude -p "List 3 countries with capitals" \
--output-format json \
--json-schema countries.schema.json | jq '.result.content[0].text | fromjson'
Guaranteed output format:
[
{"country": "France", "capital": "Paris"},
{"country": "Germany", "capital": "Berlin"},
{"country": "Japan", "capital": "Tokyo"}
]
Can pipe directly to databases, APIs, or processing scripts without validation.
Additional Context
Current workarounds:
- Manual validation with libraries like
json-repair - Retry loops with error handling
- Careful prompting (unreliable)
Comparison:
OpenAI provides this via Structured Outputs with strict: true flag, guaranteeing 100% schema compliance at token generation level.
Related issues: #586, #2904, #3383
Benefits:
- Eliminates validation code in production
- Enables reliable CI/CD automation
- Reduces error handling complexity
4 Comments
Structured outputs is now in public beta on the developer platform, so hopefully this means it will make it's way into Claude Code soon!
Announcement: https://claude.com/blog/structured-outputs-on-the-claude-developer-platform
Docs: https://docs.claude.com/en/docs/build-with-claude/structured-outputs
SDK Use Case: Structured Outputs with Pydantic Integration
This feature would enable production-grade structured outputs support in the Claude Agent SDK for Python. We've implemented and validated the complete infrastructure, which is ready to integrate once CLI support is available.
Implementation Status
PR #328 contains production-ready SDK infrastructure:
output_formatparameter)Link: https://github.com/anthropics/claude-agent-sdk-python/pull/328
Validated API Format
Through comprehensive testing, we've confirmed the correct schema format:
Current SDK Usage
The SDK automatically:
additionalProperties: falseanthropic-beta: structured-outputs-2025-11-13headerValidation Results
We've validated the implementation through the Claude Code CLI (with HTTP interception):
anthropic-beta: structured-outputs-2025-11-13Production-Ready Examples
The SDK includes sophisticated examples demonstrating:
Integration Path
Once CLI adds schema support, the integration flow would be:
The SDK is designed to integrate with whatever mechanism the CLI team implements for schema passing.
Ready to collaborate on implementation details or provide additional context.
---
References:
TESTING.md,VALIDATION_RESULTS.mdin PRThis is available in 2.0.45 via
--json-schema. The Agent SDK has also added support: https://docs.claude.com/en/docs/agent-sdk/structured-outputsThis issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.