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
Raised by `DataFrameIterator` when `class_mode` requires labels but `y_col` is not a list. When using categorical/binary/etc. class modes with a pandas DataFrame, `y_col` must be a list of column names. Wrap the column name(s) in a list.
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: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- All values in column x_col=
- Column weight_col= must be numeric.
- Expect string, list or tuple but found
- If class_mode=" ", y_col=" " column values must be strings.
- If class_mode=" ", y_col=" " column values must be type…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/86d485fbb036eecb.
Report an issue: GitHub.
Appendix: 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)