"About $1,240, due sometime in March" is not data
A small accounting tool reads supplier invoices with a model and imports the totals into a ledger. The model reads well, and it answered: "The total looks like about $1,240, due sometime in March."
That sentence is fine for a person and useless for code: the import script had to guess which number was the total and which month "sometime in March" meant. On 9 percent of 5,000 invoices a month, the guess was wrong or missing: 450 bad imports, a weekend of hand fixes, two suppliers paid late.
The model was not the problem. The interface was: prose went in where code needed fields.
A phone call is not a form
A person reading "due in March, roughly $1,240" fills the gaps from context. Code cannot: it needs to know which number is the total, what to do when a field is absent, and how to tell a complete answer from a confident-looking one.
Free text fails code in three quiet ways. First, shape drifts: one reply says "$1,240", the next "1240 dollars", the next buries the number in a paragraph. Second, missing looks like present: a sentence can sound complete while skipping a required field. Third, errors are silent. Prose has no pass or fail test, so wrong values surface far from their cause, usually when a supplier calls.
Think of it as a phone call versus a form. A call is rich and flexible; a form is dull and checkable. Write prose for people, forms for code.
The schema contract
A schema is a promise about shape: field names, types, required fields, allowed values. Four moves turn it into a pattern.
Define the shape from what your code needs, not from what the model likes to say. For an invoice: vendor as a string, total as a number that cannot be negative, currency as one of USD, EUR or GBP, due date as a proper date. Every field required, no extras allowed.
Constrain the model to the shape. Most providers offer a structured output or tool calling mode that forces the reply to fit; feature names differ, but the idea is the same: the model can only answer inside the boxes you drew.
Validate anyway. Run a validator on every reply: constraint lowers failures, it does not remove your duty to check. A total that is a number is not necessarily the right number. Range checks, enums, and cross-field checks earn their keep.
Reject or repair what fails. A failed reply is repaired once or routed to a person. It is never passed on and hoped for the best.
Keep schemas small: every extra field is one more chance to fail, and one more guess when the source is silent. If a field is often absent, allow null on purpose. A missing value that says so is honest; a guessed value that validates is worse, because nothing flags it.
Tool use is the same pattern with side effects
A tool call is a structured reply whose fields are arguments: a function name plus typed inputs. The model proposes a call like refund_payment(order_id, amount). That is a request, not an action.
Your code checks first: is this caller allowed to refund, is the amount within limits, does the order exist? Those checks live in code, not in the prompt, and only checked calls run. The result goes back to the model as data.
The model never touches your systems directly: no credentials, no way to act except by asking your code. Picture a junior assistant who fills in request slips but cannot sign them. The boundary is enforced by code, not by asking nicely.
Two warnings: a model will propose the same bad call again, so checks must hold every time, and a call can fail halfway, so state-changing calls need ids your code can deduplicate.
Confidence per field, not per document
When you extract from documents, score confidence per field, not per document. The vendor name may be crisp while the due date is a smudge, so set thresholds per field, with a high bar for money. A blended score hides the field you most need to check; per-field confidence routes only the smudged total to a person.
And null is an answer: if the document does not state a field, return null, not a guess. A guessed tax id that validates is worse than a missing one, because nothing flags it.
One repair retry, then a human
Validation errors are useful data. Feed the exact error back once: "total must be a number, you returned a string" lets the model fix its reply. Most fixable slips clear first try; later retries repeat the failure at extra cost.
After one failed repair, the item goes to a human queue with the document and error attached. Never loop forever: an unbounded loop turns one bad document into an open-ended bill, still with no record. Log every failure: a rising rate warns you that layouts or the model changed.
The numbers
Back to the accounting tool: 5,000 invoices a month, all figures illustrative. Before the contract, free text failed on 9 percent, 450 invoices fixed by hand. After the schema, validation, and one repair retry, failures fell to 0.8 percent, 40 invoices. Another 1.5 percent, 75 invoices, went to the human queue, read by a person, so handled, not failed. Clean imports: 4,885, or 97.7 percent. Manual work dropped from 450 to 115 invoices, about a quarter.
The model did not get smarter. The contract caught its slips, the retry fixed most of them, and the queue landed the rest safely.
When not to structure
Not everything belongs in a schema: brainstorming, where variety is the point; drafting prose a person reads and edits, like emails and summaries; any answer a human judges directly, with no code branching on its parts.
One test decides it: does code consume it, or a person? Structure for code. Write for people.
The full interactive chapter is free at https://deployedlab.dev/lessons/arch-06-structured-output-tool-use, with a hands-on lab where you debug a broken invoice schema. Deployed Lab has 79 chapters free with no signup, and the full bundle of 193 chapters is $5 a month or $39 a year at deployedlab.dev.
Top comments (0)