docling-project/docling · error · NotImplementedError
Label must be either code or formula
Error message
Label must be either code or formula
What it means
The VLM-based code/formula model builds its prompt token from an element label; only 'code' and 'formula' are accepted, returning '<code>' or '<formula>'. Any other label raises NotImplementedError, signalling the caller routed an unsupported element type into this stage. It is a strict contract on the input labels, not a runtime/model failure.
Source
Thrown at docling/models/stages/code_formula/code_formula_vlm_model.py:152
def _get_prompt(self, label: str) -> str:
"""Construct the prompt for the model based on the element type.
Args:
label: The type of input, either 'code' or 'formula'
Returns:
The prompt string
Raises:
NotImplementedError: If the label is not 'code' or 'formula'
"""
if label == "code":
return "<code>"
elif label == "formula":
return "<formula>"
else:
raise NotImplementedError("Label must be either code or formula")
def _extract_code_language(self, input_string: str) -> Tuple[str, Optional[str]]:
"""Extract programming language from the beginning of a string.
Checks if the input string starts with a pattern of the form
``<_some_language_>``. If it does, extracts the language string.
Args:
input_string: The input string, which may start with ``<_language_>``
Returns:
Tuple of (remainder, language) where:
- remainder is the string after the language tag (or original if no match)
- language is the extracted language if found, otherwise None
"""
pattern = r"^<_([^_>]+)_>\s*(.*)"
match = re.match(pattern, input_string, flags=re.DOTALL)
if match:View on GitHub (pinned to 61d76f1ff3)
Solutions
- Filter element batches to labels 'code' and 'formula' before invoking the model (mirror what the standard pipeline stage does).
- Normalize labels: assert/convert item.label to the expected lowercase enum value before building the prompt.
- Add a unit check in your pipeline that the set of labels reaching this model is a subset of {'code','formula'}.
Example fix
# before
query = self._get_query(item.label) # raises NotImplementedError for 'picture'
# after
if item.label in ("code", "formula"):
query = self._get_query(item.label)
else:
_log.debug(f"Skipping item with unsupported label {item.label!r}")
return Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED = {"code", "formula"}
items = [i for i in items if i.label in SUPPORTED] Type guard
def is_code_or_formula(label: str) -> bool:
return label in ("code", "formula") Prevention
- Apply the same label filter as error 261 — never route arbitrary labels into the VLM stage.
- Normalize custom labels to docling enum values at ingestion time.
- Add pipeline assertions that label sets are subsets of {'code','formula'} before dispatch.
When it happens
Trigger: Passing elements to GraniteVlmCodeFormulaModel whose label is anything other than the exact strings 'code' or 'formula' — for example 'title', 'list_item', or a mistyped/uppercased label like 'Code'.
Common situations: Custom pipelines that skip the label filter; label enum mismatches after a docling upgrade; reusing items from another backend whose labels were never normalized to docling's enum values.
Related errors
- Label must be either code or formula
- Engine not initialized
- ThreadedDoclingParseDocumentBackend only supports iter_pages
- The parameters vlm_pipeline_model, vlm_pipeline_model_local
- Cannot specify both vlm_pipeline_preset and vlm_pipeline_cus
AI-assisted analysis of docling-project/docling@61d76f1ff3 (2026-08-14).
Data as JSON: /api/errors/a59cd747ddc2de98.
Report an issue: GitHub.