keras-team/keras · error · TypeError
If class_mode="{}", y_col must be a list. Received {}.
Error message
If class_mode="{}", y_col must be a list. Received {}. What it means
With class_mode='multi_output', y_col must be a list of DataFrame column names so the iterator can yield a dict of targets per column. A single string is treated as one column and rejected.
Source
Thrown at keras/src/legacy/preprocessing/image.py:805
f"belonging to {num_classes} classes."
)
self._filepaths = [
os.path.join(self.directory, fname) for fname in self.filenames
]
super().__init__(self.samples, batch_size, shuffle, seed)
def _check_params(self, df, x_col, y_col, weight_col, classes):
# check class mode is one of the currently supported
if self.class_mode not in self.allowed_class_modes:
raise ValueError(
"Invalid class_mode: {}; expected one of: {}".format(
self.class_mode, self.allowed_class_modes
)
)
# check that y_col has several column names if class_mode is
# multi_output
if (self.class_mode == "multi_output") and not isinstance(y_col, list):
raise TypeError(
'If class_mode="{}", y_col must be a list. Received {}.'.format(
self.class_mode, type(y_col).__name__
)
)
# check that filenames/filepaths column values are all strings
if not all(df[x_col].apply(lambda x: isinstance(x, str))):
raise TypeError(
f"All values in column x_col={x_col} must be strings."
)
# check labels are string if class_mode is binary or sparse
if self.class_mode in {"binary", "sparse"}:
if not all(df[y_col].apply(lambda x: isinstance(x, str))):
raise TypeError(
'If class_mode="{}", y_col="{}" column '
"values must be strings.".format(self.class_mode, y_col)
)
# check that if binary there are only 2 different classes
if self.class_mode == "binary":View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass y_col as a list: y_col=['price','category']
- Ensure each named column exists in the DataFrame
Example fix
// before y_col='price' // after y_col=['price', 'category']
Defensive patterns
Strategy: type-guard
Validate before calling
if class_mode == 'multi_output': assert isinstance(y_col, list)
Type guard
def is_list(v): return isinstance(v, list)
Try / catch
try: flow_from_dataframe(..., y_col=y_col) except TypeError as e: if 'must be a list' in str(e): y_col = [y_col]
Prevention
- Always wrap multi-output targets in lists
When it happens
Trigger: flow_from_dataframe(..., class_mode='multi_output', y_col='price').
Common situations: Migrating from single-output code and forgetting to wrap y_col in a list.
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
- Column weight_col={weight_col} must be numeric.
- Expect string, list or tuple but found {} in {} column
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/86d485fbb036eecb.
Report an issue: GitHub.