Hot AI News

Structured outputs: How constrained decoding forces a model to answer in valid JSON

Structured outputs keep a language model's answer inside a schema you define. Constrained decoding blocks invalid tokens as the text is generated. Here is how it works and what it cannot fix.

Structured outputs: How constrained decoding forces a model to answer in valid JSON
Photo illustration: Aiholics

Structured outputs are a way to make a language model answer in a fixed format, such as JSON that matches a schema you wrote. The most reliable version works while the text is being generated: the system blocks any next piece of text that would break the format. This technique is called constrained decoding.

Why a program cannot trust a chatty model

Imagine an app that reads customer emails and fills a table with the sender, the date and the product mentioned. The app needs the model’s answer as clean data. If the model writes “Sure! Here is the JSON you asked for:” before the data, or forgets a closing bracket, the program crashes.

A model writes one token at a time. A token is a small chunk of text, often a word or part of a word. Nothing in ordinary generation forbids a stray comma or a missing quote. Prompts such as “reply only with valid JSON” help, but they are requests, not rules.

Structured outputs turn the request into a rule. The developer supplies a schema, which is a description of the allowed shape: which fields exist, which are text and which are numbers, and which values are permitted.

Advertisements

Crossing out the wrong words before the model picks

A monitor shows crossed-out gray candidate tiles and blue available choices connected to an output sequence by a highlighted violet path.
Photo illustration: Aiholics

At each step the model scores every token in its vocabulary, then one is picked by sampling. Constrained decoding adds a filter between those two steps. A checker looks at the text so far and works out which tokens could still lead to a valid answer. Every other token has its score removed, which is called masking.

Think of a form with boxes. After the model has written the opening brace and a field name, the checker knows a colon must come next. Letters, digits and other symbols are crossed out, so the model can only choose the colon. Where several choices are valid, such as the digits of a number, the model still decides freely among them.

OpenAI described this in its August 6, 2024 announcement of Structured Outputs. It said the valid tokens must be recomputed after each token is generated, not once at the start. It also said masking effectively lowers the probability of invalid tokens to 0.

From a schema to a grammar

The checker needs a precise definition of what is valid. In OpenAI’s description, the JSON Schema is converted into a context-free grammar (CFG), a set of rewriting rules that defines a language. Grammars can describe nested and recursive structures, such as a menu that contains sub-menus that contain more sub-menus. OpenAI said simpler finite-state approaches cannot generally express recursive types.

Building this grammar takes work. OpenAI said the first request with a new schema pays a latency penalty. Typical schemas take under 10 seconds on the first request, and complex ones may take up to a minute, according to the company. Later requests with the same schema reuse cached results.

Where the idea came from, and what it costs

Forcing a language model to follow a grammar predates the commercial feature. In 2023, Saibo Geng, Martin Josifoski, Maxime Peyrard and Robert West published “Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning,” accepted at EMNLP 2023. They showed that grammar constraints work without extra training. On tasks such as information extraction, entity disambiguation and constituency parsing (analyzing sentence structure), they reported that constrained models substantially outperformed unconstrained ones and could beat task-specific fine-tuned models.

A later paper raised a caution. In “Grammar-Aligned Decoding” (NeurIPS 2024), Kanghee Park and colleagues argued that standard grammar-constrained decoding can distort the model’s own probabilities. The outputs are valid, but they are not sampled in proportion to what the model believed, which can lower quality. They proposed a method called ASAp to reduce this effect. Their results come from code generation and structured language tasks, not from the commercial APIs.

Constraints also do not make the content true. OpenAI’s own page notes that structured outputs do not stop mistakes inside values, such as a wrong step in a math answer. A perfectly shaped reply can still hold a wrong date.

Advertisements

Where you meet it today

OpenAI reported that its gpt-4o-2024-08-06 model with Structured Outputs scored a perfect 100% on its evaluations of complex JSON schema following, while gpt-4-0613 scored under 40%. It also said a model trained for the task without constraints reached 93%, which it called not reliable enough for developers. These are the company’s own figures. Developers turn the feature on with response_format set to a json_schema type, or with strict set to true in a function definition.

The feature is now common across providers. Mistral’s documentation distinguishes a plain JSON mode, which guarantees valid JSON but not a particular structure, from custom structured outputs that follow a supplied schema. Structured outputs are also on the feature list for the model in our report on Mistral hosting Z.ai’s GLM 5.3 Flash, next to function calling. That connection is no accident: tool calls are structured data, so agents rely on it to send arguments to software.

Limits to know and what to watch

OpenAI listed several limits for its version:

  • Only a subset of JSON Schema is supported.
  • The answer can fail to match if the model refuses, or if generation stops early at the token limit.
  • It is not compatible with parallel function calls.
  • Schemas supplied this way are not eligible for Zero Data Retention, according to the page.

These details are specific to one vendor and may change, so check each provider’s current documentation. Other hosts may implement the masking differently or support other schema keywords.

Three questions are worth watching. One is speed: newer research aims to cut the cost of building the token masks for large grammars. Another is quality, meaning whether methods like grammar-aligned decoding reach production. The third is how wide the constraints can go, from JSON to full programming languages, where a model could be barred from writing code that fails to parse.

For a developer the practical rule is simple. Use structured outputs when software reads the answer, keep schemas small, and still validate the values that matter.

Sign Up for the Daily AI Pulse

One email a day. All the stories that matter.

Daniel ReedAI Research, Safety & Ethics Analyst

Daniel Reed currently works as an AI Research, Safety & Ethics Analyst at Aiholics, writing about how changes in artificial intelligence are affecting and will affect scholarship,…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like