keras-team/keras · error · TypeError
Expect string, list or tuple but found {} in {} column
Error message
Expect string, list or tuple but found {} in {} column What it means
When filtering classes (classes=... argument), each y_col value must be a string, list, or tuple so membership in the class set can be tested. Any other Python type raises TypeError.
Source
Thrown at keras/src/legacy/preprocessing/image.py:882
for label in df[y_col]:
if isinstance(label, (list, tuple)):
labels.append([self.class_indices[lbl] for lbl in label])
else:
labels.append(self.class_indices[label])
return labels
@staticmethod
def _filter_classes(df, y_col, classes):
df = df.copy()
def remove_classes(labels, classes):
if isinstance(labels, (list, tuple)):
labels = [cls for cls in labels if cls in classes]
return labels or None
elif isinstance(labels, str):
return labels if labels in classes else None
else:
raise TypeError(
"Expect string, list or tuple "
"but found {} in {} column ".format(type(labels), y_col)
)
if classes:
# prepare for membership lookup
classes = list(collections.OrderedDict.fromkeys(classes).keys())
df[y_col] = df[y_col].apply(lambda x: remove_classes(x, classes))
else:
classes = set()
for v in df[y_col]:
if isinstance(v, (list, tuple)):
classes.update(v)
else:
classes.add(v)
classes = sorted(classes)
return df.dropna(subset=[y_col]), classes
View on GitHub (pinned to 7a34a03db6)
Solutions
- Make all y_col values strings (or lists of strings)
- Convert numerics: df[y_col] = df[y_col].astype(str)
Example fix
// before classes=['0','1']; df['label'] numeric // after df['label'] = df['label'].astype(str)
Defensive patterns
Strategy: type-guard
Validate before calling
assert df[y_col].map(lambda v: isinstance(v, (str, list, tuple))).all()
Type guard
def label_ok(v): return isinstance(v, (str, list, tuple))
Try / catch
try: flow_from_dataframe(..., classes=classes) except TypeError as e: if 'Expect string, list or tuple' in str(e): df[y_col] = df[y_col].astype(str)
Prevention
- Keep label columns string-typed whenever classes= is used
When it happens
Trigger: flow_from_dataframe(..., classes=[...]) with numeric or NaN label values in y_col.
Common situations: Passing classes for the first time after previously running with numeric labels.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- All values in column x_col={x_col} must be strings.
- If class_mode="{}", y_col="{}" column values must be strings
- If class_mode="{}", y_col="{}" column values must be type st
- Received an invalid value for `units`, expected a positive i
- adapt() expects an iterable that yields arrays or tensors wi
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/0d9174b983c77610.
Report an issue: GitHub.