1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Structured Output

Get structured JSON output from Claude: constrained generation, schema validation, and reliable data extraction patterns.

Last updated

After this section you can

  • Rank the ways to get JSON from a model and say which have expired on current Claude models
  • Get schema-valid output with output_config.format, messages.parse() or strict tools, and pick between them
  • Validate business rules the schema cannot express, with a bounded repair loop
22

Structured Output: JSON Every Time

“How do you make an LLM always return valid JSON?” is a standard interview question. On Claude the answer is a schema the API enforces, plus validation in your code for everything a schema cannot say.

Key idea

Give the API a JSON Schema in output_config.format and the reply is constrained to match it. That guarantees the shape. Whether the values make sense for your business is still your code’s job.

Climb until the shape is guaranteed, then validate the meaning yourself
GUARANTEE, WEAKEST TO STRONGEST EXPIRED assistant prefill: 400 on current models forced tool_choice: 400 on Fable 5.1 and Opus 5.5 1 · Ask in the prompt “Reply only with JSON”: no guarantee 2 · Strict tool "strict": true arguments always match the schema whether it calls the tool is up to Claude 3 · Structured outputs output_config.format the reply always matches the schema or messages.parse() with a model class the default for JSON answers 4 · Always, under every rung: validate business rules in your code (ranges, cross-field checks, IDs that exist) From rung 2 up the shape is guaranteed. The meaning never is.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium