keras-team/keras · error · TypeError
All values in column x_col=
Error message
All values in column x_col={x_col} must be strings. What it means
Raised by `DataFrameIterator` when the columns listed in `x_col` contain non-string values. All values in `x_col` columns must be strings (file paths). Clean the DataFrame so every `x_col` entry is a string.
Solutions
- Convert paths: df[x_col] = df[x_col].astype(str)
- Drop rows with missing filenames: df = df.dropna(subset=[x_col])
- Store plain string paths in the column
Example fix
// before
df['file'] = list(paths_dir.glob('*.jpg')) # Path objects
// after
df['file'] = [str(p) for p in paths_dir.glob('*.jpg')]
Defensive patterns
Strategy: validation
Validate before calling
assert df[x_col].map(lambda v: isinstance(v, str)).all(), df[x_col][~df[x_col].map(lambda v: isinstance(v, str))].head()
Type guard
def all_str(col): return col.map(lambda v: isinstance(v, str)).all()
Try / catch
try: flow_from_dataframe(...) except TypeError as e: if 'must be strings' in str(e): df[x_col] = df[x_col].astype(str)
Prevention
- Cast path columns with astype(str) at DataFrame build time
- Drop NaN filename rows early
When it happens
Trigger: flow_from_dataframe with a column holding pathlib.Path objects, numeric IDs, or NaN entries.
Common situations: Building the DataFrame with os.scandir() Path objects; CSV import producing NaN for missing rows.
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
- 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…
- adapt() expects an iterable that yields arrays or tensors…
- Column weight_col= must be numeric.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/841b8c1ca654b4d9.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/legacy/preprocessing/image.py:812
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":
if classes:
classes = set(classes)
if len(classes) != 2:
raise ValueError(
'If class_mode="binary" there must be 2 '
"classes. {} class/es were given.".format(len(classes))
)View on GitHub (pinned to 7a34a03db6)