{"record":{"id":"841b8c1ca654b4d9","repo":"keras-team/keras","slug":"all-values-in-column-x-col-x-col-must-be-strings","errorCode":null,"errorMessage":"All values in column x_col={x_col} must be strings.","messagePattern":"All values in column x_col=(.+?) must be strings\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":812,"sourceCode":"    def _check_params(self, df, x_col, y_col, weight_col, classes):\n        # check class mode is one of the currently supported\n        if self.class_mode not in self.allowed_class_modes:\n            raise ValueError(\n                \"Invalid class_mode: {}; expected one of: {}\".format(\n                    self.class_mode, self.allowed_class_modes\n                )\n            )\n        # check that y_col has several column names if class_mode is\n        # multi_output\n        if (self.class_mode == \"multi_output\") and not isinstance(y_col, list):\n            raise TypeError(\n                'If class_mode=\"{}\", y_col must be a list. Received {}.'.format(\n                    self.class_mode, type(y_col).__name__\n                )\n            )\n        # check that filenames/filepaths column values are all strings\n        if not all(df[x_col].apply(lambda x: isinstance(x, str))):\n            raise TypeError(\n                f\"All values in column x_col={x_col} must be strings.\"\n            )\n        # check labels are string if class_mode is binary or sparse\n        if self.class_mode in {\"binary\", \"sparse\"}:\n            if not all(df[y_col].apply(lambda x: isinstance(x, str))):\n                raise TypeError(\n                    'If class_mode=\"{}\", y_col=\"{}\" column '\n                    \"values must be strings.\".format(self.class_mode, y_col)\n                )\n        # check that if binary there are only 2 different classes\n        if self.class_mode == \"binary\":\n            if classes:\n                classes = set(classes)\n                if len(classes) != 2:\n                    raise ValueError(\n                        'If class_mode=\"binary\" there must be 2 '\n                        \"classes. {} class/es were given.\".format(len(classes))\n                    )","sourceCodeStart":794,"sourceCodeEnd":830,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L794-L830","documentation":"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.","triggerScenarios":"flow_from_dataframe with a column holding pathlib.Path objects, numeric IDs, or NaN entries.","commonSituations":"Building the DataFrame with os.scandir() Path objects; CSV import producing NaN for missing rows.","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"],"exampleFix":"// before\ndf['file'] = list(paths_dir.glob('*.jpg'))  # Path objects\n// after\ndf['file'] = [str(p) for p in paths_dir.glob('*.jpg')]\n","handlingStrategy":"validation","validationCode":"assert df[x_col].map(lambda v: isinstance(v, str)).all(), df[x_col][~df[x_col].map(lambda v: isinstance(v, str))].head()","typeGuard":"def all_str(col): return col.map(lambda v: isinstance(v, str)).all()","tryCatchPattern":"try: flow_from_dataframe(...)\nexcept TypeError as e: if 'must be strings' in str(e): df[x_col] = df[x_col].astype(str)","preventionTips":["Cast path columns with astype(str) at DataFrame build time","Drop NaN filename rows early"],"tags":["keras","dataframe","type-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}