mlflow/mlflow · error · MlflowException

The dictionary values are not all numpy.ndarray.

Error message

The dictionary values are not all numpy.ndarray.

What it means

For dict inputs, if some values are numpy arrays then ALL values must be numpy arrays — this keeps the input homogeneous so MLflow can convert it to a consistent columnar format. The validator raises this error when the values mix np.ndarray with non-ndarray values (lists, strings, scalars).

Source

Thrown at mlflow/types/utils.py:646

        return isinstance(x, pyspark.sql.connect.dataframe.DataFrame)
    except ImportError:
        return False


def _validate_input_dictionary_contains_only_strings_and_lists_of_strings(data) -> None:
    # isinstance(True, int) is True
    invalid_keys = [
        key for key in data.keys() if not isinstance(key, (str, int)) or isinstance(key, bool)
    ]
    if invalid_keys:
        raise MlflowException(
            f"The dictionary keys are not all strings or indexes. Invalid keys: {invalid_keys}"
        )
    if any(isinstance(value, np.ndarray) for value in data.values()) and not all(
        isinstance(value, np.ndarray) for value in data.values()
    ):
        raise MlflowException("The dictionary values are not all numpy.ndarray.")

    invalid_values = [
        key
        for key, value in data.items()
        if (isinstance(value, list) and not all(isinstance(item, (str, bytes)) for item in value))
        or (not isinstance(value, (np.ndarray, list, str, bytes)))
    ]
    if invalid_values:
        raise MlflowException.invalid_parameter_value(
            "Invalid values in dictionary. If passing a dictionary containing strings, all "
            "values must be either strings or lists of strings. If passing a dictionary containing "
            "numeric values, the data must be enclosed in a numpy.ndarray. The following keys "
            f"in the input dictionary are invalid: {invalid_values}",
        )


def _is_list_str(type_hint: Any) -> bool:
    return type_hint in [

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Wrap every value in np.array(...): {k: np.asarray(v) for k, v in data.items()}.
  2. Alternatively convert all values to plain lists so none is an ndarray (then the list-of-strings rules apply).
  3. Ensure consistent value types by constructing the input from a single source (e.g. build a DataFrame or a dict comprehension over the same data).
  4. Check each value's type before predict with a guard (see validationCode).

Example fix

// before
model.predict({'a': np.array([1, 2]), 'b': ['x', 'y']})
// after
model.predict({'a': np.array([1, 2]), 'b': np.array(['x', 'y'])})
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
def check_homogeneous_values(data: dict):
    vals = list(data.values())
    if any(isinstance(v, np.ndarray) for v in vals) and not all(isinstance(v, np.ndarray) for v in vals):
        raise ValueError('All dict values must be numpy.ndarray when any is')

Type guard

def values_homogeneous(data: dict) -> bool:
    import numpy as np
    return all(isinstance(v, np.ndarray) for v in data.values()) or not any(isinstance(v, np.ndarray) for v in data.values())

Try / catch

from mlflow.exceptions import MlflowException
import numpy as np
try:
    pred = model.predict(data)
except MlflowException as e:
    if 'not all numpy.ndarray' in str(e):
        data = {k: np.asarray(v) for k, v in data.items()}
        pred = model.predict(data)
    else:
        raise

Prevention

When it happens

Trigger: pyfunc_model.predict({'a': np.array([1,2]), 'b': ['x','y']}) — one column ndarray, another a list (even of strings) triggers the mixed-values check in _validate_input_dictionary_contains_only_strings_and_lists_of_strings.

Common situations: Building one column from numpy preprocessing output and another from raw JSON data; partial vectorization of feature columns in inference scripts.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/c4cb0a018fc5e509. Report an issue: GitHub.