PDF Parse Lifecycle Management#
Overview#
During PDF conversion, the StandardPdfPipeline maintains three categories of intermediate per-page artifacts in the Page datamodel:
_image_cache— Adict[float, Image]mapping render scale → PIL image, populated during the preprocessing stage ._backend— A reference to thePdfPageBackend(pypdfium2 or docling-parse), which holds C-side page resources .parsed_page— ASegmentedPdfPage | Nonecarrying PDF text/bitmap cells extracted from the backend .
All three are expensive: _image_cache holds decoded bitmaps in RAM, _backend ties up native memory/file handles, and parsed_page stores the full cell list per page. The pipeline garbage-collects them as early as possible — unless downstream stages need them.
Decision Logic: What Gets Kept#
Two pipeline-level boolean flags gate resource retention. Both are computed once in _init_models() and held for the lifetime of the pipeline instance.
keep_images#
Set to True if any image-output feature is active :
generate_page_images OR generate_picture_images OR generate_table_images
When False, _image_cache is cleared to {} after assembly.
keep_backend#
Set to True if any enrichment model is active :
do_formula_enrichment OR do_code_enrichment OR do_picture_classification
OR do_picture_description OR do_chart_extraction
Enrichment models (post-assembly VLMs, picture classifiers) crop element bounding boxes from page images after assembly via BaseItemAndImageEnrichmentModel. If no enrichment flag is set, the backend is unloaded immediately after assembly and its reference is nulled.
generate_parsed_pages#
A separate PdfPipelineOptions field controlling parsed_page retention . Defaults to False. When False, parsed_page is set to None after assembly. Setting this to True is required for HeadingHierarchyModel's use_style inference, which needs parsed PDF cell heights .
Cleanup: Where and When#
Cleanup happens at two points in the pipeline execution:
1. Per-page cleanup (after assembly stage)#
_release_page_resources() runs as a postprocess callback on the assemble ThreadedPipelineStage — immediately after each page exits the assemble worker thread :
- Clears
_image_cacheifnot keep_images - Calls
page._backend.unload()and sets_backend = Noneifnot keep_backend - Sets
parsed_page = Noneifnot generate_parsed_pages
This per-page, streaming cleanup frees memory while other pages are still flowing through upstream stages — critical for multi-page documents in the threaded pipeline.
2. Bulk cleanup (after all pages complete)#
_integrate_results() performs a second sweep over all collected pages after the pipeline drains . This is a safety net for pages that may have bypassed the per-page hook (e.g., failed pages, edge cases).
Flow Summary#
Key Files#
| File | Purpose |
|---|---|
docling/pipeline/standard_pdf_pipeline.py | _init_models(), _release_page_resources(), _integrate_results() |
docling/datamodel/pipeline_options.py | PdfPipelineOptions fields: generate_parsed_pages, generate_page_images, do_code_enrichment, etc. |
docling/datamodel/base_models.py | Page datamodel: _image_cache, _backend, parsed_page fields |
Common Scenarios#
| Pipeline configuration | keep_images | keep_backend | parsed_page kept |
|---|---|---|---|
| Text-only (defaults) | False | False | False |
generate_page_images=True | True | False | False |
do_picture_classification=True | False | True | False |
do_code_enrichment=True | False | True | False |
heading_hierarchy_options.use_style=True | — | — | Requires generate_parsed_pages=True |
Implication: Any enrichment feature (do_picture_classification, do_picture_description, do_code_enrichment, do_formula_enrichment, do_chart_extraction) prevents early backend unloading, keeping native PDF page resources alive through the enrichment pass. For memory-constrained environments, disable unused enrichment features.