Skip to content
echogeco
The model as a component · Prompts and outputs

A schema for invoice line extraction

Lay out the output schema and the validation layer for invoice line extraction, deciding what the schema enforces, what code enforces, and what happens on failure.

designhardExercise · 4 min · 12 of 77
On this page

An extractor that returned valid JSON posted 120 wrong invoices in a month. The schema did what it promised; the design around it did not exist.

The situation

An accounts-payable team receives 2,000 supplier invoices a month as PDFs with the text already extracted, each with 1 to 40 line items. The first version asked the model to "return the line items as JSON" and posted the result straight to the ledger. In its first month, 6% of posted invoices — 120 — had line totals that did not sum to the invoice total, 2% carried a negative line, and 14 invoices were posted twice because the invoice number came back with a digit wrong. Reversing a bad posting takes an accountant about 20 minutes, so the month cost roughly (120 + 40 + 14) × 20 minutes ≈ 58 hours.

The ledger accepts three currencies and four tax rates. Suppliers occasionally send a credit note in the same inbox. The target is zero bad postings and a human review queue under three hours a month; a queued invoice with the failing check named takes about 3 minutes to review. The team is working from structured outputs and JSON Schema and has constrained decoding available for types, enums, required fields and additionalProperties.

The question

Design the output schema and the validation layer behind it: what the schema enforces, what code enforces, and what happens when a check fails.

A strong answer · Rubric · Where answers go wrong

Write your answer first, then open