waynetools

Guides · LLM & Structured Output

Structured Outputs with JSON Schema — patterns that hold

The five schema rules that make GPT, Claude and Gemini return the JSON you asked for — plus the repair-and-retry loop for the failure modes that survive anyway.

Patterns verified 2026-07-11 OpenAI strict · Anthropic tool_use · Gemini
Direct answer

Reliable structured outputs come from five habits: declare every property required (use null-unions for optionals), set additionalProperties: false on every object, prefer enums over free text, keep nesting shallow, and treat field descriptions as instructions. Then validate and retry with the errors — never trust, always check.

§1 · JSON mode is not structured output

"JSON mode" guarantees the response parses. It does not guarantee your field names, your types, or that status is one of the four values your database accepts. Structured outputs — schema-constrained generation — move that contract from the prompt (a request) into the API (an enforcement). The difference shows up at volume: a prompt-only pipeline that fails 2% of the time fails 20,000 times at a million calls.

There is also a cost angle: schema-constrained answers are consistently shorter than prose-wrapped ones, and output tokens cost 4–6× input tokens on every provider (see the July 2026 pricing table). The schema is a budget instrument, not just a correctness one.

§2 · The baseline recipe (works on all three providers)

Write the schema as if OpenAI strict mode were the target, even when it is not — it is the most demanding dialect, so it is the portable one:

Strict-ready fragments

PatternSchema fragment
Required + nullable"notes": { "type": ["string","null"], "description": "null when none" }
Closed object"additionalProperties": false — on EVERY object
Enum switch"status": { "enum": ["queued","running","done","failed"] }
Typed number"qty": { "type": "integer", "minimum": 0 }

Build and export this shape in one click — strict-mode OpenAI or Anthropic tool_use — with the JSON Schema Builder.

§3 · Provider dialects, one paragraph each

§4 · The failure modes that survive — and the repair loop

Even schema-constrained pipelines see malformed output at the edges (long context, high temperature, truncation). The five classics, in observed order of frequency:

The production pattern is a two-step loop: repair mechanically, then validate against the schema; on failure, retry once feeding the exact validation errors back to the model. The repair step resolves most incidents without a second API call — which, at the output prices in the July 2026 table, is the cheapest retry you will ever ship.

Test the loop on your own responses: paste raw model output + schema, get repairs and validation errors.

Open output validator →

§5 · FAQ

What is the difference between JSON mode and structured outputs?

JSON mode guarantees the output parses; structured outputs guarantee it matches YOUR schema — names, types, enums, required keys. At production volume, only the second one is a contract.

How do I make a field optional in strict mode?

You don't — you make it nullable: "type": ["string","null"], keep it in required, and treat null as absent. This is the strict-mode idiom for optionality.

My schema is correct but responses still fail. Why?

Look for fences, surrounding prose, trailing commas, truncation (max tokens too low) and number-vs-integer drift. Run the repair step before validation; feed validation errors back on retry.

Does one schema work across GPT, Claude and Gemini?

Yes, if you write it to strict-mode rules — the most demanding dialect. Objects with everything required, additionalProperties: false, enums and shallow nesting behave well on all three.

§6 · Related tools