Enrichment Pipeline#
The enrichment pipeline is Docling's post-document-assembly processing phase. After the main layout analysis, OCR, and table extraction pass assembles a DoclingDocument, an ordered list of enrichment models (self.enrichment_pipe) runs against the assembled document to annotate specific element types with additional information. All built-in enrichment models are disabled by default because they require additional model inference and increase processing time.
Execution Loop#
The loop in BasePipeline._enrich_document() iterates each model in enrichment_pipe, uses the model's prepare_element() to filter and prepare candidates, chunks them into batches via elements_batch_size, and calls the model as a callable on each batch. The inner iterator must always be fully exhausted.
Built-in Enrichments#
| Feature | Flag | Element type | Notes |
|---|---|---|---|
| Code understanding | do_code_enrichment | CodeItem | Sets CodeItem.code_language; uses CodeFormulaVlmModel |
| Formula understanding | do_formula_enrichment | TextItem(FORMULA) | Extracts LaTeX; exported as MathML in HTML |
| Picture classification | do_picture_classification | PictureItem | Classifies chart types, logos, diagrams, etc. |
| Picture description | do_picture_description | PictureItem | Captioning via VLM (local or remote API) |
Pipeline Composition#
ConvertPipeline initializes the base enrichment_pipe with the picture classifier, picture description, and chart extraction models. StandardPdfPipeline._init_models() prepends CodeFormulaVlmModel to the inherited list using spread syntax — so code/formula enrichment always runs first.
Base Classes#
All enrichment models in docling/models/base_model.py extend one of two abstract base classes:
BaseEnrichmentModel #
Simplest variant. prepare_element() returns the element as-is if is_processable() returns True, otherwise None. No image cropping. Used for text-only enrichment.
BaseItemAndImageEnrichmentModel #
Used when the enrichment needs a cropped image of the element's bounding box. prepare_element():
- If the element is a
PictureItemwith an embedded image, returns it directly. - Otherwise, crops the region from the source page image at
self.images_scalewith optionalexpansion_factorbounding-box expansion.
Returns an ItemAndImageEnrichmentElement (a (item, image) pair) ready for batch inference.
Both classes inherit from GenericEnrichmentModel[T], which declares the three required abstract methods: is_processable, prepare_element, and __call__.
Plugin Architecture#
Custom enrichment models can be injected in two ways:
1. Subclass the pipeline directly (recommended for development)#
Subclass StandardPdfPipeline, override __init__, and replace self.enrichment_pipe with your model list. Pass the custom pipeline class via PdfFormatOption(pipeline_cls=...).
class MyPipeline(StandardPdfPipeline):
def __init__(self, pipeline_options):
super().__init__(pipeline_options)
self.enrichment_pipe = [MyEnrichmentModel(...)]
2. Setuptools entry-point plugins#
The factory system uses a PluginManager (backed by pluggy) to discover registered models from installed packages via setuptools entry points. External plugins are only loaded when allow_external_plugins=True is set in PipelineOptions — by default only modules whose names start with "docling." are trusted.
Production Implementations#
| Model | Base class | Key output |
|---|---|---|
DocumentPictureClassifier | BaseItemAndImageEnrichmentModel | item.meta.classification predictions |
CodeFormulaVlmModel | BaseItemAndImageEnrichmentModel | item.text (LaTeX or source code), CodeItem.code_language |
Key Files#
| File | Purpose |
|---|---|
docling/models/base_model.py | BaseEnrichmentModel, BaseItemAndImageEnrichmentModel, GenericEnrichmentModel |
docling/pipeline/base_pipeline.py | _enrich_document() loop |
docling/pipeline/standard_pdf_pipeline.py | enrichment_pipe construction |
docs/usage/enrichments.md | User-facing enrichment docs with code examples |
docs/examples/develop_picture_enrichment.py | Scaffold for building a custom enrichment model |
docs/examples/develop_formula_understanding.py | Scaffold for custom formula enrichment |
docling/models/factories/base_factory.py | Plugin discovery and model registration |