Zenaique

Match each structured output toolkit to how it enforces the schema

Match pairs·Medium·4.0 · 0·~2 min·Asked atRedisSourcegraphWorkday
Attempt it

Drag each answer to line up with its matching prompt

OpenAI Structured Outputs

Pydantic model + retry and repair loop on the client side; works across providers

Anthropic tool use with input_schema

Server side JSON mode with a schema field on the generation config

Instructor (Python)

Zod schema enforced by the SDK with retries; framework agnostic on the server

Vercel AI SDK generateObject

Server side constrained decoding bound to a JSON Schema; invalid output is impossible by construction

Outlines / SGLang grammar

Tool definition includes a JSON Schema the model is biased to match; library validates on receive

Google Gemini responseSchema

Local constrained decoding via regex or context free grammar at the token level

TL;DR

Three toolkits enforce schema on the server (OpenAI, Anthropic, Gemini), three on the client (Instructor, Vercel AI SDK, Outlines self-hosted); Outlines is the only true grammar-level constraint.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine six checkout lines in a grocery store. Three of them have a real machine that physically refuses to scan an item that is not on your list, so you cannot leave with the wrong thing. Three of them have a kind cashier who looks at your bag at the end, and if something is wrong, sends you back to fix it. Both keep your bag correct, but the machine catches the error before it happens and the cashier catches it after. Some lines are at the store you visit (server-side); some lines are at a check station in your kitchen at home (client-side). Different stores have different machines, and one of them, Outlines, has a special scanner that checks the shape of every single item, not just the bag at the end.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

On a feature-comparison page these six toolkits look like the same thing with different vendor logos. They all take a schema, they all return structured JSON, they all 'enforce' it. Pick whichever fits your stack.

In production they fail differently and that is where the choice gets interesting. A team that picks the wrong one is not breaking on the happy path. They are breaking on the edges: a tail input where the schema-invalid output costs a tool call retry; a schema compile that adds 200ms to p99; a cross-provider deployment where one of the providers does not honor the schema as strictly as the other.

The right way to read this matrix is by enforcement layer, not by vendor. There are three layers, server-side constrained decoding, server-side bias plus validation, and client-side validate and retry, and each layer has a different failure profile. Once you know the layer, the vendor choice is mostly about which provider your stack is already on.

Server-side constrained decoding (OpenAI, Outlines)

This is the hardest guarantee available. The provider takes your JSON Schema, compiles it to a finite-state grammar over the tokenizer's vocabulary, and masks the sampler's logits at every decode step. Any next-token that would leave the grammar gets a logit of negative infinity. The output literally cannot leave the grammar.

OpenAI Structured Outputs in strict mode (response_format=json_schema) gives this guarantee for GPT-5.5 and the 4.1-class snapshots. The Pydantic to Schema compile happens in the SDK, the Schema to grammar compile happens on the server, and the grammar is cached by schema hash so subsequent calls do not pay the compile cost.

Outlines and lm format enforcer give the same guarantee for self-hosted models via vLLM, SGLang, or Transformers backends. SGLang 0.4's structured generation pipeline fuses grammar compilation into the prefill kernel, which removes the per-call compile overhead almost entirely.

The failure mode this layer cannot escape is forced fills. When the input does not contain enough information to satisfy a required field, the sampler picks something rather than fail. You get a well-shaped output with priority: 'medium' because the grammar required a priority value. The fix is to mark fields optional in the schema rather than hoping the model will refuse to fill them.

Server-side bias plus validation (Anthropic, Gemini)
Client-side validate and retry (Instructor, Vercel AI SDK)
Picking the right toolkit
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
ToolkitLayerEnforcementBest for
OpenAI Structured OutputsServerConstrained decoding (hard)Closed-provider strict extraction
Anthropic input_schemaServerBiased + validated (medium)Claude tool calls in agents
Gemini responseSchemaServerBiased + validated (medium)Gemini-native structured tasks
Instructor (Python)ClientValidate + retry (soft)Cross-provider Python agents
Vercel AI SDKClientValidate + retry (soft)Next.js / Node TypeScript apps
Outlines / SGLangServer (self-hosted)Token-level grammar (hard)Self-hosted open-weight models

Real products, models, and research that use this idea.

  • OpenAI Structured Outputs in strict mode ships with the GPT-5.5 responses API; the Python SDK's responses.parse method accepts a Pydantic model directly.
  • Anthropic Claude Opus 4.7 tool use enforces input_schema on every tool argument; the same mechanism powers Computer Use and the MCP tool layer.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow does Outlines compile a Pydantic model into a token-level mask, and why is that compile step expensive?
A

Pydantic compiles to JSON Schema, the JSON Schema compiles to a regex or context-free grammar, the grammar is converted to a deterministic finite automaton over the tokenizer's vocabulary. The DFA construction is O(|grammar| x |vocab|) and is the slow step; cache by schema hash.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Treating these six as the same feature with different vendor logos. They split into server-side constrained decoding, server-side bias, and client-side validate-retry, each fails differently.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Which three toolkits enforce on the server side

  • Which three toolkits enforce on the client side

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Pick the most effective intervention when an agent's context grows by 8KB every iteration
MCQ·Medium