Study FAQ
Why can Claude structured output fail validation, and how should I handle it?
Also asked as 2
- Why is Claude’s JSON incomplete or invalid even when I use structured outputs?
- How do I handle refusal, token limits, enum casing, and schema errors in Claude structured outputs?
Answer
Check the Claude API completion state before parsing a structured response. An HTTP 200 can carry stop_reason: "refusal", which can produce text outside the schema. stop_reason: "max_tokens" can leave an incomplete result. Route a refusal to the defined fallback or review path. For truncation, consider a bounded retry with a larger output limit if the task and model budget allow it.
A response can complete normally while differing from a declared string enum or const only in case. This documented caveat applies to JSON outputs and strict tool use. Choose schema values that are distinct after case normalization. If policy allows casing differences, map the value back to a unique allowed value before application acceptance and use exact matching for fields whose case carries meaning.
If the API rejects an unsupported schema feature or fails to compile a combined schema, fix the request. Check the supported subset, simplify nested and optional fields, or split the work across requests. A grammar can be too complex even when each published count limit is met. Retrying the unchanged request will leave those design problems in place.
Claude structured output exceptions and limits · Claude API stop reasons
Full explanation
Structured Outputs, Validation, Retry, and Batch Patterns
Study guide · CCAR-F
Read the full explanation