huggingface/transformers · error · TypeError
Training input {text_b} is not a string
Error message
Training input {text_b} is not a string What it means
Same XNLI train-example construction as the text_a check, but asserting column 1 (text_b, the premise/second sentence) is a str. A non-string value means the TSV row does not have the expected shape - typically a mis-parsed or truncated row - and the processor refuses to build an InputExample from it.
Source
Thrown at src/transformers/data/processors/xnli.py:51
self.language = language
self.train_language = train_language
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]View on GitHub (pinned to a597f97485)
Solutions
- Inspect the failing row (the traceback loop index) in the raw TSV and fix or remove it.
- Re-download the XNLI-MT-1.0 data and diff file sizes against the official archive.
- Validate every row has at least 3 string columns before calling the processor.
Example fix
# before
examples = processor.get_train_examples(data_dir)
# after: pre-validate rows
import csv
with open(train_tsv_path, encoding="utf-8") as f:
rows = list(csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE))
bad = [i for i, r in enumerate(rows[1:], 1) if len(r) < 3 or not all(isinstance(c, str) for c in r[:3])]
assert not bad, f"bad rows: {bad}"
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:]
bad = [i for i, r in enumerate(rows) if len(r) < 3 or not all(isinstance(c, str) for c in r[:3])]
assert not bad, f"malformed rows: {bad[:5]}" Prevention
- Validate every row has at least 3 string columns before calling the processor.
- Keep the original archive checksum to detect corruption early.
- Watch for truncated final rows after interrupted downloads.
When it happens
Trigger: get_train_examples(data_dir) where a row's second column is missing or the row was split incorrectly due to quoting/delimiter drift; a partially downloaded or truncated TSV.
Common situations: Interrupted downloads leaving truncated files; files edited with different tab/quote settings; wrong language file variant with a different column layout.
Related errors
- Training input {text_a} is not a string
- Training label {label} is not a string
- Text and labels have mismatched lengths {len(texts_or_text_a
- Text and ids have mismatched lengths {len(texts_or_text_and_
- PUSH_TO_HUB_TOKEN is not set, cannot push results to the Hub
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/a4fe06ae1d0d691d.
Report an issue: GitHub.