fix(files): add OCR fallback for image-only PDFs - #3442
Open
Sanderhoff-alt wants to merge 1 commit into
Open
Conversation
Sanderhoff-alt
force-pushed
the
fix/markitdown-pdf-ocr-fallback
branch
from
August 12, 2026 16:10
57f9436 to
29c7957
Compare
MarkItDown only passed image uploads to its configured vision model. PDFs without a text layer therefore returned no content even when OCR was enabled. When normal PDF extraction is empty, render each page at 200 DPI and pass the temporary PNG through MarkItDown's existing image OCR path. Preserve useful pages when one page fails, and raise an actionable error when OCR is disabled or produces no content. The existing parser chain can then advance to the next configured parser, such as llama_parse. Add pypdfium2 for page rendering, document the behavior, and cover page OCR, partial failures, disabled OCR, and parser-chain fallback with regression tests.
Sanderhoff-alt
force-pushed
the
fix/markitdown-pdf-ocr-fallback
branch
from
August 12, 2026 16:13
29c7957 to
9679232
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Image-only and scanned PDFs currently fail when MarkItDown cannot extract a text layer, even when MarkItDown OCR is configured. This change adds a PDF-specific OCR fallback while preserving the existing parser fallback chain.
Problem
MarkItDown's configured vision model is used for image inputs, but its PDF converter only attempts text-layer extraction. A scanned PDF can therefore return no content or raise during conversion without any OCR attempt.
Design
When MarkItDown conversion of a PDF produces no usable text or raises an exception, the parser renders the PDF one page at a time at 200 DPI using pypdfium2. Each page is written to a temporary PNG, passed through the existing MarkItDown image OCR path, and deleted immediately after processing. Successful page results are combined in document order. A failed page is logged and does not discard successful results from other pages.
If OCR is disabled, unavailable, or produces no content, the parser raises an actionable error. The existing parser registry then continues to the next configured parser, so configurations such as
markitdown,llama_parseretain their expected fallback behavior.Dependency and Licensing
PDF rendering uses the explicitly declared
pypdfium2>=5.4.0dependency. This avoids adding PyMuPDF's AGPL/commercial dual-licensing requirements to the MIT-licensed package. The lockfile is updated accordingly.Testing
Regression tests cover empty PDF extraction, PDF converter exceptions, successful per-page OCR, partial page failures, OCR-disabled errors, OCR failure without duplicate attempts, and advancement to the next parser in the configured fallback chain. The focused test suite, project lint, type checks, and lockfile validation pass.
Fixes #3255