{"record":{"id":"b4a11330fcf8d4bf","repo":"mlflow/mlflow","slug":"expected-1d-array-got-array-with-shape-col-shape","errorCode":null,"errorMessage":"Expected 1d array, got array with shape {col.shape}","messagePattern":"Expected 1d array, got array with shape (.+?)","errorType":"validation","errorClass":"MlflowException","httpStatus":null,"severity":"error","filePath":"mlflow/types/utils.py","lineNumber":512,"sourceCode":"\ndef _is_none_or_nan(x):\n    if isinstance(x, float):\n        return np.isnan(x)\n    # NB: We can't use pd.isna() because the input can be a series.\n    return x is None or x is pd.NA or x is pd.NaT\n\n\ndef _infer_required(col) -> bool:\n    if isinstance(col, (list, pd.Series)):\n        return not any(_is_none_or_nan(x) for x in col)\n    return not _is_none_or_nan(col)\n\n\ndef _infer_pandas_column(col: pd.Series) -> DataType:\n    if not isinstance(col, pd.Series):\n        raise TypeError(f\"Expected pandas.Series, got '{type(col)}'.\")\n    if len(col.values.shape) > 1:\n        raise MlflowException(f\"Expected 1d array, got array with shape {col.shape}\")\n\n    if col.dtype.kind == \"O\":\n        col = col.infer_objects()\n    if col.dtype.kind == \"O\":\n        try:\n            # We convert pandas Series into list and infer the schema.\n            # The real schema for internal field should be the Array's dtype\n            arr_type = _infer_colspec_type(col.to_list())\n            return arr_type.dtype\n        except Exception as e:\n            # For backwards compatibility, we fall back to string\n            # if the provided array is of string type\n            if pd.api.types.is_string_dtype(col):\n                return DataType.string\n            raise MlflowException(f\"Failed to infer schema for pandas.Series {col}. Error: {e}\")\n    else:\n        # NB: The following works for numpy types as well as pandas extension types.\n        return _infer_numpy_dtype(col.dtype)","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/types/utils.py#L494-L530","documentation":"MLflow's schema inference (_infer_pandas_column) requires each pandas DataFrame column to be a 1-dimensional pd.Series. This error is thrown when the underlying values of a column have more than one dimension (e.g. a column of arrays/vectors), because MLflow's Schema types (DataType, TensorSpec) cannot represent a 2D object column. It protects against silently inferring a wrong model signature.","triggerScenarios":"Calling mlflow.models.infer_signature(model, pandas_df) (or _infer_schema directly) where a DataFrame column holds multi-dimensional data, e.g. df['x'] = np.random.rand(10, 3, 3) (each cell a 2D array) so col.values.shape is (n, 3, 3). Also happens when a column was built from lists of unequal arrays that pandas stored as object arrays with ndim > 1.","commonSituations":"Passing DataFrames with image arrays, embedding matrices, or multi-dim feature tensors as columns; converting numpy arrays with np.stack incorrectly; pandas columns created from lists of numpy arrays that end up rectangular (shape (n, m)).","solutions":["Reshape each column to be strictly 1D: flatten or keep per-cell values as ragged lists so col.values.shape has ndim 1.","For tensor/multi-dim data, use mlflow.models.infer_signature with a numpy ndarray input instead of a pandas DataFrame, so a TensorSpec is inferred instead.","Convert per-cell arrays into separate scalar columns, or into lists (object dtype with 1D shape).","Cast the offending column explicitly: df['x'] = df['x'].apply(lambda v: list(v)) and re-run inference."],"exampleFix":"// before\ndf = pd.DataFrame({'x': np.random.rand(10, 3, 3)})  # column values shape (10,3,3)\nsig = infer_signature(df, model)\n// after\ndf = pd.DataFrame({'x': [np.random.rand(3, 3).tolist() for _ in range(10)]})  # object col of lists\n# or pass ndarray input to get a TensorSpec:\nsig = infer_signature(np.random.rand(10, 3, 3), model)","handlingStrategy":"validation","validationCode":"import pandas as pd\ndef check_1d_columns(df: pd.DataFrame):\n    bad = [name for name in df.columns if getattr(df[name].values, 'ndim', 1) > 1]\n    if bad:\n        raise ValueError(f'Columns with >1D values: {bad}')","typeGuard":"def is_1d_series(col) -> bool:\n    import pandas as pd\n    return isinstance(col, pd.Series) and col.values.ndim == 1","tryCatchPattern":null,"preventionTips":["Keep DataFrame columns scalar or ragged-list (object) typed; never store rectangular arrays as column values.","Use ndarray inputs (not DataFrames) for tensor data so TensorSpec is inferred.","Run signature inference early in training, not at deployment time.","Add a column-ndim assertion in data-preparation tests."],"tags":["mlflow","schema-inference","pandas"],"backgroundTag":"invalid-array-dimension","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}