Skip to content

AI document data extraction: OCR, vision models and structured output

B2Bnetwork team · · 6 min

Technology and trends

In brief

AI document reading has three layers: text (what is written), layout (what sits in which table column) and meaning (which number is the gross amount and which is the order number). OCR gives only text. A vision model, or a language model reading the text layer of a PDF, extracts fields into a schema: tax ID, date, line items, amounts. The result always passes through control rules: line totals, the tax ID checksum, a match with the order in the ERP. A document that fails the rules goes to a person.

Three layers of reading: text, layout, meaning

LayerQuestionTypical toolWhat goes wrong
Textwhich characters are on the page?OCR, the PDF text layer"0" instead of "O", "1" instead of "l", a stamp over the amount
Layoutwhat is in which column and row?layout analysis, a vision modela line item split across two rows, a table continuing on the next page
Meaningwhich field holds which information?a language or vision model with a schemasale date confused with issue date, net price with gross

Classic OCR, such as the Tesseract engine, recognizes characters but does not know what they mean. Older systems added templates on top of OCR: "the amount is in the bottom right corner". A template works for one invoice layout and breaks when the supplier changes it. Vision models read the page image together with its layout and understand that "Amount due" next to a number means the gross amount, wherever it sits on the page. A comparison of both approaches for invoices is in AI Worker vs invoice OCR.

Matching the method to the document

The first rule: do not read from an image what you can get as data.

DocumentMethodNotes
domestic invoice from KSeFread the XML file, no OCRdata is already structured, matching to the order remains
PDF generated by the supplier's systemPDF text layer plus a language modelfaster and cheaper than a page image
scan or photo of a documenta vision model, or OCR plus a language modelscan quality decides the result
delivery note with handwritten notesa vision model, handwriting always checkedhandwriting is the weak spot of every method
contract, terms, amendmenttext plus a language model, reading selected clausesa long document, citing page and paragraph matters

Invoices from KSeF (Poland's national e-invoicing system) come as a structured XML file. Reading PDFs therefore mostly concerns foreign invoices, warehouse documents, contracts and correspondence. The specifics of foreign invoices are covered in Foreign and multi-currency invoices with AI.

Extraction into a schema: fields, types and gaps

The model should not answer with a description in sentences. It gets a schema in JSON Schema and returns an object with fields. The structured output modes offered by model providers guarantee conformance to the schema format. They do not guarantee that the values are true.

{
  "type": "object",
  "properties": {
    "nip_sprzedawcy": { "type": "string" },
    "numer_faktury": { "type": "string" },
    "data_wystawienia": { "type": "string", "format": "date" },
    "waluta": { "type": "string" },
    "numer_zamowienia": { "type": ["string", "null"] },
    "pozycje": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "nazwa": { "type": "string" },
          "ilosc": { "type": "number" },
          "cena_netto": { "type": "number" },
          "stawka_vat": { "type": "string" }
        }
      }
    },
    "kwota_brutto": { "type": "number" }
  },
  "required": ["nip_sprzedawcy", "numer_faktury", "data_wystawienia", "kwota_brutto"]
}

Two things in the schema make a big difference. A field may be `null`, so the model does not have to guess an order number that is not on the invoice. Amounts are numbers, not text, so "1 234,50 zł" is converted into a number at extraction time, not in the ERP.

Control rules: how to catch an extraction error

A model can read an amount with transposed digits and still look convincing. That is why ordinary code with rules runs after extraction:

  1. The tax ID checksum and IBAN check digits. A single wrong digit shows up immediately.
  2. Invoice arithmetic. Quantity times price gives the line value, the sum of lines gives the net amount, net plus VAT gives gross. A difference beyond rounding means an extraction error or a supplier error.
  3. Match with the ERP. The tax ID is in the supplier master data, the order number exists and is open, the bank account matches the master data.
  4. Duplicate. The same invoice number from the same supplier has already been seen.
  5. Dates and currency. The due date is after the issue date, the currency matches the order.

The rules catch most extraction errors before anyone sees the document. A document that passes them all can move on as a draft. One that fails goes to a person with a note on which rule failed.

Extraction confidence and routing to a person

A language model does not give a reliable "confidence" for each field, so what a person checks is decided by rules and error history, not by the model's own claim. A proven approach:

  • Each field measured separately on a test set. A tax ID from a clean PDF may be flawless while quantities from a handwritten delivery note are weak.
  • Thresholds by value. An invoice above a set amount is always reviewed, whatever the rules say.
  • New supplier, new layout. The first documents from a new supplier go to a person until extraction proves reliable.
  • Record the source. Each field keeps its page number, so the accountant sees where a number came from.

This is how the Purchase invoice checker from DYNAMINDS Labs works. It pulls invoices from KSeF and foreign PDF invoices from the invoices@ mailbox, checks the tax ID, prices, quantities and payment terms, and matches each invoice to the purchase order and goods receipt. Every morning it delivers a list of invoices ready for posting and, separately, the discrepancies with a description. Posting stays with a person. How a model uses documents it never saw in training is covered in Fine-tuning vs RAG.

Questions and answers

Does AI invoice reading still make sense once KSeF is in place?

For domestic invoices from KSeF, image reading is not needed because the data is in an XML file. What remains is work KSeF does not do: matching the invoice to the order and receipt, describing the cost, explaining discrepancies. PDF reading covers foreign invoices, delivery notes, contracts and documents outside KSeF.

Is a vision model always better than OCR?

Not always. For clean PDFs with a text layer, extracting the text and giving it to a language model is cheaper and faster. A vision model wins on scans, photos, unusual layouts and tables spanning several pages. A test on a sample of your documents decides.

How many documents do you need to check extraction quality?

For a first assessment, usually 200–300 documents from recent months in the same proportions as daily work, plus a separate group of hard ones: scans, multi-page documents, new suppliers. Results are calculated per field, because an average hides weak spots.

What about personal data in documents?

Invoices and contracts contain data about people, for example sole traders or supplier staff. The GDPR applies: extraction in an environment the company controls, or with a provider under a data processing agreement, plus a limited retention period for images and logs.

What it looks like in a project

Considering a new technology for your system? Talk to an engineer.

Let's talk

Read more: Technology and trends

All in Technology and trends →