huggingface/transformers · error · ValueError

Text and labels have mismatched lengths {len(texts_or_text_a

Error message

Text and labels have mismatched lengths {len(texts_or_text_and_labels)} and {len(labels)}

What it means

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.

Source

Thrown at src/transformers/data/processors/utils.py:197

        ids = []
        for i, line in enumerate(lines):
            texts.append(line[column_text])
            labels.append(line[column_label])
            if column_id is not None:
                ids.append(line[column_id])
            else:
                guid = f"{split_name}-{i}" if split_name else str(i)
                ids.append(guid)

        return self.add_examples(
            texts, labels, ids, overwrite_labels=overwrite_labels, overwrite_examples=overwrite_examples
        )

    def add_examples(
        self, texts_or_text_and_labels, labels=None, ids=None, overwrite_labels=False, overwrite_examples=False
    ):
        if labels is not None and len(texts_or_text_and_labels) != len(labels):
            raise ValueError(
                f"Text and labels have mismatched lengths {len(texts_or_text_and_labels)} and {len(labels)}"
            )
        if ids is not None and len(texts_or_text_and_labels) != len(ids):
            raise ValueError(f"Text and ids have mismatched lengths {len(texts_or_text_and_labels)} and {len(ids)}")
        if ids is None:
            ids = [None] * len(texts_or_text_and_labels)
        if labels is None:
            labels = [None] * len(texts_or_text_and_labels)
        examples = []
        added_labels = set()
        for text_or_text_and_label, label, guid in zip(texts_or_text_and_labels, labels, ids):
            if isinstance(text_or_text_and_label, (tuple, list)) and label is None:
                text, label = text_or_text_and_label
            else:
                text = text_or_text_and_label
            added_labels.add(label)
            examples.append(InputExample(guid=guid, text_a=text, text_b=None, label=label))

View on GitHub (pinned to a597f97485)

Solutions

  1. Check len(texts) == len(labels) before calling add_examples and fix the construction of one of the lists.
  2. If each item already carries its label as (text, label) tuples, drop the separate labels argument entirely.
  3. Regenerate both lists from the same source iteration so they cannot drift apart.

Example fix

# before
featurizer.add_examples(texts, labels=labels)  # len mismatch

# after
assert len(texts) == len(labels), (len(texts), len(labels))
featurizer.add_examples(texts, labels=labels)
Defensive patterns

Strategy: validation

Validate before calling

if labels is not None:
    assert len(texts) == len(labels), f"texts={len(texts)} labels={len(labels)}"
featurizer.add_examples(texts, labels=labels)

Try / catch

try:
    featurizer.add_examples(texts, labels=labels)
except ValueError as e:
    if "mismatched lengths" in str(e):
        raise ValueError(f"Data pipeline length drift: {e}") from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/e186c01a38e55bdd. Report an issue: GitHub.