Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Extracting structured data from documents: OCR, schemas & confidence

How a photo or scan of an invoice becomes fields you can pay against: OCR with layout, a fixed schema, every value traced to its region on the page, confidence and validation rules as separate checks, and review of only the doubtful fields.

Forty-eight bags of cement, as pixels

At 07:50 a site foreman in Utrecht photographs a supplier's invoice on the dashboard of his van: 48 bags of cement, a VAT line, a total, and the merchant's bank details in small print. The photo goes to Lintel, a payables platform that some 400 building subcontractors use to pay their suppliers. About 2,500 documents arrive on a normal day, by app or forwarded email, and about 9,000 a day at month end. A few are PDFs a merchant's system generated. Most are photos: creased, badly lit, a coffee ring over a corner.

The accounting system downstream wants one row per invoice with eight fields: supplier, supplier VAT number, invoice number, invoice date, net amount, VAT, total and the IBAN to pay. Getting from the photo to that row is document extraction. Reading the text is the easy part. The hard part is knowing which values can be paid against without a person looking, and showing that person exactly what to check for the rest.

Text, and where it sits on the page

A PDF generated by software usually carries its text already. A photo or a scan needs OCR (optical character recognition), which finds the characters in the image. For extraction the words alone are not enough: 1.284,00 means nothing until you know it sits on the line labelled Totaal and not in the unit-price column. So the OCR step also returns layout: each word and line with its page and its bounding box, the rectangle around it in page coordinates, and often structure built from them, such as tables and label-value pairs.

OCR takes seconds per page and month end brings several times a normal day's volume, so the upload only stores the original and queues a job; workers and their retries are Message queues & async workers.

Eight fields, in the same shape every time

First the worker decides what kind of document this is, because an invoice, a credit note and a delivery note carry different fields, and a credit note's total reduces what the subcontractor owes. Each type has a fixed schema: the field names, their types (a date, an amount in cents, an IBAN string) and which are required.

A document AI service with a prebuilt invoice model fills such fields itself, each with a page and a box, and most with a score. An LLM given the OCR text and the schema can do the same, and with structured outputs a provider can constrain a completed answer to match a JSON Schema. That settles the shape, not the content: the model can still put the VAT amount in the total field, or write a plausible IBAN that appears nowhere on the page.

So Lintel sends the model the OCR lines numbered, and the schema asks, for each field, for the value and the numbers of the lines it was read from. Code checks that the value really appears in those lines, which catches an invented IBAN, and copies their page and bounding boxes onto the field, so every value that passes points to a region of the original. A required field with no value, or with a value not found in its lines, is flagged for review.

Two questions about each value

The first signal is confidence: a score the OCR engine or the document model attaches to what it extracted, higher when it is more certain. Not every field gets one (Azure documents that not all of its fields do), and a number an LLM writes about its own certainty is something else: in one study (Xiong and others, 2023), models that stated their confidence in words tended to be overconfident. For fields the LLM filled, Lintel takes confidence from the OCR words the value was read from, not from the model's opinion of itself. That score says how well the characters were read, not whether they belong in that field; the rules below check part of that.

The second signal is validation: deterministic rules that encode what Lintel accepts as a valid invoice. Net plus VAT equals the total, to the cent. The date exists and is not after the day of upload. The IBAN's check digits are right, which catches a single misread character. The VAT number has the Dutch format: NL, nine digits, B and two more digits. And the IBAN matches the one already on file for that supplier.

The two signals miss different things. On the cement invoice, OCR read the total as 1.234,00 with a confidence of 0.97: the coffee ring had turned an 8 into a 3. Confidence said accept; the rule saw that net 1.061,16 plus VAT 222,84 makes 1.284,00, not 1.234,00, and flagged all three amounts, since it cannot tell which one is wrong. The other way round, a faint VAT number can pass the format check while its digits carry a confidence of 0.41: the format is right, but whether those are the supplier's digits is a guess. A field goes to a person when either signal objects.

Not every field deserves the same bar. A misspelt supplier name costs a bookkeeper a minute to fix later, since the VAT number already identifies the supplier. A wrong IBAN sends money to a stranger, and changed bank details on a genuine-looking invoice are a known form of fraud. So an IBAN that differs from the one on file goes to a person whatever its confidence, while a supplier name is accepted at a score that would send an amount to review.

Pages in, checked fields out

The upload returns at once; a worker runs OCR, then extraction against the document type's schema. Rules and confidence decide which fields a person checks, and the webhook waits until the last field is resolved.

The reviewer sees the field and the pixels

A document whose fields all pass goes straight through. For the rest, the reviewer gets only the flagged fields, each beside a crop of the original cut from its bounding box (or the whole page, on which the reviewer marks the region, if it has none), with the reason it was flagged: total does not equal net plus VAT, IBAN differs from the supplier's. If 15% of documents need two minutes each, month end costs about 45 reviewer-hours a day.

A document is done only when its last field is resolved, so the customer's system is notified then, not when the model finishes; delivering that notification reliably is Webhooks & third-party integration. Every field keeps its page and region, who or what set it, and the value before any correction, so an auditor asking why Lintel paid €1.284,00 can be shown the line on the photo.

Who picks 0.90

A threshold is a dial between two costs. Raise the confidence a total needs and fewer wrong totals get through while more documents wait for reviewers; lower it and the reverse. Engineers measure both sides; where to sit on that curve is a business choice for whoever carries the cost of a wrong payment and of the reviewers, at Lintel the head of operations.

The score means little until it is checked against what people found. Lintel had 3,000 documents, drawn at random, checked field by field, grouped the extracted totals by score and counted how many were right in each group: in that sample, totals scored above 0.95 were correct 99.7% of the time, those between 0.85 and 0.95 only 96%. That table, per field and per document type, turns 0.90 into a decision: how many errors a threshold lets through, and how many reviews it costs. It is rebuilt after any model or provider change, because a new model's 0.90 is a different 0.90, and so is a fallback provider's.

Corrections are the accuracy report

Every reviewed field is stored as a row, corrected or not: document type, source (photo, scan or PDF), field, extracted value, final value, score, provider and model version. Counted per field and type, the share of rows a person changed shows where extraction fails most, such as IBANs on photographed invoices, and reveals drift, such as one merchant's new layout sending its totals to review.

But people only ever see flagged fields, so a wrong total that was accepted never reaches the table; a small random sample of accepted documents, reviewed in full each day, measures what the thresholds let through. The checked documents also become a labelled set for testing a new prompt or model before it ships (Evaluating LLM output: golden sets, judges & release gates); training a model on them is Fine-tuning an LLM: when it beats prompting, and how to prove it.

The AI Document Processing Challenge asks for this service. The design to avoid there is the one where only a low score sends a field to a person: the cement total would have been paid at 0.97. So before you draw the review console, decide where your rules run, which fields go to a person whatever their score, and what a correction writes.

Practice this