DEV Community

Kamal Mosadegh
Kamal Mosadegh

Posted on

PDF Extraction Kept Failing Silently — So I Built an Extractor That Refuses to Be Quietly Wrong

Every PDF pipeline I have used shares the same bad habit: when extraction goes wrong, it goes wrong quietly. You get a 200 status, some markdown, and no idea that half the document is missing — or that it is the wrong document entirely.

pdf-slice is my attempt at the opposite contract: a small, local, CPU-only PDF-to-markdown tool that never silently returns wrong content. If the output might be wrong or incomplete, it says so, loudly.

Failure mode 1: the cache served a different document

Several different files shared the name Nexus.pdf — and one cache key. A user asking for the April report silently received the March report instead, with correct-looking metadata. The fix is boring and absolute: pdf-slice keys its cache by full URL + SHA-256 of the downloaded bytes, never the filename, and re-verifies the content hash before serving anything cached. Mismatch means fresh extraction, never the wrong document.

Failure mode 2: the text was there, but invisible

A 46-page PDF with a full text layer came back as ~3,000 header-only characters. The body text was white, nested inside form XObjects under an opaque image — and the classifier had confidently declared "text-based, no OCR needed". Other extractors found 86,000+ characters in the same file.

pdf-slice treats a suspiciously thin native extraction as a signal, not an answer: it falls back to OCR automatically, and always reports which path produced the output.

The design rules

  1. Identity is a hash, not a name. Two files with the same name and different hashes are different documents.
  2. Thin output is a signal. Below a per-page threshold, OCR runs; if it is still thin afterwards, the result is labelled low-confidence (fallback) with a warning.
  3. Every page carries provenance. The JSON metadata records each page's origin (native/OCR), character count, tables and timing — plus the document's content_hash and cache state.
  4. Fail loudly. Encrypted, corrupt, empty or non-PDF inputs produce a clear error — never empty "successful" output.

Outputs: clean markdown (multi-column reading order, tables as markdown tables), provenance JSON, and RAG chunks in JSONL — each chunk with text, page, element_type, confidence, source (URI + content hash) and reading_order.

Benchmark

On the fixtures bundled with the tool (generated by its own test suite):

File Path Word recovery Seconds/page Silent errors
white native 100% 1.575 0
scanned ocr 100% 2.802 0
multilingual native 100% 1.401 0
table native 100% 1.632 0
large (120 pages) native — 0.462 0

Total silent errors: 0 — verified by a 26-group test matrix (cache collisions, white text, scanned, multilingual, tables, columns, corrupt, encrypted, batch mode, MCP, clean installs), each test repeated 3×.

MCP server included

pdf-slice ships an MCP server (stdio) for Claude Code, Cursor and Codex, with two tools: extract_pdf and verify_pdf — the latter extracts the same document from two routes and reports a match_rate, so a route serving different content is caught instead of trusted. There is also a ready-made agent skill in the repo.

Try it

git clone https://github.com/kamalmosadegh/pdf-slice && cd pdf-slice
python -m venv .venv && .venv/bin/pip install -r requirements.txt
.venv/bin/python extract.py your-file.pdf
Enter fullscreen mode Exit fullscreen mode

If you feed PDFs into LLM pipelines or RAG systems, I would genuinely like to hear where this breaks for you — issues and feedback very welcome.

Top comments (9)

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Caching on the full URL plus the hash instead of the filename is the detail that saves this from the usual stale-file bug. Does the dual-route check run on every extract, or only when the routes disagree?

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

Thank you, Odinaka — and yes, that cache detail was exactly the bug I was trying to kill: same filename, different document, silently served.

Good question, and it's worth separating the two checks. The native-first, per-page fallback runs on every extract: pdf-slice extracts natively first, and if a page's native text is suspiciously thin — below a configurable chars-per-page threshold — or the page is image-heavy with little text (the white-text / nested-form case), that page is automatically routed to OCR. The output always states which path produced it — native, OCR, or fallback — and if it's still thin afterwards, it's labelled low-confidence with a warning instead of staying silent.

The separate verify_pdf check is different: it's an on-demand MCP tool that extracts the same document from two routes and reports a match_rate, so a route serving different content gets caught rather than trusted.

On caching: the key is the full URL plus the SHA-256 of the downloaded bytes, and that hash is verified before any cached output is served — so a changed document can never be served as the old one.

Have you run into the same stale-cache or thin-extraction failure in your own pipelines? I'd be curious which PDFs break most often for you.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Verifying the hash before serving anything cached is what makes this hold together. A low-confidence label beats a silent thin extract, since the thin page is the one that gets trusted.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

Thank you, Odinaka — you captured the core trade-off exactly. A thin extract that looks confident is far more dangerous than one labelled low-confidence, because nobody double-checks the confident-looking page. That is why the hash is verified before any cached result is served, and why a page that stays thin after the OCR fallback is flagged low-confidence with a warning instead of being passed through silently.

If you ever hit a PDF where the two routes disagree in an interesting way, I would love to hear about it — those are the cases that teach the extractor the most.

— Kamal

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

When two routes disagree, one of them guessed at the layout. Which do you trust then, the structured parse or the raw text layer?

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

Great question — and the honest answer is: neither one, blindly. That is exactly why verify_pdf exists.

The structured parse is usually better when the document has real structure to preserve — tables, multi-column reading order, headings. But it can also guess wrong: it can reorder columns, merge cells that should stay apart, or treat a layout artifact as content.

The raw text layer is closer to the characters as stored, so it is a useful check on what is actually there. But it has its own failure modes: it can include hidden or white text, lose reading order entirely, and flatten a table into an unreadable stream of values.

So when the two routes disagree, my rule in pdf-slice is:

  1. Treat the disagreement itself as the signal — do not silently pick a winner.
  2. Look at the per-page provenance in the JSON: origin (native/OCR), character count, and tables. A route that suddenly drops most characters, or reports tables where the other finds none, is usually the one that guessed.
  3. Compare the differing pages directly, not just the overall match_rate. A small overall difference concentrated on one table is very different from text missing across the whole document.
  4. If neither route can explain the other, label that page low-confidence and flag it, rather than shipping a confident-looking wrong answer.

In practice, I trust the structured parse more for well-formed, text-based documents with tables, and I trust the raw layer more as a sanity check on completeness — did we lose text, or invent text? The moment they disagree, the document goes into the "needs a human look" bucket.

Thanks for pushing on this — it is the exact edge case the two-route check was built to surface.

— Kamal

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

Treating the disagreement itself as the signal, not silently picking a winner, is what makes the two-route check worth running.

Thread Thread
 
kamalmosadegh profile image
Kamal Mosadegh •

Exactly — that is the whole point of the design. A confident-looking wrong answer is far more dangerous than an honest "I don't know", especially when the output feeds a RAG pipeline where nobody reads the source PDF again. If the two routes agree, I trust the result; if they don't, the disagreement itself becomes the most useful piece of information in the output. Thanks again for the thoughtful back-and-forth — this thread genuinely sharpened how I think about it.

— Kamal

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

the disagreement being the most useful output is the part that sticks.