B2reading roomEssay19 August 20264-minute read

Letting an LLM read PDFs without trusting it

Models are good at reading messy documents and bad at admitting when they've guessed.

Language models are very good at reading messy documents. They're also very good at sounding sure when they've guessed. I built a service that reads hotel booking confirmations, PDFs from different suppliers in English, Arabic or both, and most of the work went into the space between those two facts.

Text, pictures, or both

The first version sent the model the PDF's extracted text. Text is cheap and quick, and it loses things: tables collapse into lines, and Arabic can come out of a PDF in an order no person would ever read.

The next version rendered every page as an image instead. That keeps the layout, but now the model can misread a digit in a picture, and a booking total is exactly the kind of thing you don't want misread.

The version that stuck sends both in one request, the text and the page images, with an instruction that the images win when the two disagree. The text gives the model exact strings; the pictures tell it where things are and what belongs to what. Later, a second provider could read the PDF directly. By then all of it sat behind one interface, and the code asking for a booking stopped caring which method or model was answering.

Examples leak

To show a model the shape you want, you give it an example. Mine was a filled-in booking: a hotel, a guest, a city, a couple of totals. The model learns the shape from it. It also learns the values. Put "Mock Hotel" in your example and sooner or later a document without a clear hotel name will come back as Mock Hotel.

So the prompt carries a rule that sounds silly until you need it: never use a value from the example unless it's actually in the document.

Ask it to read, not to research

An early version of the prompt asked the model to work out the postal code and the coordinates from the hotel's address. That's asking a reader to become a researcher, and the answer looks exactly the same whether it was read or invented.

The later prompt says the opposite: extract only what's in the document, and never guess. Anything that can be worked out, like the city and the currency, is worked out in code afterwards, where it can be tested and where it's wrong in the same way every time.

Empty has to look empty

A model filling in a form wants to fill every box. So the prompt spells out what empty looks like for every kind of field: an empty string, null, an empty list, zero. Even that leaves a trap. Zero children might mean no children, or it might mean the document didn't say. If the difference matters, the schema needs a way to tell them apart.

Give it somewhere to confess

The most useful field in the whole schema isn't a booking field. It's a list of notes, in both languages, where the model writes down what it assumed: defaults it used, things that were ambiguous, fields it translated, values that conflicted. Those notes are shown to the person checking the booking.

A model that can say "I wasn't sure about this one" is far easier to work with than a model that has to commit to an answer for every box.

Shape isn't truth

Every answer is checked against a JSON Schema, and a schema is a good reviewer: cheap, fast and never tired. But it checks shape, not truth. A wrong date in the right format sails through. So the schema decides whether an answer is worth showing to anyone, and a person decides whether it's right.

The model never saves anything

That's the rule under all the others. The model reads, the schema checks, the answer pre-fills a booking form, and a person submits it. If an answer fails the schema, the form stays empty, because a half-right booking is worse than a blank one.

What I'd tell someone starting

  • Send text and images together, and say which one wins.
  • Keep the example's values out of the answer, in so many words.
  • Ask the model to read, not to research. Work out the rest in code.
  • Define what empty means for every field.
  • Give the model a place to say what it guessed, and put it in front of a person.
  • Let a schema check the shape, and leave the truth to a human.

Models are good readers. They're just bad at admitting when they've guessed, so build a system that admits it for them.

Ring the bell

Got a question one of these didn't answer? Ask me directly.

Get in touch