{"record":{"id":"e186c01a38e55bdd","repo":"huggingface/transformers","slug":"text-and-labels-have-mismatched-lengths-len-texts","errorCode":null,"errorMessage":"Text and labels have mismatched lengths {len(texts_or_text_and_labels)} and {len(labels)}","messagePattern":"Text and labels have mismatched lengths (.+?) and (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/data/processors/utils.py","lineNumber":197,"sourceCode":"        ids = []\n        for i, line in enumerate(lines):\n            texts.append(line[column_text])\n            labels.append(line[column_label])\n            if column_id is not None:\n                ids.append(line[column_id])\n            else:\n                guid = f\"{split_name}-{i}\" if split_name else str(i)\n                ids.append(guid)\n\n        return self.add_examples(\n            texts, labels, ids, overwrite_labels=overwrite_labels, overwrite_examples=overwrite_examples\n        )\n\n    def add_examples(\n        self, texts_or_text_and_labels, labels=None, ids=None, overwrite_labels=False, overwrite_examples=False\n    ):\n        if labels is not None and len(texts_or_text_and_labels) != len(labels):\n            raise ValueError(\n                f\"Text and labels have mismatched lengths {len(texts_or_text_and_labels)} and {len(labels)}\"\n            )\n        if ids is not None and len(texts_or_text_and_labels) != len(ids):\n            raise ValueError(f\"Text and ids have mismatched lengths {len(texts_or_text_and_labels)} and {len(ids)}\")\n        if ids is None:\n            ids = [None] * len(texts_or_text_and_labels)\n        if labels is None:\n            labels = [None] * len(texts_or_text_and_labels)\n        examples = []\n        added_labels = set()\n        for text_or_text_and_label, label, guid in zip(texts_or_text_and_labels, labels, ids):\n            if isinstance(text_or_text_and_label, (tuple, list)) and label is None:\n                text, label = text_or_text_and_label\n            else:\n                text = text_or_text_and_label\n            added_labels.add(label)\n            examples.append(InputExample(guid=guid, text_a=text, text_b=None, label=label))\n","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/data/processors/utils.py#L179-L215","documentation":"SeqClassificationFeaturizer.add_examples (in the legacy data/processors/utils.py featurizer API) requires that when a separate labels list is passed, it has exactly as many entries as texts_or_text_and_labels. The library throws this ValueError to prevent silent truncation, because the examples are built with zip() which would otherwise drop unpaired elements.","triggerScenarios":"Calling add_examples(texts, labels=labels) where len(labels) != len(texts); calling get_examples/add_examples with texts_or_text_and_labels as a list of (text, label) tuples AND also a labels list of a different length; passing ids/labels generated from a different split than the texts.","commonSituations":"Off-by-one when slicing datasets (e.g. texts[1:] but labels left unsliced); filtering rows of texts but forgetting to filter labels the same way; mixing per-example tuple input with a stale parallel labels list.","solutions":["Check len(texts) == len(labels) before calling add_examples and fix the construction of one of the lists.","If each item already carries its label as (text, label) tuples, drop the separate labels argument entirely.","Regenerate both lists from the same source iteration so they cannot drift apart."],"exampleFix":"# before\nfeaturizer.add_examples(texts, labels=labels)  # len mismatch\n\n# after\nassert len(texts) == len(labels), (len(texts), len(labels))\nfeaturizer.add_examples(texts, labels=labels)","handlingStrategy":"validation","validationCode":"if labels is not None:\n    assert len(texts) == len(labels), f\"texts={len(texts)} labels={len(labels)}\"\nfeaturizer.add_examples(texts, labels=labels)","typeGuard":null,"tryCatchPattern":"try:\n    featurizer.add_examples(texts, labels=labels)\nexcept ValueError as e:\n    if \"mismatched lengths\" in str(e):\n        raise ValueError(f\"Data pipeline length drift: {e}\") from e\n    raise","preventionTips":["Derive texts and labels in one pass over the same source so they stay aligned.","Assert equal lengths in your data loader before any transformers call.","Prefer (text, label) tuple input when the data naturally comes paired."],"tags":["data-processing","validation","length-mismatch"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}