# SPDX-FileCopyrightText: The Docling Contributors # SPDX-License-Identifier: MIT import warnings from enum import Enum from typing import Any, ClassVar, Dict, Iterator, Literal, Optional from pydantic import Field, model_validator from typing_extensions import Self from docling.datamodel.stage_model_specs import StagePresetMixin, VlmModelSpec from docling.models.inference_engines.vlm.base import VlmEngineOptionsMixin class ChartExtractionOutputFormat(str, Enum): """Names the output-parsing contract for a chart extraction model. Each value identifies how the chart stage should interpret the raw text returned by the VLM for each active prompt, independently of the generic VLM ``ResponseFormat`` used by the conversion pipeline. Values ------ GRANITE_VISION_CHARTS The multi-pass Granite Vision chart model protocol: * ```` → fenced ````csv```` block (or bare CSV), parsed into ``TabularChartMetaField`` * ```` → plain-text sentence(s), stored in ``DescriptionMetaField`` * ```` → fenced ````python```` block, stored in ``CodeMetaField`` """ GRANITE_VISION_CHARTS = "granite_vision_charts" GRANITE_VISION_CHART2CSV = "granite_vision_chart2csv" class ChartExtractionVlmEngineOptions(StagePresetMixin, VlmEngineOptionsMixin): """Configuration for the chart extraction enrichment stage. Uses the unified VLM engine system (Transformers / API / MLX / vLLM) and the same preset mechanism as picture description and code/formula stages. The three output modes are independent; enable the ones you need: * ``chart2csv`` — extract numeric data as a CSV table (default: True) * ``chart2summary`` — generate a natural-language description (default: False) * ``chart2code`` — generate Python code that recreates the chart (default: False) The ``granite_vision_v3_3`` preset uses the older CSV-only Chart2CSV model and its recommended plain-language prompt. ``granite_vision_v4`` remains the default and additionally supports summaries and code. Examples:: # Default preset (granite_vision_v4, automatic local engine selection) options = ChartExtractionVlmEngineOptions.from_preset("granite_vision_v4") # Override engine at preset time from docling.datamodel.vlm_engine_options import ApiVlmEngineOptions, VlmEngineType options = ChartExtractionVlmEngineOptions.from_preset( "granite_vision_v4", engine_options=ApiVlmEngineOptions( engine_type=VlmEngineType.API_OPENAI, url="http://localhost:8000/v1/chat/completions", ), ) # Serving a GGUF quantization that lacks the fine-tuned special tokens? # Enable natural-language prompt substitution: options = ChartExtractionVlmEngineOptions.from_preset( "granite_vision_v4", engine_options=ApiVlmEngineOptions( engine_type=VlmEngineType.API_LMSTUDIO, url="http://localhost:1234/v1/chat/completions", ), use_natural_language_prompts=True, ) """ kind: ClassVar[Literal["chart_extraction_vlm_engine"]] = ( "chart_extraction_vlm_engine" ) model_spec: VlmModelSpec = Field( description="Model specification with engine-specific overrides" ) chart2csv: bool = Field( default=True, description=( "Extract numeric data from the chart as a CSV table with headers and values." ), ) chart2summary: bool = Field( default=False, description=("Generate a natural-language summary describing the chart."), ) chart2code: bool = Field( default=False, description=("Generate Python code that recreates the chart."), ) output_format: ChartExtractionOutputFormat = Field( default=ChartExtractionOutputFormat.GRANITE_VISION_CHARTS, description=( "Parsing contract for the model's raw output. " "Each value selects the set of post-processors applied to the " "per-prompt responses. Add a new enum member and handler when " "integrating a model whose output shape differs from the existing ones." ), ) use_natural_language_prompts: bool = Field( default=False, description=( "Replace special-token prompts (, , ) " "with natural-language equivalents. Enable this when the deployed model does " "not have the fine-tuned special tokens (e.g. a GGUF quantization served via " "an API endpoint that lacks HF fine-tuning)." ), ) @model_validator(mode="after") def _at_least_one_output(self) -> Self: if not (self.chart2csv or self.chart2summary or self.chart2code): raise ValueError( "At least one of chart2csv, chart2summary, or chart2code must be True." ) if self.output_format == ChartExtractionOutputFormat.GRANITE_VISION_CHART2CSV: if not self.chart2csv or self.chart2summary or self.chart2code: raise ValueError( "The granite_vision_v3_3 Chart2CSV preset supports CSV output only." ) return self def active_prompts(self) -> list[str]: """Return the ordered list of special-token prompts to send for each chart image.""" prompts: list[str] = [] if self.chart2csv: prompts.append("") if self.chart2summary: prompts.append("") if self.chart2code: prompts.append("") return prompts # --------------------------------------------------------------------------- # Register chart extraction presets alongside the options class so that # from_preset() works even when pipeline_options has not been imported yet. # --------------------------------------------------------------------------- from docling.datamodel import stage_model_specs as _stage_model_specs # noqa: E402 ChartExtractionVlmEngineOptions.register_preset( _stage_model_specs.CHART_EXTRACTION_GRANITE_VISION ) ChartExtractionVlmEngineOptions.register_preset( _stage_model_specs.CHART_EXTRACTION_GRANITE_VISION_V4 ) ChartExtractionVlmEngineOptions.register_preset( _stage_model_specs.CHART_EXTRACTION_GRANITE_VISION_V4_MLX ) # --------------------------------------------------------------------------- # Deprecated shims — kept for backwards compatibility only # --------------------------------------------------------------------------- class _ChartExtractionModelKindMeta(type): """Metaclass that makes ChartExtractionModelKind iterable and subscriptable so that legacy code using ``list(ChartExtractionModelKind)`` or ``ChartExtractionModelKind["GRANITE_VISION"]`` does not raise TypeError. """ def __iter__(cls) -> Iterator[Any]: return iter(cls._members.values()) def __getitem__(cls, item: str) -> Any: return cls._members[item] def __contains__(cls, item: object) -> bool: return item in cls._members.values() class ChartExtractionModelKind(metaclass=_ChartExtractionModelKindMeta): """Deprecated — use ``ChartExtractionVlmEngineOptions.from_preset()`` instead. .. deprecated:: Use :meth:`ChartExtractionVlmEngineOptions.from_preset` with ``'granite_vision_v4'`` instead. ``GRANITE_VISION`` retains the current Granite Vision 4.1 default. """ GRANITE_VISION = "granite-vision" GRANITE_VISION_V4 = "granite-vision-v4" # Instances exposed as attributes so that `.value` works like an enum member class _Member: def __init__(self, name: str, value: str) -> None: self.name = name self.value = value def __repr__(self) -> str: return f"" def __str__(self) -> str: return self.value def __eq__(self, other: object) -> bool: if isinstance(other, str): return self.value == other if isinstance(other, ChartExtractionModelKind._Member): return self.value == other.value return NotImplemented def __hash__(self) -> int: return hash(self.value) _members: ClassVar[Dict[str, "_ChartExtractionModelKindMeta"]] = {} # Map legacy enum values to the registered preset IDs. _PRESET_MAP: ClassVar[Dict[str, str]] = { "granite-vision": "granite_vision_v4", "granite-vision-v4": "granite_vision_v4", } # Populate _members after the class body is fully defined for _name, _val in [ ("GRANITE_VISION", ChartExtractionModelKind.GRANITE_VISION), ("GRANITE_VISION_V4", ChartExtractionModelKind.GRANITE_VISION_V4), ]: _member = ChartExtractionModelKind._Member(_name, _val) ChartExtractionModelKind._members[_name] = _member # type: ignore[assignment] setattr(ChartExtractionModelKind, _name, _member) class ChartExtractionModelOptions(ChartExtractionVlmEngineOptions): """Deprecated — use ``ChartExtractionVlmEngineOptions`` instead. For backwards compatibility, instantiating this class emits a ``DeprecationWarning`` and returns a fully functional ``ChartExtractionVlmEngineOptions`` configured from the requested preset. The default remains ``granite_vision_v4``. """ kind: ClassVar[Literal["chart_extraction"]] = "chart_extraction" # type: ignore[assignment] # The old 'model' field is accepted but ignored after mapping to a preset. model: Optional[str] = Field( default=None, description=( "Deprecated. Use ChartExtractionVlmEngineOptions.from_preset() instead." ), exclude=True, ) def __init__(self, **data: Any) -> None: warnings.warn( "ChartExtractionModelOptions is deprecated. " "Use ChartExtractionVlmEngineOptions.from_preset('granite_vision_v4') instead.", DeprecationWarning, stacklevel=2, ) # Resolve preset from the legacy 'model' field if provided. model_val = data.pop("model", None) if model_val is not None: model_str = str(model_val) if model_str not in ChartExtractionModelKind._PRESET_MAP: raise ValueError( f"Unknown model {model_str!r}. " f"Valid values: {list(ChartExtractionModelKind._PRESET_MAP)}" ) # Bootstrap from the preset so model_spec and engine_options are populated, # then allow the caller's remaining kwargs (chart2csv, etc.) to override. preset_instance = ChartExtractionVlmEngineOptions.from_preset( ChartExtractionModelKind._PRESET_MAP.get( str(model_val), "granite_vision_v4" ) ) merged = {**preset_instance.model_dump(), **data} super().__init__(**merged)