You can ask an LLM to return JSON. You can beg. You can add five paragraphs to your prompt explaining the exact schema. About one in seven times, it will still break.

The failure modes are specific. JSON wrapped in markdown code fences. A number field containing the string "42." A required field silently omitted. A trailing comma that makes the output unparseable. The whole response as prose with JSON buried mid-paragraph.

Each failure mode needs different error handling. Most teams end up with layered try/catch logic, a regex stripper for markdown fences, and a retry loop. The retry is the expensive part. When a 500ms LLM call fails and retries, you have just paid 1000ms plus. Your p95 latency climbs fast.

The fix is not better prompting. The fix is constrained decoding.

How Constrained Decoding Actually Works

LLMs generate text one token at a time. At each step, the model assigns probabilities across its entire vocabulary of 50,000-plus tokens. Normally it picks the highest-probability token. Constrained decoding changes this.

When you enable strict structured outputs, the provider compiles your JSON Schema into a finite state machine. The FSM maps every valid state of your output. Which fields have been opened. Which types are expected next. Where commas and brackets belong.

At each token generation step, any token that would violate the FSM path gets its probability set to zero before sampling. The model cannot emit an invalid token. Not because you asked nicely in the prompt, but because those tokens are mathematically excluded from the distribution.

This is fundamentally different from prompt engineering. A prompt like "respond only in JSON with this schema" influences the model. Constrained decoding enforces it. GPT-4o with Structured Outputs scores 100% on complex JSON schema adherence evaluations.

Modern implementations like NVIDIA Triton Inference Server can skip "boilerplate" tokens and only spend compute on the actual data. The performance hit is typically 5% to 8% on total inference time.

The Hidden Cost of Structure

You cannot get something for nothing. There is a fundamental tension between making the output look right and making the output be right.

Research by Schall and de Melo (RANLP 2025) tested 11 models under structural constraints. They found a divergence between base and instruction-tuned models. Base models often benefit from constrained decoding. Their outputs become more precise. Instruction-tuned models frequently suffer performance degradation on generation tasks despite maintaining stability on classification tasks.

The mechanism is clear from log probability analysis. Constrained decoding forces models away from their preferred natural language patterns into lower-confidence structured alternatives. Constrained tokens often carry a probability nearly five times lower than the model's natural choice. This introduces bias into the output distribution.

The model becomes so focused on closing a JSON bracket that it forgets the actual logic of the answer. Schall and de Melo call this "semantic degradation." The researchers also found that constrained models show steeper performance gains from additional few-shot examples compared to unconstrained generation. The prompt still matters. It just matters differently.

The size dependency is notable. Smaller models under 14B parameters benefit massively from constrained decoding, with some seeing a 41.2% improvement in structured reasoning tasks. For giant models, the bias can outweigh the benefits. A few-shot unconstrained prompt might actually be more accurate than a zero-shot constrained one for 70B-plus parameter models.

Draft-Conditioned Constrained Decoding

The most promising recent advance is Draft-Conditioned Constrained Decoding, or DCCD, proposed by Chapparapu et al. in February 2026.

The problem DCCD solves is specific. Standard constrained decoding can distort generation when the model assigns low probability mass to valid continuations. It pushes decoding toward locally valid yet semantically incorrect trajectories. The grammar is correct. The content is wrong.

DCCD decouples semantic planning from structural enforcement. It works in two steps. First, an unconstrained draft is generated. The model reasons freely in natural language. Second, constrained decoding is applied, conditioned on that draft, to guarantee validity.

The paper analyzes this through a KL-projection view. Draft conditioning increases feasible mass and reduces the cumulative "projection tax" induced by hard constraints. The draft gives the model a semantic roadmap. The constrained pass just stamps the structure on top.

The results are striking. DCCD improves strict structured accuracy by up to 24 percentage points over standard constrained decoding. On GSM8K with a 1B model, accuracy jumps from 15.2% to 39.0%. Smaller model pairs can match or exceed much larger constrained baselines. That is a substantial gain in parameter efficiency.

An optional best-of-K draft selection further improves results by generating multiple drafts and picking the best one before applying constraints.

Thinking Before Constraining

A complementary approach from Zumot et al. (January 2026) takes a similar philosophical stance. Their paper "Thinking Before Constraining" proposes allowing LLMs to reason freely until specific trigger tokens are generated, then switching to structured generation.

The idea is simple. Let the model think in natural language. When it reaches a point where structure matters, switch on the constraints. The method preserves expressive reasoning while ensuring reliable structured outputs.

Their results show up to 27% accuracy gain over pure natural generation, with only 10 to 20 extra tokens of overhead. That is a small price for a large improvement.

Provider Support in 2026

All three major providers now support constrained decoding as of early 2026.

OpenAI offers the .parse() method with Pydantic models. It accepts a Pydantic class directly and returns a typed response. Strict mode enforces your schema via FSM. The limits are 100 object properties total and 5 levels of nesting. additionalProperties must be false.

Anthropic has two modes. JSON Outputs Mode uses the output_format parameter with a JSON Schema. Strict Tool Use Mode uses strict: true on tool definitions. Tool use mode adds 313 to 346 tokens of system prompt overhead per request. JSON Outputs Mode avoids that. The first request with a new schema incurs 100 to 300ms compilation overhead. The compiled grammar is cached for 24 hours.

Gemini uses the responseschema parameter alongside responsemimetype: "application/json". There is a known property ordering issue. Without explicitly setting propertyordering, field order can differ from schema definition. This does not break parsing but confuses downstream code.

Open-source options include vLLM, Text Generation Inference, and the Outlines library for defining constraints directly in Python code.

When to Use It and When Not To

Use constrained decoding when the cost of a syntax error exceeds the cost of slight semantic degradation. API responses, data extraction, form filling, and financial transactions all qualify.

Avoid it for creative writing, open-ended brainstorming, or any task where linguistic fluency matters more than structural precision. The restrictions that make it great for data extraction make it terrible for storytelling.

For smaller models, constrained decoding is almost always a net win. The structural guarantees improve both format compliance and reasoning accuracy. For larger models, test both approaches. A well-crafted few-shot unconstrained prompt might outperform a zero-shot constrained one.

Add few-shot examples when using constraints. Schall and de Melo found that constrained models show steeper gains from demonstrations than unconstrained models. The examples give the model a semantic guide to follow while the FSM handles the syntax.

The bottom line: constrained decoding is no longer experimental. It is a production technique with strong theoretical foundations, broad provider support, and measurable benefits. If you are still relying on prompt engineering alone for structured output, you are accepting a 15% failure rate you no longer need.