title: RAG gives wrong answers on scanned PDFs: how to find the cause and fix it
description: A scanned PDF has no text layer, so a text loader hands your RAG pipeline empty or garbage chunks and the model answers from nothing. A ten-line check that finds the pages with no text, then three fixes with working Python, cheapest first: add a text layer with OCR, re-read weak pages with a vision model, and refuse to answer from low-confidence text.
RAG gives wrong answers on scanned PDFs¶
Your RAG can't read. A scanned PDF is a stack of photos. A text loader finds no words on them, so your index holds empty or garbled chunks, the retriever returns the wrong ones, and the model fills the gap with a confident guess.
The model is rarely the problem. Check what went into the index first.
1. Find the pages with no text (free, ten lines)¶
import pymupdf # pip install pymupdf
def pages_without_text(pdf_path, min_chars=50):
"""Page numbers (1-based) where a text loader will get little or nothing."""
weak = []
for page in pymupdf.open(pdf_path):
if len(page.get_text().strip()) < min_chars:
weak.append(page.number + 1)
return weak
print(pages_without_text("contract_scan.pdf")) # e.g. [1, 2, 3, 4]: every page is an image
If this prints page numbers, those pages never reached your vector store as text. Some scans do carry a text
layer from an old OCR pass; then the check passes and the text is still wrong. Print a chunk and read it:
rn for m, 1 for l, columns interleaved line by line are the usual signs.
Three fixes, cheapest first.
| Fix | Works for | Cost |
|---|---|---|
| 2. Add a text layer with OCR | clean printed scans, one column | free |
| 3. Read weak pages with a vision model | tables, multi-column pages, forms, faded or skewed scans | model cost, or a GPU for local parsers |
| 4. Do not answer from text you do not trust | every pipeline | free |
2. Add a text layer with OCR (free)¶
pip install ocrmypdf # needs Tesseract: brew install tesseract / apt install tesseract-ocr
ocrmypdf --skip-text --deskew contract_scan.pdf contract_text.pdf
--skip-text leaves pages that already have text alone. Load contract_text.pdf with the loader you already use.
This is enough for clean, single-column printed pages. It is not enough when the answer sits in a table (OCR returns words, not rows and columns), on a two-column page (lines from both columns get merged), or in handwriting.
3. Read weak pages with a vision model¶
When the layout carries the meaning, something has to look at the page. ReadAnyDoc (open source, Apache-2.0) reads each page with a vision model, checks the result against local OCR, and returns Markdown with tables as tables:
from readanydoc.integrations.langchain import ReadAnyDocLoader
docs = ReadAnyDocLoader("contract_scan.pdf", split="block").load() # one Document per paragraph, table...
print(docs[0].page_content)
print(docs[0].metadata) # source, page, block_type, bbox, confidence
How good is it on scans, measured against Reducto and against
Claude Haiku on its own:
| Test | ReadAnyDoc | Reducto | Claude Haiku alone |
|---|---|---|---|
| 85.4 | 80.9 | 82.2 | |
| olmOCR-bench, old scans (98 pages) | 52.9 | 49.0 | 47.5 |
| Scanned forms, question paired with its answer (FUNSD, 199 forms) | 77.4% | 78.6% | 71.6% |
Measured by us; method and per-page results. Read the middle row twice: on old, faded scans the best system gets about half the tests right. No parser makes a bad archive scan safe to answer from.
Where it loses: 20-50 seconds a page against about 5 for Reducto, and about $0.0045 a page in model usage, so run
it only on the pages step 1 flagged if cost matters. Other parsers that read layout:
Docling (local, MIT),
Unstructured's hi_res strategy,
LlamaParse (hosted), Azure Document Intelligence and Amazon
Textract (cloud).
How they compare.
4. Do not answer from text you do not trust¶
A wrong answer with a citation is worse than "I could not read that page".
- Keep the page number and a confidence label in each chunk's metadata, and drop or flag the doubtful chunks
before they reach the prompt. With the loader above:
[d for d in docs if d.metadata.get("confidence") != "low"]. - Show the page next to the answer, so a person can check a number against the scan. Draw the source box on the page.
- For numbers that must be right (totals, dates, account numbers), extract them into a schema and check them before saving instead of asking a chat model to recall them.
Questions¶
Why does my chatbot answer correctly on some PDFs and not others? The ones that work were exported from Word or a browser and contain real text. The ones that fail are scans or photos. Step 1 tells you which is which.
Will a bigger model fix it? No. If the chunk is empty or garbled, a stronger model writes a more convincing wrong answer.
Should I just send the whole PDF to a model with vision? For a few short documents, yes, and it is the simplest thing that works. It stops working when the document set is larger than the context window or when you need the same page answered thousands of times; then you need the text stored once, correctly. ReadAnyDoc vs a plain LLM.
Do I need a GPU? Not for steps 1, 2 or 4. Step 3 with ReadAnyDoc calls a model over an API; local parsers such as Docling are faster with one.