Three gates of structured output
Prompt-level requests, API-level schema constraints, and protocol-side validation each cover one stretch — valid format never means correct content.
Prompt effect has not been tested.
A prompt is a re-runnable template, not a promise of results. Changing the model, version or input can change the outcome. Use the checkpoints on this page to verify it yourself, and treat the official sources as evidence of mechanism rather than a guarantee of effect.
What this guide covers
"Please output JSON" is a request, not a guarantee. Making output consumable by programs takes three gates: request the format in the prompt, constrain generation with a schema at the API level, and validate results at the protocol level. The three gates cover different things and fail in different ways. We have not run hands-on tests; the mechanisms below come from official docs, specs, and papers.
Prompt level: tags and prefill
Anthropic's official tutorial teaches two moves. First: have the model wrap the answer in XML tags, so a program can extract what sits between them. Second is prefill: put the opening tag or { into the assistant turn, and the model continues from that position. The official wording is deliberately restrained: enforcing JSON output is "not deterministically, but close to it". A companion trick: set the closing tag as stop_sequences so generation stops as soon as the answer is out, skipping the closing remarks.
The prompt level is cheap and works with any model, but it is only guidance. The model can still drift; downstream code must tolerate that.
API level: schema constraints
OpenAI's Structured Outputs pushes the constraint down into generation itself. Official text: it "guarantees the model will always generate responses that adhere to your supplied JSON Schema". Enable it with strict: true, on either a response format or function definitions.
Keep the difference from JSON mode clear: JSON mode only guarantees the output is valid JSON; Structured Outputs constrains the shape as well. Official use cases: rendering structured answers in a UI, extracting document content into a database, extracting entities to call tools.
Two engineering details: when the model refuses for safety reasons, the refusal does not follow your schema — the API marks it with a separate refusal field, so handle it before deserializing. The SDK ships a parse helper that accepts a Pydantic model instead of a hand-written schema.
Decoding level: grammar-constrained generation
Where does the API-level guarantee come from? Grammar-constrained decoding (GCD) is one mechanism behind it. The paper's approach: describe the output space with a formal grammar, and allow only grammar-conforming tokens during generation. Structure is thereby guaranteed rather than left to the model's self-discipline. The paper also introduces input-dependent grammars: different inputs can map to different output structures. This route needs no finetuning, which suits structured tasks where training data is scarce or finetuning is expensive.
Protocol level: MCP's two schemas
Tool protocols write validation into the spec. In an MCP tool definition: inputSchema constrains parameters; outputSchema optionally constrains the result structure. The spec requires: when an outputSchema is provided, servers MUST return conforming results, and clients SHOULD validate against the schema.
One easily confused concept gets an explicit clarification in the spec: "structuredContent is server-produced result data and is unrelated to LLM 'structured outputs'". In other words, MCP's structured content is data produced by the server, a different thing from schema-constrained model generation. The two gates guard separate doors.
Failure modes
- Valid is not correct: JSON parses fine and field values can still be wrong. Semantic correctness needs evals.
- Refusals pierce the schema: safety refusals do not follow your format, and hard deserialization errors out. Check refusal first.
- Prefill is not a guarantee: it pushes the model toward the format without locking it in. Downstream still validates.
- Confusing the gates: treating server-returned structured data as a model-generation guarantee, or vice versa.
Stronger constraints mean less flexibility: an over-rigid schema leaves the model no exit on edge inputs. Leave reasonable optional fields and a refusal path in the design.
Sources
- OpenAI Cookbook · Introduction to Structured Outputs
- Anthropic official tutorial · Formatting Output and Speaking for Claude
- MCP specification · Tools
- Grammar-Constrained Decoding (arXiv 2305.13971)
Verified 2026-11-07; all four fetched directly with HTTP 200. The OpenAI and Anthropic doc sites were unreachable from this network; official content was taken from raw files in both vendors' official repositories.
Sources
| Source | URL |
|---|---|
| OpenAI Cookbook · Introduction to Structured Outputs (official repo raw file, direct fetch 200 on 2026-11-07) | https://raw.githubusercontent.com/openai/openai-cookbook/main/examples/Structured_Outputs_Intro.ipynb |
| Anthropic official tutorial · Formatting Output and Speaking for Claude (official repo raw file, direct fetch 200 on 2026-11-07) | https://raw.githubusercontent.com/anthropics/courses/master/prompt_engineering_interactive_tutorial/Anthropic%201P/05_Formatting_Output_and_Speaking_for_Claude.ipynb |
| MCP specification · Tools (direct fetch 200 on 2026-11-07) | https://modelcontextprotocol.io/specification/2025-06-18/server/tools |
| Grammar-Constrained Decoding paper (arXiv 2305.13971, abstract page direct-fetched 2026-11-07) | https://arxiv.org/abs/2305.13971 |
Source snapshot: 2026-11-07; item-specific rechecks and their scope are recorded in the text or source notes. Features and terms may change; check the original sources before use.