Skip to content

fix(files): add OCR fallback for image-only PDFs - #3442

Open
Sanderhoff-alt wants to merge 1 commit into
vectorize-io:mainfrom
Sanderhoff-alt:fix/markitdown-pdf-ocr-fallback
Open

fix(files): add OCR fallback for image-only PDFs#3442
Sanderhoff-alt wants to merge 1 commit into
vectorize-io:mainfrom
Sanderhoff-alt:fix/markitdown-pdf-ocr-fallback

Conversation

@Sanderhoff-alt

Copy link
Copy Markdown
Contributor

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_parse retain their expected fallback behavior.

Dependency and Licensing

PDF rendering uses the explicitly declared pypdfium2>=5.4.0 dependency. 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

@Sanderhoff-alt
Sanderhoff-alt force-pushed the fix/markitdown-pdf-ocr-fallback branch from 57f9436 to 29c7957 Compare August 12, 2026 16:10
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
Sanderhoff-alt force-pushed the fix/markitdown-pdf-ocr-fallback branch from 29c7957 to 9679232 Compare August 12, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

file conversion: image-only (scanned) PDFs fail with 'no content extracted' — no OCR fallback

1 participant