Topic 1 of 5 · 4 min read
Extracting structured data from scanned documents
Goal: Write an extraction request with a schema for a scanned document and find the fields where the model output does not match the source.
Extract from a document by giving the model the page and a strict schema, and by telling it what to do when a field is not there. A vision model will happily fill every slot you give it. Your job is to make "not found" an allowed answer.
Check yourself · 1 of 3
A scanned invoice has an empty box for the purchase order number. How should the extraction request handle it?
Why it matters
Invoices, receipts and forms arrive as scans, photos and PDFs. Typing them in by hand is slow and error-prone. Claude, Gemini and GPT models can read a page image and return JSON, but a fluent wrong value looks exactly like a right one. A wrong invoice total reaches your accounting system as clean data.
How to do it
- Choose the input. A born-digital PDF has a text layer you can extract for free and exactly. A scan has no text layer, so send page images. Many APIs accept a PDF directly and use both the text and the page image. Check your provider's docs for page and size limits.
- Write the schema first. List each field, its type and its format:
invoice_numberas a string,totalas a number,issue_dateasYYYY-MM-DD,currencyas a three-letter code. Use structured output or a JSON schema so the shape is enforced. - Allow null. Mark every field that may be missing as nullable, and say in the prompt: "If a field is not visible on the page, return null. Never guess or calculate it."
- Ask for the source. Add a short
evidencetext next to important fields, such as the exact line the value came from. It makes review fast. - Handle many pages. Send pages in order with their numbers, extract per page or per section, then merge. Totals usually live on the last page; line items run across several.
Before and after
Weak request: "Read this invoice and give me the data as JSON."
The model invents its own field names, writes dates as "3rd of March", and when the PO number is missing it returns a plausible one.
Fixed request: a schema with typed fields, purchase_order nullable, the instruction to return null for anything not printed, and one line of evidence per money field. Now a missing PO number comes back as null, and a reviewer can check the total against the quoted line in seconds.
What goes wrong
- Filled blanks. An empty "VAT number" box comes back with a realistic number.
- Column slips. In a dense table, a quantity lands in the unit-price column of the next row.
- Page drops. On a long file the model skips a page of line items and still reports a total.
- Silent calculation. The model computes a missing subtotal itself instead of reading it.
Examples
A missing purchase order
An invoice has a blank box labelled PO number. The first prompt returns "po_number": "PO-48213", a value that appears nowhere on the page. After adding a nullable field and the rule "return null when not printed", the output is "po_number": null. The reviewer sees a real gap instead of a fake value.
Page two goes missing
A 3-page supplier statement has line items on pages 1 and 2 and the total on page 3. The model returns the page 3 total with only page 1 items. Sending pages as separate numbered images and returning page on every row exposes the gap: no rows say page 2.
Key points
- Use the PDF text layer when it exists; send page images when the file is a scan or photo.
- Define every field with a type and format in a schema, and enforce it with structured output.
- Make missing fields nullable and tell the model to return null instead of guessing or calculating.
- Ask for the quoted source line next to money fields so a reviewer can check them quickly.
- Process multi-page files in order with page numbers, then merge, and check no page was skipped.
Common mistakes
- Sending a scan as plain text extraction and getting an empty or garbled result.
- Letting the model choose its own field names and date formats, then mapping them later.
- Leaving every field required, which forces the model to invent values for blanks.
- Trusting a total because the JSON validated; schema validity says nothing about correctness.
Check yourself · 2 of 3
A PDF was exported from accounting software and its text can be selected with the mouse. What is the best first step for extraction?
Check yourself · 3 of 3
Why add an evidence field holding the source line next to each total?
Practice items, not counted towards your mastery.
Start here
- Vision (Claude docs) (opens in a new tab)DocsFree
Image size limits, token cost per image, and tips for prompt placement and resizing.
- Document understanding (Gemini API) (opens in a new tab)DocsFree
How a model reads PDFs as pages and layout, with extraction examples and file limits.
- Images and vision (OpenAI docs) (opens in a new tab)DocsFree
Image input formats, detail levels and the stated limits of vision models.