huggingface/transformers · error · TypeError

Training label {label} is not a string

Error message

Training label {label} is not a string

What it means

The third XNLI train-row check: the label (column 2, with 'contradictory' mapped to 'contradiction') must be a str. Because the mapping expression 'contradiction' if line[2] == 'contradictory' else line[2] passes through whatever is in the column, a non-string there propagates and this TypeError fires, indicating a malformed label column rather than an invalid label value.

Source

Thrown at src/transformers/data/processors/xnli.py:53

    def get_train_examples(self, data_dir):
        """See base class."""
        lg = self.language if self.train_language is None else self.train_language
        lines = self._read_tsv(os.path.join(data_dir, f"XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv"))
        examples = []
        for i, line in enumerate(lines):
            if i == 0:
                continue
            guid = f"train-{i}"
            text_a = line[0]
            text_b = line[1]
            label = "contradiction" if line[2] == "contradictory" else line[2]
            if not isinstance(text_a, str):
                raise TypeError(f"Training input {text_a} is not a string")
            if not isinstance(text_b, str):
                raise TypeError(f"Training input {text_b} is not a string")
            if not isinstance(label, str):
                raise TypeError(f"Training label {label} is not a string")
            examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
        return examples

    def get_test_examples(self, data_dir):
        """See base class."""
        lines = self._read_tsv(os.path.join(data_dir, "XNLI-1.0/xnli.test.tsv"))
        examples = []
        for i, line in enumerate(lines):
            if i == 0:
                continue
            language = line[0]
            if language != self.language:
                continue
            guid = f"test-{i}"
            text_a = line[6]
            text_b = line[7]
            label = line[1]
            if not isinstance(text_a, str):

View on GitHub (pinned to a597f97485)

Solutions

  1. Check the label column of the failing row and the file's column layout against the official XNLI-MT format.
  2. Re-download the dataset if any corruption is suspected.
  3. Pre-validate that line[2] is a non-empty string for every data row before calling get_train_examples.

Example fix

# before
examples = processor.get_train_examples(data_dir)

# after: confirm labels parse cleanly first
import csv
with open(train_tsv_path, encoding="utf-8") as f:
    rows = list(csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE))
labels = {r[2] for r in rows[1:]}
assert labels <= {"contradictory", "contradiction", "entailment", "neutral"}, labels
examples = processor.get_train_examples(data_dir)
Defensive patterns

Strategy: validation

Validate before calling

with open(train_tsv_path, encoding="utf-8") as f:
    rows = list(csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE))[1:]
labels = {r[2] for r in rows if len(r) > 2}
assert labels <= {"contradictory", "contradiction", "entailment", "neutral"}, f"bad labels: {labels}"

Prevention

When it happens

Trigger: get_train_examples(data_dir) on a TSV whose label column is empty or non-text for some row; files where column order differs from the expected (text, text, label) layout.

Common situations: Header/row column drift after manual editing; using a different NLI TSV that happens to have 3+ columns but a different order; truncated rows from a bad download.

Related errors


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