mlflow/mlflow · error · MlflowException
Invalid data type: {data_type!r}
Error message
Invalid data type: {data_type!r} What it means
_enforce_type dispatches on the schema data type object (DataType, Array, Object, Map, AnyType). If the data_type argument is none of the recognized schema type classes, MLflow raises this error because it has no enforcement rule for it. This almost always means a raw Python type (like str or dict) or a foreign type object was passed where an mlflow.types schema type instance was expected.
Source
Thrown at mlflow/models/utils.py:1465
if not all(isinstance(k, str) for k in data):
raise MlflowException("Expected all keys in the map type data are string type.")
return {k: _enforce_type(v, map_type.value_type, required=required) for k, v in data.items()}
def _enforce_type(data: Any, data_type: DataType | Array | Object | Map, required=True):
if isinstance(data_type, DataType):
return _enforce_datatype(data, data_type, required=required)
if isinstance(data_type, Array):
return _enforce_array(data, data_type, required=required)
if isinstance(data_type, Object):
return _enforce_object(data, data_type, required=required)
if isinstance(data_type, Map):
return _enforce_map(data, data_type, required=required)
if isinstance(data_type, AnyType):
return data
raise MlflowException(f"Invalid data type: {data_type!r}")
def validate_schema(data: PyFuncInput, expected_schema: Schema) -> None:
"""
Validate that the input data has the expected schema.
Args:
data: Input data to be validated. Supported types are:
- pandas.DataFrame
- pandas.Series
- numpy.ndarray
- scipy.sparse.csc_matrix
- scipy.sparse.csr_matrix
- List[Any]
- Dict[str, Any]
- str
View on GitHub (pinned to 6a27f2decc)
Solutions
- Use mlflow.types.schema types: Column('x', DataType.from_python_type(str)) or infer_signature to generate the schema
- Verify every Column/ParamSpec/TensorSpec data type is an instance of mlflow.types.DataType, Array, Object, or Map
- Do not pass bare Python types into Schema construction; convert with DataType.from_python_type
- Check mlflow version compatibility if the schema came from a saved model artifact
Example fix
// before
schema = Schema([Column("x", dict)])
// after
from mlflow.types.schema import Schema, Column, DataType
schema = Schema([Column("x", DataType.string)]) Defensive patterns
Strategy: type-guard
Validate before calling
from mlflow.types.schema import DataType, Array, Object, Map
def check_schema_types(schema):
for col in schema.columns:
if not isinstance(col.type, (DataType, Array, Object, Map)):
raise TypeError(f"column {col.name!r} has invalid type {type(col.type).__name__}") Type guard
def is_valid_mlflow_type(t) -> bool:
from mlflow.types.schema import DataType, Array, Object, Map
return isinstance(t, (DataType, Array, Object, Map)) Try / catch
from mlflow.exceptions import MlflowException
try:
validate_schema(data, schema)
except MlflowException as e:
if "Invalid data type" in str(e):
raise ValueError("Rebuild schema with mlflow.types.schema types or infer_signature") from e Prevention
- Never pass bare Python types into Schema; use DataType / DataType.from_python_type
- Generate signatures with infer_signature rather than manual construction
- Round-trip test schema save/load when persisting models
- Pin and align mlflow versions between training and serving environments
When it happens
Trigger: Constructing a Schema or calling _enforce_type / _enforce_col_schema paths with a plain Python type (e.g., str, dict) or an instance of a non-MLflow class instead of mlflow.types.DataType/Array/Object/Map instances.
Common situations: Building a Schema manually with Column('x', dict) instead of Column('x', DataType); mixing custom type wrappers into a Schema; version drift where a schema was serialized/deserialized incorrectly; passing a type class rather than an instance of mlflow.types schema types.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Input DataFrame must contain a 'prompt' column. Got columns:
- Error when coercing {value} to {cls.__name__}: {e}
- Unsupported value type '{value_type}', expected instance of
- EXPECTED_TYPE_MESSAGE.format(arg_name="value_type", passed_t
- Unsupported data type.
AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29).
Data as JSON: /api/errors/3cfd8d2fbdfdb9c7.
Report an issue: GitHub.