Quant Memo
Core

Schema-Constrained JSON Output

How to make a large language model reliably return data in a fixed, parseable structure instead of free-form prose, so its output can be fed directly into code without a human reading it first.

Prerequisites: Structured Extraction From Filings With LLMs

A pipeline asks a large language model to extract a company's reported revenue and guidance from an earnings call transcript, intending to feed the result directly into a downstream model as a feature. The model replies with something like "The company reported revenue of $1.2 billion, which represents..." — a perfectly reasonable sentence for a human to read, and completely useless as input to code expecting a number in a specific field. If thousands of transcripts need this extraction run automatically every quarter, a human can't proofread every response, and free-form prose can't be reliably parsed by a downstream program without constant breakage.

The idea: constrain the shape of the answer, not just ask nicely

Schema-constrained output means specifying, in advance, the exact structure a response must follow — typically a JSON object with named fields and types, such as {"revenue_usd": number, "guidance_direction": "raised" | "maintained" | "lowered"} — and having the generation process enforce that structure rather than merely requesting it in the prompt. The distinction matters: simply asking a model to "please respond in JSON" is a request it can still ignore, add commentary around, or format inconsistently between calls. True schema constraint — increasingly supported directly by model providers — restricts what tokens the model can generate at each step, so the output is guaranteed valid against the schema, not just usually well-behaved.

This is the difference between a form with labeled boxes versus a blank sheet with instructions at the top. The form structurally can't be filled out wrong in a way that breaks a parser; the blank sheet can always come back organized just differently enough to break the code expecting a specific format.

Worked example: a downstream pipeline that would break without a schema

A pipeline processes 500 earnings-call transcripts each quarter, feeding an LLM's extracted guidance_direction field directly into a trading signal. Without schema enforcement, prompting alone ("respond with just: raised, maintained, or lowered") still allows drift: on transcript 214, the model might respond "The guidance appears to have been raised, though modestly" instead of the bare word raised. A parser expecting an exact match against three known strings throws an error, and the pipeline either crashes for that transcript or silently treats the response as missing — quietly dropping a real signal unnoticed until someone checks completeness.

With a schema constraining guidance_direction to strictly one of the three literal values, the model is structurally prevented from returning the extra caveat as part of that field — the enforced format guarantees exactly one of the three strings every time, zero parsing failures from drift. Genuine ambiguity still has to be handled, but it shows up as an explicit allowed value, like a fourth "unclear" category added to the schema — not an unparseable sentence breaking the pipeline.

Function explorer
-2222.8
x = 1.00f(x) = 0.000

Treat that curve as "parsing reliability" against "how tightly output is constrained": prompting alone sits low and plateaus, since natural-language drift keeps causing occasional failures no matter how careful the wording; true schema enforcement sits at the top, because the format simply cannot deviate.

What this means in practice

Schema constraints don't fix a model being wrong about the extracted facts — it can confidently report the wrong revenue figure inside a perfectly valid JSON object — but they eliminate a recurring category of pipeline failure: format drift breaking parsing. The schema design matters too: fields should include an explicit way to express "not stated" rather than forcing a guess just to satisfy a required field, since a forced guess inside valid JSON is a much harder failure to detect than a missing value.

Schema-constrained output enforces the structure of a model's response at generation time, guaranteeing a downstream parser never breaks on unexpected formatting — but it says nothing about factual correctness, which still needs separate validation against the source document.

Asking a model nicely to "respond in JSON" inside the prompt text is not the same as schema enforcement — prompt-only formatting requests can still drift, adding commentary or inconsistent structure across calls. Only a generation-time constraint, or a downstream validator that hard-rejects and retries malformed output, actually guarantees a parseable result every time.

Related concepts

Practice in interviews

Further reading

  • OpenAI documentation, Structured Outputs and JSON mode
  • Anthropic documentation, tool use and structured output
ShareTwitterLinkedIn