When teams integrate large language models (LLMs) into invoice extraction, delivery-note parsing, or purchase-order intake for accounting and ERP platforms, the primary failure mode is rarely reading comprehension. Systems break at the boundary between probabilistic token generation and the rigid type contracts expected by relational SQL tables and ledger APIs.
Prompting a model with instructions to "output valid JSON only" eventually fails in production. Models wrap payloads in Markdown fences, drift from tax_amount to vat_total, inject formatted currency strings such as "$1,500.00" into decimal columns, or omit line-item properties on long multi-page invoices. Connecting AI extraction directly to enterprise systems requires shifting from verbal instructions to constrained decoding at generation time, backed by deterministic validation before posting to a database.
According to OpenAI's official Structured Outputs documentation, traditional JSON Mode (json_object) only guarantees that the model emits syntactically valid JSON, without enforcing adherence to a specific object structure. Structured Outputs goes further by compiling a supplied JSON Schema into a context-free grammar used during constrained decoding. At each generation step, the sampler restricts candidate tokens strictly to those permitted by the active schema state.
| Engineering dimension | Prompted JSON only | JSON Mode (json_object) | Structured Outputs (strict: true) |
|---|
| Valid JSON syntax | Fragile, often padded with prose | Guaranteed valid JSON | Guaranteed valid JSON |
| Key names and data types | Prone to drift and type mismatches | Missing keys or wrong types still occur | Enforced 100% against the compiled schema |
| Extra unrequested keys | Unbounded | Model may invent extra fields | Blocked via additionalProperties: false |
Controlled vocabulary (enum) | May produce off-list labels | Still risks typos or formatting variance | Constrained strictly to declared enum values |
| ERP ingestion reliability | Low, requires brittle regex fallbacks | Moderate, still needs structural repair | |
In Anthropic's engineering guide "Building effective agents," the authors similarly emphasize that the interface between an LLM and deterministic software tools (the Agent-Computer Interface) deserves the same rigor as system prompts. Keeping output formats intuitive and avoiding unnatural serialization burdens, such as forcing models to pre-compute line counts or escape deeply nested strings inside JSON, measurably improves extraction reliability.
Designing strict JSON Schemas for accounting and ERP records
To pass strict: true schema compilation without triggering a 400 Bad Request error and to keep extraction accurate on messy financial documents, engineers must follow strict schema design conventions:
- Set `additionalProperties: false` on every object: Every
object definition in the schema, including nested line-item objects inside an array, must explicitly declare "additionalProperties": false. This blocks the model from appending unrequested commentary keys like "notes" or "confidence" that break strict backend deserializers. - List every property inside `required`: Strict mode does not support omitting keys from the
required array to mark them optional. Every key declared in properties must appear in required. - Model optional fields via `anyOf` with `null`: Real-world receipts often lack a buyer tax ID or purchase-order reference. If a field is typed strictly as
"string", a constrained model is forced to fill the slot and will often fabricate "N/A" or "None". Instead, define the property using anyOf: [{ "type": "string" }, { "type": "null" }] with a clear field description instructing the model to emit null when the value is absent from the source document.
Structured Outputs solves structural conformance, but it never replaces accounting controls. Before writing a goods receipt or accounts-payable draft into an ERP, extracted payloads should pass four deterministic gates:
- 01Application-side schema verification (Zod or Pydantic): Re-validate every payload locally after the API call returns. This catches schema version drift between services and handles explicit
refusal payloads when an uploaded scan is illegible or truncated. - 02Deterministic arithmetic and tax reconciliation: Run pure application code to verify that
sum(line_items.line_total) equals subtotal, and subtotal + tax_amount matches grand_total within an explicit rounding tolerance. When arithmetic fails, flag the record for human review instead of asking the LLM to guess a second time. - 03Master-data resolution against ERP tables: Syntactically valid text can still fail business rules if a vendor name or item label does not match an active entity. Resolve supplier tax IDs, internal SKU codes, and general-ledger account mappings against live ERP tables before staging a draft entry.
- 04Idempotency keys and exception queues: Attach a cryptographic hash of the source document as an idempotency key so network retries never post duplicate invoices. Route records that pass 100% of math and master-data checks into a fast-approval draft queue, while sending mismatched documents to an exception queue for accounting staff.
Scope, caveats, and honest limitations
The practices outlined in this guide synthesize official OpenAI Structured Outputs documentation, Anthropic's "Building effective agents" engineering principles, and ToanKhonTech's experience integrating LLM extraction pipelines into business software. They serve as architectural guidance rather than a fixed accuracy benchmark across specific document templates or handwritten forms.
Engineering teams must distinguish clearly between structural validity and factual accuracy. Structured Outputs guarantees that a response conforms to your JSON Schema types, keys, and enum constraints, not that the extracted numbers reflect reality. A model can still misread a blurred digit on a low-DPI scan, confuse unit packaging (cases vs single units), or misclassify a tax exemption when document context is ambiguous. Any production pipeline linking LLMs to accounting or ERP ledgers must preserve deterministic cross-checks and human sign-off before final posting.