{"record":{"id":"c4cb0a018fc5e509","repo":"mlflow/mlflow","slug":"the-dictionary-values-are-not-all-numpy-ndarray","errorCode":null,"errorMessage":"The dictionary values are not all numpy.ndarray.","messagePattern":"The dictionary values are not all numpy\\.ndarray\\.","errorType":"validation","errorClass":"MlflowException","httpStatus":null,"severity":"error","filePath":"mlflow/types/utils.py","lineNumber":646,"sourceCode":"\n        return isinstance(x, pyspark.sql.connect.dataframe.DataFrame)\n    except ImportError:\n        return False\n\n\ndef _validate_input_dictionary_contains_only_strings_and_lists_of_strings(data) -> None:\n    # isinstance(True, int) is True\n    invalid_keys = [\n        key for key in data.keys() if not isinstance(key, (str, int)) or isinstance(key, bool)\n    ]\n    if invalid_keys:\n        raise MlflowException(\n            f\"The dictionary keys are not all strings or indexes. Invalid keys: {invalid_keys}\"\n        )\n    if any(isinstance(value, np.ndarray) for value in data.values()) and not all(\n        isinstance(value, np.ndarray) for value in data.values()\n    ):\n        raise MlflowException(\"The dictionary values are not all numpy.ndarray.\")\n\n    invalid_values = [\n        key\n        for key, value in data.items()\n        if (isinstance(value, list) and not all(isinstance(item, (str, bytes)) for item in value))\n        or (not isinstance(value, (np.ndarray, list, str, bytes)))\n    ]\n    if invalid_values:\n        raise MlflowException.invalid_parameter_value(\n            \"Invalid values in dictionary. If passing a dictionary containing strings, all \"\n            \"values must be either strings or lists of strings. If passing a dictionary containing \"\n            \"numeric values, the data must be enclosed in a numpy.ndarray. The following keys \"\n            f\"in the input dictionary are invalid: {invalid_values}\",\n        )\n\n\ndef _is_list_str(type_hint: Any) -> bool:\n    return type_hint in [","sourceCodeStart":628,"sourceCodeEnd":664,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/types/utils.py#L628-L664","documentation":"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).","triggerScenarios":"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.","commonSituations":"Building one column from numpy preprocessing output and another from raw JSON data; partial vectorization of feature columns in inference scripts.","solutions":["Wrap every value in np.array(...): {k: np.asarray(v) for k, v in data.items()}.","Alternatively convert all values to plain lists so none is an ndarray (then the list-of-strings rules apply).","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).","Check each value's type before predict with a guard (see validationCode)."],"exampleFix":"// before\nmodel.predict({'a': np.array([1, 2]), 'b': ['x', 'y']})\n// after\nmodel.predict({'a': np.array([1, 2]), 'b': np.array(['x', 'y'])})","handlingStrategy":"validation","validationCode":"import numpy as np\ndef check_homogeneous_values(data: dict):\n    vals = list(data.values())\n    if any(isinstance(v, np.ndarray) for v in vals) and not all(isinstance(v, np.ndarray) for v in vals):\n        raise ValueError('All dict values must be numpy.ndarray when any is')","typeGuard":"def values_homogeneous(data: dict) -> bool:\n    import numpy as np\n    return all(isinstance(v, np.ndarray) for v in data.values()) or not any(isinstance(v, np.ndarray) for v in data.values())","tryCatchPattern":"from mlflow.exceptions import MlflowException\nimport numpy as np\ntry:\n    pred = model.predict(data)\nexcept MlflowException as e:\n    if 'not all numpy.ndarray' in str(e):\n        data = {k: np.asarray(v) for k, v in data.items()}\n        pred = model.predict(data)\n    else:\n        raise","preventionTips":["Normalize inputs at one boundary: {k: np.asarray(v) for k, v in raw.items()}.","Prefer DataFrames or a single ndarray for mixed-type tabular inputs.","Add input-shape/type assertions in inference scripts."],"tags":["mlflow","pyfunc","input-validation","numpy"],"backgroundTag":"inconsistent-input-types","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}