Structured Information Extraction#
Docling's structured information extraction is a distinct capability from document conversion. Where DocumentConverter transforms documents into Docling's intermediate representation (markdown, JSON, etc.), the extraction subsystem uses a Vision-Language Model (VLM) to pull specific structured fields from a document page — returning typed, schema-driven data rather than a converted document.
The two subsystems share infrastructure (backends, InputDocument, pipeline caching) but are intentionally separate classes with separate APIs .
Entry Point: DocumentExtractor#
DocumentExtractor in docling/document_extractor.py is the top-level class. It mirrors the DocumentConverter interface but exposes:
extract(source, template, ...)— single-document extraction, returnsExtractionResultextract_all(source, template, ...)— batch extraction, returnsIterator[ExtractionResult]
Both methods require a template argument of type ExtractionTemplateType (see below). Supported input formats are PDF and IMAGE only.
Pipeline instances are cached by (pipeline_class, options_hash), so repeated calls with the same options reuse the same model.
Template: ExtractionTemplateType#
ExtractionTemplateType (in docling/datamodel/extraction.py) is a Union type alias:
Union[str, Dict[str, Any], BaseModel, Type[BaseModel]]
Four template forms are accepted:
| Form | How it's serialized to a prompt |
|---|---|
str | Used verbatim |
dict | json.dumps(template, indent=2) |
BaseModel instance | model.model_dump_json(indent=2) |
BaseModel class | A factory instance is built from Field(examples=...) / defaults via polyfactory |
Serialization logic lives in ExtractionVlmPipeline._serialize_template(). When passing a Pydantic class, Field(examples=[...]) values are used to generate representative JSON, which the VLM uses as a schema hint.
Pipeline: ExtractionVlmPipeline#
ExtractionVlmPipeline (in docling/pipeline/extraction_vlm_pipeline.py) is the default (and currently only) extraction pipeline. It:
- Renders each document page to an image via the PDF/image backend.
- Serializes the template into a prompt string.
- Feeds
(image, prompt)into aTransformersExtractionModel. - Attempts
json.loads()on the VLM output; falls back toraw_textif not valid JSON.
It does not support ThreadedDoclingParseDocumentBackend.
Default model: NU_EXTRACT_2B_TRANSFORMERS (NuExtract-2B). An alternative is GRANITE_VISION_4_1_TRANSFORMERS. Both are configured via VlmExtractionPipelineOptions.
Configuration: VlmExtractionPipelineOptions#
Located in docling/datamodel/pipeline_options.py :
vlm_options(InlineVlmOptions): which VLM model to load; defaults toNU_EXTRACT_2B_TRANSFORMERSextraction_prompt_style(ExtractionPromptStyle): prompt format;NUEXTRACT(default) orGRANITE_VISION
Pass a custom VlmExtractionPipelineOptions via ExtractionFormatOption when constructing DocumentExtractor:
from docling.document_extractor import DocumentExtractor, ExtractionFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import VlmExtractionPipelineOptions
from docling.pipeline.extraction_vlm_pipeline import ExtractionVlmPipeline
extractor = DocumentExtractor(
extraction_format_options={
InputFormat.PDF: ExtractionFormatOption(
pipeline_cls=ExtractionVlmPipeline,
pipeline_options=VlmExtractionPipelineOptions(...),
)
}
)
Output: ExtractionResult#
ExtractionResult contains:
status:ConversionStatus(SUCCESS,PARTIAL_SUCCESS,FAILURE)errors: document-levelErrorItemlistpages: list ofExtractedPageData, each with:page_no: 1-indexed page numberextracted_data: parsedDict[str, Any](populated when VLM output is valid JSON)raw_text: raw VLM output string (always populated)errors: page-level error strings
To load structured data back into a Pydantic model:
invoice = Invoice.model_validate(result.pages[0].extracted_data)
PARTIAL_SUCCESS is set when the VLM hits a token-length or stop-sequence limit.
Key Source Files#
| File | Purpose |
|---|---|
docling/document_extractor.py | DocumentExtractor — public API |
docling/datamodel/extraction.py | ExtractionResult, ExtractedPageData, ExtractionTemplateType |
docling/pipeline/extraction_vlm_pipeline.py | ExtractionVlmPipeline — VLM execution, template serialization |
docling/pipeline/base_extraction_pipeline.py | BaseExtractionPipeline — abstract base |
docling/datamodel/pipeline_options.py | VlmExtractionPipelineOptions |
docling/datamodel/extraction_options.py | ExtractionPromptStyle enum |
docs/examples/extraction.ipynb | End-to-end usage notebook (all four template forms) |
Install note: The VLM extraction feature requires
pip install docling[vlm].