keras-team/keras · error · TypeError

All values in column x_col={x_col} must be strings.

Error message

All values in column x_col={x_col} must be strings.

What it means

The column named by x_col must contain only strings (file paths or filenames). Any non-string (Path object is fine only if converted, numbers, NaN) raises TypeError.

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)

Solutions

  1. Convert paths: df[x_col] = df[x_col].astype(str)
  2. Drop rows with missing filenames: df = df.dropna(subset=[x_col])
  3. 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

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: 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


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/841b8c1ca654b4d9. Report an issue: GitHub.