{"record":{"id":"8c04cac79ca9eefd","repo":"mlflow/mlflow","slug":"items-in-key-must-be-either-an-instance-of-c","errorCode":null,"errorMessage":"Items in `{key}` must be either an instance of `{cls.__name__}` or a dict matching the schema. Received `{type(v).__name__}`","messagePattern":"Items in `(.+?)` must be either an instance of `(.+?)` or a dict matching the schema\\. Received `(.+?)`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mlflow/types/llm.py","lineNumber":113,"sourceCode":"            if required:\n                raise ValueError(f\"`{key}` is required\")\n            return\n\n        if not isinstance(mapping, dict):\n            raise ValueError(f\"`{key}` must be a dict\")\n\n        # create a new map to avoid mutating the original\n        new_mapping = {}\n        for k, v in mapping.items():\n            if isinstance(v, cls):\n                new_mapping[k] = v\n            elif isinstance(v, dict):\n                try:\n                    new_mapping[k] = cls.from_dict(v)\n                except TypeError as e:\n                    raise ValueError(f\"Error when coercing {v} to {cls.__name__}: {e}\")\n            else:\n                raise ValueError(\n                    f\"Items in `{key}` must be either an instance of `{cls.__name__}` \"\n                    f\"or a dict matching the schema. Received `{type(v).__name__}`\"\n                )\n        setattr(self, key, new_mapping)\n\n    def to_dict(self):\n        return asdict(self, dict_factory=lambda obj: {k: v for (k, v) in obj if v is not None})\n\n    @classmethod\n    def from_dict(cls, data):\n        \"\"\"\n        Create an instance of the class from a dict, ignoring any undefined fields.\n        This is useful when the dict contains extra fields, causing cls(**data) to fail.\n        \"\"\"\n        field_names = [field.name for field in fields(cls)]\n        filtered_data = {k: v for k, v in data.items() if k in field_names}\n        return cls(**filtered_data)\n","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/types/llm.py#L95-L131","documentation":"_convert_dataclass_map requires every map value to be either an instance of the target dataclass or a dict that can be coerced. Values of any other type (str, int, list, bool, None) trigger this ValueError, which reports the offending value's type name.","triggerScenarios":"Schema maps like {'city': 'string'} (string where a dict/ParamProperty was required), or {'count': 1}, or values that are JSON arrays instead of objects.","commonSituations":"Writing shorthand type definitions ('city': 'string') as in some frameworks, mistaking a map-of-objects field for a map-of-strings field, or truncating nested JSON during templating.","solutions":["Wrap shorthand values in the full object form: {'city': {'type': 'string'}}.","Convert non-dict values to the target dataclass or dict before construction.","Validate the map contents with a loop asserting isinstance(v, (dict, TargetClass)) before calling the API."],"exampleFix":"// before\n{\"city\": \"string\"}\n// after\n{\"city\": {\"type\": \"string\"}}","handlingStrategy":"type-guard","validationCode":"for k, v in props.items():\n    if not isinstance(v, (dict, ParamProperty)):\n        raise TypeError(f\"property '{k}' must be a dict or ParamProperty, got {type(v).__name__}\")","typeGuard":"def is_property_map(props):\n    return isinstance(props, dict) and all(isinstance(v, (dict, ParamProperty)) for v in props.values())","tryCatchPattern":"try:\n    obj = MyType(properties=props)\nexcept ValueError as e:\n    if \"must be either an instance\" in str(e):\n        props = {k: {\"type\": v} if isinstance(v, str) else v for k, v in props.items()}\n        obj = MyType(properties=props)\n    else:\n        raise","preventionTips":["No shorthand values: 'city': 'string' must be {'city': {'type': 'string'}}.","Assert map value types before API calls in tests.","Keep a small schema-builder helper that emits correct shapes."],"tags":["python","mlflow","type-validation","json-schema"],"backgroundTag":"type-mismatch","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}