{"record":{"id":"ff9bd6b97e03aa94","repo":"mlflow/mlflow","slug":"all-items-in-key-must-be-of-type-val-type-n","errorCode":null,"errorMessage":"All items in `{key}` must be of type {val_type.__name__}","messagePattern":"All items in `(.+?)` must be of type (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mlflow/types/llm.py","lineNumber":46,"sourceCode":"            raise ValueError(\n                f\"`{key}` must be of type {val_type.__name__}, got {type(value).__name__}\"\n            )\n\n    def _validate_literal(self, key, allowed_values, required):\n        value = getattr(self, key, None)\n        if required and value is None:\n            raise ValueError(f\"`{key}` is required\")\n        if value is not None and value not in allowed_values:\n            raise ValueError(f\"`{key}` must be one of {allowed_values}, got {value}\")\n\n    def _validate_list(self, key, val_type, required):\n        values = getattr(self, key, None)\n        if required and values is None:\n            raise ValueError(f\"`{key}` is required\")\n\n        if values is not None:\n            if isinstance(values, list) and not all(isinstance(v, val_type) for v in values):\n                raise ValueError(f\"All items in `{key}` must be of type {val_type.__name__}\")\n            elif not isinstance(values, list):\n                raise ValueError(f\"`{key}` must be a list, got {type(values).__name__}\")\n\n    def _convert_dataclass(self, key: str, cls: \"_BaseDataclass\", required=True):\n        value = getattr(self, key)\n        if value is None:\n            if required:\n                raise ValueError(f\"`{key}` is required\")\n            return\n\n        if isinstance(value, cls):\n            return\n\n        if not isinstance(value, dict):\n            raise ValueError(\n                f\"Expected `{key}` to be either an instance of `{cls.__name__}` or \"\n                f\"a dict matching the schema. Received `{type(value).__name__}`\"\n            )","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/types/llm.py#L28-L64","documentation":"_validate_list raises this when the field is a list but one or more items are not instances of the required element type. E.g. messages must be a list of ChatMessage; a raw dict inside the list fails.","triggerScenarios":"ChatCompletionRequest(messages=[{\"role\": \"user\", \"content\": \"hi\"}]) — a dict instead of a ChatMessage instance; mixing parsed and unparsed items in the list.","commonSituations":"Building requests from JSON logs where nested objects stay dicts; appending raw provider payloads into a list alongside proper dataclass instances; partially migrated code after schema changes.","solutions":["Convert each item to the expected dataclass first: [ChatMessage.from_dict(m) for m in raw_messages]","Ensure every appended item is an instance of the element type named in the message","Validate the list before construction: all(isinstance(v, ExpectedType) for v in values)"],"exampleFix":"// before\nreq = ChatCompletionRequest(messages=[{\"role\": \"user\", \"content\": \"hi\"}])\n// after\nreq = ChatCompletionRequest(messages=[ChatMessage.from_dict({\"role\": \"user\", \"content\": \"hi\"})])","handlingStrategy":"type-guard","validationCode":"if not all(isinstance(m, ChatMessage) for m in messages):\n    messages = [m if isinstance(m, ChatMessage) else ChatMessage.from_dict(m) for m in messages]","typeGuard":"def is_chat_message_list(v):\n    return isinstance(v, list) and all(isinstance(m, ChatMessage) for m in v)","tryCatchPattern":"try:\n    req = ChatCompletionRequest(messages=messages)\nexcept ValueError as e:\n    log.error(\"Bad message list: %s\", e)\n    messages = [ChatMessage.from_dict(m) for m in messages]\n    req = ChatCompletionRequest(messages=messages)","preventionTips":["Convert dicts to dataclasses at ingestion, not at request-construction time","Type-hint list fields as List[ChatMessage] and let static checkers catch raw dicts","Centralize a normalize_messages() helper used by all request builders"],"tags":["python","list-validation","type-mismatch","mlflow"],"backgroundTag":"schema-validation-failed","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}