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: 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
- Schema validation with JSON Schema validators or libraries such as Pydantic and Zod, even when the provider enforces the schema, to catch version mismatches.
- Business rules: totals, formats, allowed combinations and ranges.
- Cross-checks against systems of record, such as supplier master data or an order.
- Confidence handling: route records with missing or doubtful fields to a person.
| Measure | Purpose |
|---|
| Field-level accuracy against labeled documents | Shows which fields need work |
| Null accuracy | Does the model correctly return null when a value is absent? |
| Business-rule failure rate | Signals extraction or source-document problems |
| Share of records approved without edits | Practical 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.