Ground AI answers in PDFs with PyMuPDF
Updated
Grounding means linking every claim in an AI answer to the exact place in the source document that supports it: the page, and the region on that page. PyMuPDF reads the position of every character in a PDF, so it can turn any model's citation into exact page coordinates, a highlighted passage or an evidence image that a reviewer can check in seconds.
Why grounding matters
AI answers about documents are only useful if people can check them. Language models often cite the right document but the wrong passage, and sometimes quote text that is not in the source at all: courts have now dealt with more than a thousand cases of AI-fabricated citations in legal filings. A citation that says "page 47" still leaves the reader searching. A highlight on the exact sentence, table cell or figure lets them confirm it.
What PyMuPDF adds
- Exact coordinates. Positions come from the PDF's own text layer, not from a vision model's estimate, so evidence boxes are exact for born-digital PDFs.
- Any model. Claude, OpenAI, Gemini or a local model: any answer that includes a quote and a page can be located and highlighted.
- Runs locally. Locating, highlighting and checking citations need no extra model or cloud service.
- The hard cases. Text across line breaks and hyphenation, table cells with their row and column labels, and text that came from OCR.
Articles
Cookbook recipes
Fuzzy Number Matching with the PyMuPDF Grounding Skill
Match numbers in AI quotes against their word equivalents in a PDF
Chunked Documents with PyMuPDF
Chunk blocks of text with bounding boxes using PyMuPDF.
Build an Evidence Pack for an AI Answer
Turn an AI answer and its citations into one PDF that a reviewer, an auditor or a customer can check without any other tools.
Catch Hallucinated Citations
Run every quote an AI answer cites through the source PDF, and count the ones that are not there.
Cite the Table Cell, Not the Page
When an AI answer quotes a number from a financial table, point to the exact cell, with its row and column, instead of the whole page.
Know Which Evidence Came from OCR
Not all text in a PDF is equally reliable. Tell born-digital text from OCR text, and flag OCR evidence for review.
Verify AI-Extracted Values Against the PDF
An LLM pulls fields out of an invoice or contract. Before anyone relies on them, check each value against the document and show where it is printed.
Keep Page Coordinates with Your RAG Chunks
Every chunk in your vector store can remember exactly where it came from in the PDF. PyMuPDF already gives you the coordinates; this article shows how to keep them.
Cite Claude Answers
Sends a PDF to Claude with citations enabled, locates each cited passage on the page with PyMuPDF, highlights it, and exports coordinates for a web viewer.
Ask a PDF and Highlight the Evidence
Answer questions from a PDF with page citations, verify each quoted passage, and create a copy with the evidence highlighted using PyMuPDF.