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

Production7 min readFirst readTool Use with Claude

After this section you can

  • Climb the reliability ladder from prompt-and-pray to a schema-constrained response
  • Use native structured outputs and strict tool use, and say which fits which job
  • Explain why a validation layer is still required when the schema subset excludes your constraint
29

Structured Output: JSON Every Time

“How do you make an LLM always return valid JSON?” is a top-5 FDE interview question, and the answer is never “ask nicely in the prompt.” It’s a spectrum of enforcement — from hoping, to guaranteeing — plus a validation layer that assumes the model will still occasionally lie.

THE CENTRAL IDEA

An LLM is a text generator, not a schema-aware serializer — left to itself it will happily emit prose, markdown fences, or almost-valid JSON that breaks your parser at 2am. Production systems need output that downstream code (APIs, databases, UIs) can consume deterministically. So structured output is about moving up a reliability ladder: from prompt-and-pray, to provider features that constrain the response, to constrained decoding that makes invalid tokens literally impossible — and then validating anyway. With Claude the workhorse rung is now native structured outputs: you hand the API a JSON Schema and the response is constrained to match it.

The reliability ladder — five ways to get structured output, weakest to strongest
flexible · unreliable constrained · guaranteed valid → 1 · Prompt & hope fences · prose · commas 2 · JSON mode valid JSON, but not YOUR schema 3 · Force a tool tool_choice the old default 4 · Native schema output_config.format strict: true on tools response matches schema CLAUDE DEFAULT 5 · Constrained grammar / CFG / regex self-hosted decoding VALIDATION LAYER — wrap any rung; the model can still violate intent

Related

More in LLM & Agentic

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

Unlock Premium