Data as of Aug 25, 2026 · Based on 269 AI responses from ChatGPT Search and Google AI Mode · See how Parse measures this
To ensure models follow complex JSON schemas, move beyond standard prompting to constrained decoding technologies. If you use API models, leverage their native structured output features, which provide high reliability. If you run local models, libraries that manipulate token generation—such as XGrammar, SGLang, or
Outlines—are best for enforcing strict, error-free formatting during the decoding process.
Brands AI recommends here
Provides native structured output support via specific parameters that enforce JSON schema compliance directly on the model, ensuring reliable results without external validation logic.
A high-performance library for local models that uses token masking to guarantee structured output with near-zero latency, making it ideal for high-throughput applications.
Yes. The category you want is structured outputs / constrained decoding. The important distinction is that these tools constrain generation during decoding, rather than merely asking the model to produce JSON and validating it afterward.
strict: true tool schemas. If you want the decoder itself under your control, these are particularly relevant:
| Situation | Best starting point |
|---|---|
| Just want reliable JSON from an API | OpenAI Structured Outputs |
| Want Claude | Anthropic Structured Outputs |
| Want Gemini | Gemini Structured Outputs |
| Self-hosting models | vLLM + XGrammar |
| Python experimentation | Outlines |
| Existing Transformers/vLLM stack | LM Format Enforcer |
| Arbitrary grammars / sophisticated constraints | LLGuidance |
The key architectural change is: don't prompt your way out of a schema problem. Give the schema to the decoder. Prompting can still explain what the fields mean, but the constraint engine should enforce what can physically be emitted. That's precisely the distinction OpenAI describes between ordinary prompting/JSON mode and constrained decoding.
One caveat: schema validity isn't semantic correctness. Constrained decoding can force "age": 42 to be an integer and prevent undeclared fields, but it can't necessarily tell whether 42 is the right age.
If you're building something where the schemas are really complex (nested unions, recursive objects, conditionals, arrays, enums, etc.), I'd lean toward native Structured Outputs for hosted models or vLLM + XGrammar for self-hosting, rather than trying to solve it with increasingly elaborate prompts.
Struggling with prompt-only JSON is completely normal—relying on "parse-and-pray" or basic response_format: { type: "json_object" } often leads to missing fields, wrong types, or broken syntax as schemas get complex.
True schema-constrained generation (using constrained decoding/logits masking via a Finite State Machine at inference time) makes syntax errors and wrong types literally impossible by construction.
Major commercial API providers and open-source inference/orchestration libraries offer native, robust constrained decoding tools:
These providers intercept token generation at the API layer, masking logits so the model can only output tokens that strictly align with your provided JSON Schema.
strict: true parameter alongside response_format: { type: "json_schema", ... } to guarantee 100% adherence to complex schemas.response_schema configuration field.If you are running open-weight models (like Llama 3 or Mistral) locally or on your own infrastructure, these inference backends enforce grammars or JSON schemas directly in the sampling loop:
format parameter set to a JSON schema object for local enforcement.If you want provider independence or need to wrap multiple models with a unified schema-enforcement layer:
llama-cpp-python or other pipelines to force JSON schemas or regex patterns.If you share whether you are deploying locally (open-source weights) or using a cloud API (like OpenAI/Anthropic) , I can provide a quick code snippet showing how to implement the schema constraint for your setup.
Constrained decoding (often called grammar-constrained generation or token masking) intercepts the model's logits at every generation step and masks out any token that violates your specified JSON schema . This makes malformed output mathematically impossible rather than just unlikely.
Major cloud model providers, open-source inference engines, and open-source orchestration libraries offer native tools and parameters for this capability.
response_format with type: "json_schema" and strict: true on supported GPT-4o and GPT-4o-mini models. When enabled, it guarantees that the output matches your exact JSON schema.output_config / output formats) alongside strict tool-use guarantees (strict: true on tool definitions) to force Claude to adhere strictly to defined schemas.response_mime_type: "application/json" paired with a response_schema field in the generation configuration using Pydantic or TypedDict -derived schemas.If you are running open-weights models (like Llama 3, Mistral, or Qwen) locally or on your own infrastructure, specialized inference engines perform real-time constrained decoding natively:
guided_json and guided_grammar which hook into fast backend libraries like Outlines, llguidance, or XGrammar to constrain decoding.format="json" ) to restrict token selection to valid syntax paths.guided_json to pass a JSON schema directly into the inference server.If you want to abstract away the differences between individual provider APIs or need automatic schema generation from code:
Are you deploying your solution via a managed API (like OpenAI/Anthropic) or self-hosting open-weights models (like Llama 3 on vLLM) ? Let me know so I can share a code snippet tailored to your exact tech stack.
Yes. What you’re looking for is generally called structured outputs, grammar-constrained generation, or constrained decoding. The important distinction is that these systems constrain the decoder itself rather than merely telling the model “please output JSON.”
If you want actual control over the decoding layer, these are worth investigating:
A useful architecture is:
JSON Schema → grammar/constraint compiler → allowed-token mask → model decoder
rather than:
JSON Schema → prompt → model → hope → JSON parser → retry
That distinction is exactly why constrained decoding is much more reliable than prompt engineering. OpenAI, for example, describes dynamically restricting the next-token vocabulary based on the schema during generation.
“JSON mode” isn't necessarily constrained schema decoding. A mode that guarantees valid JSON can still return the wrong shape. What you want is schema-constrained/strict structured output.
If you're choosing a stack today, I'd shortlist OpenAI Structured Outputs for hosted inference, or vLLM + XGrammar/LM Format Enforcer if you're self-hosting open-weight models.
Yes. If your goal is “stop asking the model to obey my JSON schema and instead make invalid tokens impossible,” there are several good options.
strict: true / JSON Schema, OpenAI uses constrained decoding: the inference system dynamically masks tokens that cannot lead to a schema-valid output. OpenAI says its implementation converts the schema into a context-free grammar and applies token-level constraints during generation. This is where the ecosystem gets particularly interesting:
There are really three levels of “JSON mode”:
| Approach | What is actually enforced? |
|---|---|
| Prompt: “Return JSON” | Nothing |
| JSON mode | Usually syntactically valid JSON |
| Constrained decoding / Structured Outputs | The generated token sequence is constrained to the schema |
That last category is what you're looking for. OpenAI explicitly distinguishes its older JSON mode from Structured Outputs: JSON mode guarantees JSON syntax but doesn't guarantee conformity to your particular schema.
One caveat: schema validity isn't semantic correctness. A constrained decoder can force {"age": 42} to match your schema, but it can't necessarily force 42 to be the correct age. Google explicitly recommends validating semantic values in your application as well.
My shortlist: if you want a hosted API, start with OpenAI Structured Outputs or Gemini Structured Outputs. If you want to run open models yourself, look at vLLM + Outlines/LM Format Enforcer. Those are fundamentally different from trying to improve your prompt until the model finally behaves.
Struggling with prompt-based formatting (the classic "parse-and-pray" method) is a universal pain point . Fortunately, modern infrastructure has shifted toward constrained decoding (also called structured generation), where a finite state machine (FSM) or context-based grammar actively masks invalid token probabilities at inference time. It literally makes syntax errors impossible by construction.
The landscape of providers and tools offering native or programmatic constrained decoding breaks down across three layers:
Major commercial model providers handle the FSM constraint directly in their inference engines when you pass a schema parameter:
response_format with json_schema set to strict: true , guaranteeing 100% adherence to your specified layout on supported models.response_schema parameters inside their generation configs.If you are self-hosting or running open-weight models locally (like Llama, Mistral, or Qwen), the inference serving layer handles token masking:
If you want clean abstractions in code—translating Python objects to schemas and managing API differences or local backends—these tools are the industry standard:
If you share what environment or model you are using (e.g., local open-weight via Ollama vs. a cloud API like OpenAI/Anthropic) and your preferred language/framework , I can provide a specific code snippet using the right tool for your setup.
Yes. The key term you’re looking for is constrained decoding (also called grammar-constrained generation or structured decoding). Instead of merely telling the model “please output this JSON,” these systems restrict the tokens the decoder is allowed to emit.
| Provider/tool | What it does | Best for |
|---|---|---|
| OpenAI Structured Outputs | Accepts JSON Schema and uses constrained decoding server-side | Easiest managed API |
| Google Gemini Structured Outputs | Generates JSON conforming to a supplied schema | Managed Gemini applications |
| vLLM Structured Outputs | Constrained decoding for self-hosted/open-weight models; supports JSON, regex, CFG, etc. | Serving your own models |
| Outlines | Grammar/JSON-schema constrained generation | Python/local models |
| XGrammar | High-performance grammar-constrained decoding | High-throughput inference |
| Guidance / llguidance | Programmatic grammars and constrained generation | Complex generation logic |
| llama.cpp grammars | Grammar-constrained local inference | Lightweight local deployment |
OpenAI is a particularly direct answer to your problem: its Structured Outputs implementation converts the supplied JSON Schema into a grammar and dynamically masks invalid tokens during generation. OpenAI reports 100% schema adherence on its cited complex-schema evaluation, while noting that semantic mistakes inside otherwise-valid values can still occur.
For open-source/self-hosted models, vLLM + XGrammar is probably the combination I'd investigate first. Current vLLM exposes structured JSON, regex, choice, and context-free-grammar constraints and supports XGrammar and Guidance backends.
There are three different levels of “structured output”:
Prompting: “Return exactly this JSON.”
→ No hard guarantee.
JSON mode: “Return valid JSON.”
→ Prevents malformed JSON, but may not satisfy your particular schema.
Constrained decoding: JSON Schema/grammar actually restricts the decoder.
→ Invalid structures are effectively removed from the generation space.
For your use case, #3 is what you want. Google explicitly recommends its Structured Outputs feature rather than prompting when the schema becomes complex.
If you tell me whether you're using OpenAI, Anthropic, Gemini, open-source models, or a framework like LangChain/Pydantic, I can narrow this down to the best 2–3 options and show how they handle a genuinely nasty nested JSON Schema.
Constrained decoding (forcing token-level masking so that an LLM physically cannot output invalid JSON or violate a schema) is natively supported by major API providers and open-source inference engines . You no longer have to rely purely on "parse-and-pray" prompt engineering.
Major API Providers (Cloud)
response_format (using type: "json_schema" with strict: true ) or via strict tool definitions. It guarantees 100% structural adherence to your JSON schema at the token level.strict: true ), ensuring schema compliance at the sampling level.response_schema parameter in its configuration, forcing compliance with your defined layout.Open-Source & Local Inference Engines
If you are self-hosting open-weight models (like Llama or Mistral), inference frameworks build grammar-constrained decoding directly into the runtime layer:
Python & Middleware Frameworks
If you want abstracted tools or client-side wrappers that interface cleanly with models or handle Pydantic validation:
If you can share which model or provider you prefer to use (e.g., OpenAI, local Llama via vLLM, or Anthropic), I can provide an exact code snippet showing how to implement the constrained schema.
Yes. What you’re looking for is generally called constrained decoding or structured outputs: instead of merely telling the model “please output this JSON,” the inference layer restricts the tokens it is allowed to emit so that the result stays within the schema.
| Provider / tool | How it enforces structure | Best for |
|---|---|---|
| OpenAI Structured Outputs | Native constrained decoding against JSON Schema | Easiest managed API |
| Anthropic Structured Outputs | JSON-schema structured outputs + strict tool use | Claude-based applications |
| Google Gemini Structured Outputs | JSON Schema response format | Gemini applications |
| vLLM | Guided decoding via XGrammar, Outlines, or lm-format-enforcer | Self-hosted/open models |
| Outlines | Grammar/JSON-schema constrained generation | Python + local/open models |
| XGrammar | High-performance grammar-constrained decoding | Production inference |
| llama.cpp | Grammar-constrained generation | Running models locally |
OpenAI explicitly describes its implementation as dynamically constraining the valid token set at each generation step, rather than relying solely on the model following instructions.
For self-hosting, vLLM is particularly interesting because it exposes structured generation while letting you choose among Outlines, XGrammar, and lm-format-enforcer as the underlying guided-decoding backend. It supports JSON Schema, regex, choices, and context-free grammars.
Anthropic now likewise offers native structured outputs: JSON responses can be constrained to a schema, while strict: true constrains tool names and inputs. platform.claude.com Gemini's API supports a JSON-Schema-based structured-output mode as well, although its supported schema is a subset of full JSON Schema.
If you're building an application rather than running inference yourself:
If you're running open models yourself:
vLLM + XGrammar is where I'd start. You get actual decoder-level enforcement without having to build the machinery yourself.
One important caveat: schema compliance isn't semantic correctness. A constrained decoder can guarantee that age is an integer and that required fields exist, but it can't guarantee that the model chose the right age. Recent research specifically finds that schema-valid output can still contain substantial semantic errors.
If you tell me whether you're using OpenAI/Anthropic APIs, open-source models, or a framework like LangChain/LiteLLM, I can give you a shortlist of the best constrained-decoding tools for that stack.
Struggling with prompt-based formatting (hoping the model doesn't add conversational filler or mess up brackets) is a classic bottleneck . Moving to constrained decoding —where the inference engine masks invalid tokens at the vocabulary level so breaking the schema is physically impossible—completely changes reliability.
The primary providers and open-source ecosystems offering tools that force token-level constrained decoding for JSON schemas include:
strict: true in their chat and responses endpoints. It compiles your JSON Schema or Pydantic model into a token-level constraint, delivering near 100% compliance.response_schema parameter in its configuration, forcing the output to conform directly to the defined schema.If you are running open-weight models locally or on your own infrastructure (like Llama 3, Mistral, or Qwen), inference engines now bake constrained decoding directly into the runtime:
guided_json (which accepts a JSON schema) and guided_grammar (using EBNF/GBNF) to mask invalid tokens on the fly.If you want abstraction layers that handle schema compilation (often via Pydantic ) and apply constrained decoding or smart retry loops:
.with_structured_output() abstractions that interface with native provider constraints or fallback parsing depending on the underlying model.Are you deploying your pipeline using commercial APIs (like OpenAI/Anthropic) or self-hosted open-weight models (like vLLM/llama.cpp) ? If you share your preference, I can provide a code snippet using the right tool for your setup.