[FEAT] : add PDF Image Parsing Memory Optimizations (OOM Prevention)#516
Merged
Merged
Conversation
param20h
approved these changes
Jun 7, 2026
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.
🔗 Related Issue
Closes #487
📝 What does this PR do?
This PR resolves the memory-exhaustion (OOM) risks during PDF ingestion by optimizing how images are parsed and captioned:
extract_pdf_imagesto be a generator that yields images lazily, page-by-page.chunk_document. Raw image byte strings are processed, transformed into text descriptions, and freed immediately.finallyblock to guarantee raw binary data is garbage-collected page-by-page even if captioning throws an exception.chunk_documentso that if the captioning model/API fails (e.g. rate limit, network timeout), the image isn't dropped. Instead, it falls back to a placeholder text chunkImage on page X., ensuring content presence is preserved.test_pdf_image_captioning_on_the_flyto verify the generator loop behavior, error fallbacks, and byte-clearing correctness.🗂️ Type of Change
🧪 How was this tested?
test_pdf_image_captioning_on_the_flyinbackend/tests/test_chunker.py).venv/Scripts/pytest— all 120 tests passed)generate_captions_for_chunksinvision.pyis kept for API compatibility, but is now safely bypassed during PDF chunking because the returned list of chunks fromchunk_documentcontains pre-captioned image descriptions without raw byte payloads.✅ Self-Review Checklist
dev, notmainmainbranch or any HuggingFace deployment config