FIELD NOTESAI & AGENTS · FINTECH

When the statement is a scan: OCR fallback

A scanned statement has no text layer; it needs a different reader and a stricter trust check. Detect it from the PDF, share one output contract, and accept a reading only when its balances close.

By Braxx-90003 min readPublished
ON THIS PAGE
Open original
Alpaca Mango technical archive cover art.

Building Taxadoo, part 4 of 7. Lessons from building the statement engine behind Taxadoo, a UK bookkeeping product — the decisions and the failures, not the internals. The whole series.

A scanned statement has no text layer, so the reader for it must be chosen from the PDF itself and held to the same output contract as everything else. If you notice a scan only when the parser returns zero rows, you will conflate "no text" with "text we did not recognise" and send the operator after the wrong failure.

Detect scans from the file, not the user#

If a PDF has a text layer, it yields plenty of characters per page; a scan yields almost none. Detect the difference at upload with a loose threshold on characters per page, and do not ask the customer whether the file is scanned. An opt-in "try harder" button is the wrong thing to show someone who has just uploaded a file. If the fallback is off, fail quickly.

Keep "zero rows" separate from "no text". A guard can report a file as image-only when the text is present and the parser simply does not recognise a known layout variant. That sends the operator in the wrong direction. "No text" and "text with no rows" need different people to fix.

One contract, whichever reader#

Whichever reader handles a page, downstream code should not care whether the input was a scan or a text layer. Use one schema and one validator for every reader, so deduplication, categorisation and checks work unchanged. The first fallback instructions missed fields the text path required, such as page numbers and per-page balances; that is how supposedly equivalent paths drift.

A vision-capable model is usually the better fallback than dedicated OCR followed by a second parsing pass. OCR returns text, which then needs another stage to be read as a table. A vision model returns rows directly, using a provider already in use and no native binaries in a serverless runtime. It costs more per page, but this is a long-tail path, so the total stays small. Write down the upload share and monthly cost that would trigger a rethink.

Check the simplest thing the API allows first. The plan was to render pages to images and call once per page; sending the whole PDF in one call worked with no rendering or native dependencies. For long documents, split by page count and log adjustments.

Trust closes balances, not OCR output#

A scan reading should not be accepted just because OCR output exists. Accept it only when its balances close across the whole file. If not, the next model in the chain tries, skipping any model that cannot read images and recording that it did. A card statement with no running balance is checked against its printed previous and new balances. When no reading closes, keep the one that got furthest and hold the statement for a person.

Delete the feature scans broke#

Highlighting a row on the PDF is the clearest casualty. Clicking a row should wrap a box on the page, but that requires a text layer to anchor positions. There are two ways to get positions back: a second OCR pass returns word boxes to fuzzy-match against amounts, which adds cost and has poor recall on tight columns; or ask the vision model for page-relative boxes, which is one pass but some models invent coordinates. If you choose the second, validate the boxes and fall back silently to no highlight.

Then delete the feature. Linking every row to a place on the page forced page membership, per-page balances and guesses about page order through the pipeline. Once each statement is read once into a CSV and the PDF kept only as a picture beside the rows, a large amount of page reconciliation went. Be wary of features that turn the source document into a data structure.