huggingface/transformers · error · TypeError
Training input {text_a} is not a string
Error message
Training input {text_a} is not a string What it means
While building XNLI training examples from the multilingual TSV, the processor asserts that column 0 of each row (text_a) is a str. The CSV reader normally yields strings, so this TypeError signals a malformed row: wrong delimiter, a file without a header as expected, QUOTE_NONE artifacts, or a row whose columns are not plain text. The check exists to fail loudly on corrupt data instead of producing broken InputExamples.
Source
Thrown at src/transformers/data/processors/xnli.py:49
def __init__(self, language, train_language=None):
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}"View on GitHub (pinned to a597f97485)
Solutions
- Re-download the XNLI-MT-1.0 archive and verify multinli.train.{language}.tsv opens as tab-separated text with a header row.
- Sanity-parse the file yourself with csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE) and inspect the offending row index.
- Confirm data_dir contains XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv exactly as the processor expects.
Example fix
# before
examples = processor.get_train_examples(data_dir) # corrupt TSV
# after: validate the file first
import csv
with open(f"{data_dir}/XNLI-MT-1.0/multinli/multinli.train.en.tsv", encoding="utf-8") as f:
rows = list(csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE))
assert all(isinstance(r[0], str) for r in rows[1:])
examples = processor.get_train_examples(data_dir) Defensive patterns
Strategy: validation
Validate before calling
import csv
path = f"{data_dir}/XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv"
with open(path, encoding="utf-8") as f:
rows = list(csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE))[1:]
assert rows and all(isinstance(r[0], str) and r[0] for r in rows), "malformed text_a column" Try / catch
try:
examples = processor.get_train_examples(data_dir)
except TypeError as e:
if "not a string" in str(e):
raise ValueError(f"Corrupt XNLI train TSV under {data_dir}; re-download XNLI-MT-1.0") from e
raise Prevention
- Download XNLI archives from official sources and verify file sizes.
- Pre-parse the TSV with the same csv settings the processor uses before running.
- Never hand-edit the raw TSVs; generate a cleaned copy instead.
When it happens
Trigger: get_train_examples(data_dir) on a multinli.train.{lg}.tsv that is corrupted, uses a different delimiter/quoting, or where line[0] is empty/None-like; pointing data_dir at the wrong directory so a different file layout is read.
Common situations: Downloading XNLI-MT-1.0 manually and getting HTML or partial content; locale/encoding issues producing bytes; editing the TSV in a spreadsheet that changed quoting.
Related errors
- Training input {text_b} 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/c13335ea01f6fe86.
Report an issue: GitHub.