Structured Output

Structured output is a model feature that makes a language model return data in an exact, machine-readable format, usually JSON matching a schema you supply, instead of free text your code must parse.

Also known as: Structured outputs, JSON mode, constrained decoding

Related terms in this glossary

How it works

You pass a JSON Schema describing the fields, types and required values you expect. The strongest implementations use constrained decoding: at each step the model is only allowed to produce tokens that keep the output valid against the schema, so malformed JSON cannot occur.

  • OpenAI introduced Structured Outputs with strict JSON Schema adherence in August 2024.
  • Anthropic's structured outputs offers a JSON output mode and strict tool use that guarantees tool arguments match their schema.
  • Google's Gemini API accepts a response schema.
  • Open-source libraries such as Outlines and XGrammar, and inference servers such as vLLM, provide the same for self-hosted models.

Older "JSON mode" features only ensure valid JSON, not that it matches your schema, which is a weaker guarantee.

Why it matters

Most business uses of AI feed results into other software: extracted invoice fields go into an ERP, a ticket category goes into a queue, an agent's arguments go into an API. Free text breaks those pipelines unpredictably. Structured output removes a whole class of parsing failures and retries. It guarantees shape, not truth: a field can be perfectly formatted and still wrong, so validate values against business rules and systems of record, and evaluate accuracy per field. Keep schemas simple, give fields clear names and descriptions, and allow null where information may be missing so the model is not pushed to invent it. See NLP and document AI.

Structured Output in depth

Designing schemas that models fill well

The schema is effectively part of the prompt. Schemas that work well in production share some habits:

  • Descriptive field names and descriptions, such as invoice_date with "Date printed on the invoice, not the due date, in YYYY-MM-DD".
  • Enums for fixed choices, like document type or ticket category, instead of free strings.
  • Nullable fields where information may be absent, so the model can say "not present" instead of inventing a value.
  • Flat structures where possible; deep nesting and very large schemas raise error rates and may hit provider limits on schema complexity.
  • Evidence fields where useful, such as the page or sentence a value came from, which helps reviewers.
  • Field order that follows reasoning, for example a short reason before a decision field.

Example: extracting a GST invoice

Example: an accounts team wants invoices turned into records for its accounting system. The schema has supplier name, supplier GSTIN, buyer GSTIN, invoice number, invoice date, place of supply, a list of line items with HSN code, quantity, rate and taxable value, and tax amounts split into CGST, SGST and IGST, each nullable. The model returns JSON that always matches the schema. Code then validates the values: the GSTIN pattern and check character, line items summing to the taxable total, CGST and SGST appearing together and never with IGST, and the date not being in the future. Records that pass go to a review queue for quick approval; records that fail show the reviewer which rule broke. The schema keeps the pipeline from crashing; the validation keeps wrong numbers out of the books.

Validation layers after generation

  1. Schema validation with JSON Schema validators or libraries such as Pydantic and Zod, even when the provider enforces the schema, to catch version mismatches.
  2. Business rules: totals, formats, allowed combinations and ranges.
  3. Cross-checks against systems of record, such as supplier master data or an order.
  4. Confidence handling: route records with missing or doubtful fields to a person.

Measuring extraction quality

MeasurePurpose
Field-level accuracy against labeled documentsShows which fields need work
Null accuracyDoes the model correctly return null when a value is absent?
Business-rule failure rateSignals extraction or source-document problems
Share of records approved without editsPractical measure of reviewer workload

Our document AI demo shows schema-based extraction on sample files.

Typical problems are required fields with no null option, which pushes the model to make up values, one giant schema covering every document type, trusting well-formed output without business validation, and changing a schema without re-running the evaluation set. Structured output is closely related to tool use, where tool arguments follow a schema, and to entity recognition; it does not prevent hallucination on its own. Gepton applies it in NLP and document AI, data extraction agents and document classification.

Questions about Structured Output

Something else? Send us your question and an engineer replies with next steps.

Is structured output slower or more costly than plain text?

Usually not meaningfully. Constrained decoding adds a little processing per token, and the first request with a new schema may take longer on some providers while the schema is prepared. JSON field names add some output tokens. In return, you avoid retries and repair steps for malformed output, which often saves more than it costs. Measure latency and tokens on your own workload if either is tight.

Can structured output be used with streaming responses?

Yes. Most providers stream structured output token by token, so you receive a growing JSON document. Partial JSON is not valid until it completes, so use a parser that tolerates incomplete input if you want to show fields as they arrive, or wait for the full object before acting on it. Never trigger actions, such as database writes, from partial output.

Articles that mention Structured Output

All articles

Jump to

↑↓ to move↵ to open