Getting clean JSON out of a language model is harder than it looks, and a new benchmark of 150 live calls shows where the problems sit. The test compared five setups for structured outputs on a release-note classifier and found that valid JSON is only half the job.
Prompt-only JSON failed completely. Strict schemas fixed the syntax but still shipped wrong labels until the prompt got a clear policy and a few well-chosen examples.
Four ways to ask for JSON
Developers often treat the options as one thing, but they enforce different guarantees. Prompt-only JSON describes the shape in plain language and hopes the model complies. JSON mode guarantees parseable output but not your fields. Schema-constrained structured outputs compile a JSON Schema into a grammar, so the model cannot emit a token that breaks it. Strict tool calling uses the same engine for a tool’s arguments and suits cases where the model must pick between several actions.
In the benchmark, prompt-only JSON on gpt-4.1-mini scored 0% on raw JSON.parse(). All 30 responses came wrapped in Markdown code fences. Stripping the fences hides a worse risk: one response for a change to a cookie’s SameSite default came back parseable but classified as a feature when it was a breaking change.

How the grammar constraint works
At every step the model scores each token in its vocabulary. With a strict schema, the server compiles the schema into a state machine or grammar, then sets the score of every illegal token to negative infinity before sampling. Only schema-valid tokens survive.
Two practical consequences follow. The first request with a new schema pays a compilation cost, and providers cache the result by schema hash. If your code injects timestamps or user IDs into the schema on each call, the cache never hits. Keep schemas static and put dynamic options in the prompt.
The mask also can’t undo a choice. Generation runs left to right, so once the model writes a category, every later field is conditioned on it.

Schema design changes accuracy
Field order matters because the model fills fields in the order the schema lists them. In the test, a strict schema with the verdict first and no rationale matched the ground truth on 26 of 30 entries, or 86.7%. Moving to a clear policy with a short rationale field first reached 30 of 30.
Nullable fields matter just as much. Ten release notes contained no issue number. When the schema required a string, the model returned an empty string on all 10. With a nullable type, it returned null on all 10. An empty string can quietly break a database key or a URL template downstream.
Other findings from the same test:
- Strict mode accepts only a subset of JSON Schema, so keep cross-field rules in a validator such as Zod’s superRefine().
- Wide enums with near-synonyms split probability between similar tokens, so use a few distinct labels and separate booleans.
- Add an “other” or “unknown” option when inputs can fall outside your classes.
Valid does not mean correct
The strict, zero-shot setup produced 100% valid JSON and 100% schema compliance. It still got 4 of 30 entries wrong. Two soft deprecation warnings were marked as needing migration, and two test or CI-only changes were labeled fixes instead of internal work.
Neither mistake was careless. Both followed a reasonable reading of the text that clashed with the team’s policy. No schema tweak can settle a question like whether a future-removal warning counts as a migration today.
Few-shot examples at the boundaries
Easy examples such as “added dark mode” waste tokens, because modern models already get those right. The useful ones sit where two categories nearly touch: a soft deprecation, a devDependency bump phrased like a bug fix, a silent default change, and a backwards-compatible security patch.
Pass them as real user and assistant message turns that match the live schema, not as text inside the system prompt. Update every example when the schema gains a field.
Adding four boundary examples also sped things up. Compared with the zero-shot setup with a rationale, input grew from 255 to 460 tokens, yet median latency fell from 938ms to 827ms. Average output dropped from 60 tokens to 38, a 37% cut, because the examples showed the model how short a rationale should be. Both setups scored 30 of 30.
Output tokens cost more and are generated one at a time. Input tokens are processed in parallel and can be cached when the prefix stays fixed.
Handling failures in production
A strict call still has several non-happy outcomes, and the benchmark’s example caller keeps them apart:
- HTTP errors such as rate limits, which are retried with backoff.
- Model refusals, which skip the schema and return an explanation instead of content.
- Truncation when max_tokens runs out, which leaves partial JSON.
- Empty content, which can happen when hidden reasoning tokens use the whole budget.
- Validation failures after parsing.
Tagging each outcome separately makes monitoring useful. A spike in truncation means raising the token limit. A spike in empty content on a reasoning model points to the budget.
What to do with this
For a narrow classifier, three to five static boundary examples are usually enough, and they keep the cached prefix intact. Retrieval-selected examples make sense once a taxonomy grows past about 30 categories, though each request then changes the prompt and loses caching.
The working recipe from the test is short: a strict schema, a rationale field first, nullable optional fields, a written policy, a few boundary examples, and a local validator behind the API’s own checks.
Source: flaviocopes.com