{"record":{"id":"0fe7313fc8d29d35","repo":"docling-project/docling","slug":"label-must-be-either-code-or-formula","errorCode":null,"errorMessage":"Label must be either code or formula","messagePattern":"Label must be either code or formula","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"docling/models/stages/code_formula/code_formula_model.py","lineNumber":237,"sourceCode":"        label : str\n            The type of input, either 'code' or 'formula'.\n\n        Returns\n        -------\n        str\n            The constructed prompt including necessary tokens and query.\n\n        Raises\n        ------\n        NotImplementedError\n            If the label is not 'code' or 'formula'.\n        \"\"\"\n        if label == \"code\":\n            query = \"<code>\"\n        elif label == \"formula\":\n            query = \"<formula>\"\n        else:\n            raise NotImplementedError(\"Label must be either code or formula\")\n\n        messages = [\n            {\n                \"role\": \"user\",\n                \"content\": [{\"type\": \"image\"}, {\"type\": \"text\", \"text\": query}],\n            },\n        ]\n\n        prompt = self._processor.apply_chat_template(\n            messages, add_generation_prompt=True\n        )\n\n        return prompt\n\n    def _post_process(self, texts: list[str]) -> list[str]:\n        \"\"\"\n        Processes a list of text strings by truncating at '<end_of_utterance>' and\n        removing a predefined set of unwanted substrings.","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/docling-project/docling/blob/61d76f1ff3f8428065465889f7b4577da7df704c/docling/models/stages/code_formula/code_formula_model.py#L219-L255","documentation":"The code/formula recognition model maps an element label to a special query token: only the labels 'code' and 'formula' are valid, producing '<code>' or '<formula>'. Any other label reaches the else branch and raises NotImplementedError. It is a programming-contract error: the stage is only designed to process CodeItem and FormulaItem elements.","triggerScenarios":"Calling _get_query (or feeding the model a batch) with an element whose .label is neither exactly 'code' nor 'formula' — e.g. 'CodeItem', 'formula ' (case/whitespace), or a text label like 'paragraph' reaching this stage due to a filtering bug upstream.","commonSituations":"Custom pipelines that route all items through the code/formula stage without filtering by label; label strings that differ in case or use item-class names instead of the enum values; upgrading docling where label enum values changed.","solutions":["Filter the batch before the stage so only items with label 'code' or 'formula' are passed (the standard pipeline does this).","Check for exact lowercase strings; normalize labels (item.label.lower().strip()) if labels come from custom code.","If you genuinely need a new label handled, subclass or extend the model — do not pass unsupported labels."],"exampleFix":"# before\nfor el in element_batch:\n    query = self._get_query(el.item.label)  # raises for 'paragraph'\n\n# after\nfor el in element_batch:\n    if el.item.label not in (\"code\", \"formula\"):\n        continue\n    query = self._get_query(el.item.label)","handlingStrategy":"validation","validationCode":"SUPPORTED_LABELS = {\"code\", \"formula\"}\nbatch = [el for el in element_batch if el.item.label in SUPPORTED_LABELS]","typeGuard":"from typing import TypeGuard\n\ndef is_supported_label(label: str) -> TypeGuard[str]:\n    return label in (\"code\", \"formula\")","tryCatchPattern":"try:\n    query = self._get_query(item.label)\nexcept NotImplementedError:\n    _log.debug(\"Skipping unsupported label %r\", item.label)\n    return","preventionTips":["Filter batches by label before the stage, mirroring the standard pipeline.","Keep a unit test asserting only code/formula labels reach this model.","Treat NotImplementedError from this API as a caller bug — fix the filter, do not catch in production."],"tags":["code-formula","validation","label","not-implemented"],"backgroundTag":null,"analyzedSha":"61d76f1ff3f8428065465889f7b4577da7df704c","analyzedAt":"2026-08-14T23:53:18.727Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}