mlflow/mlflow · error

All items in `{key}` must be of type {val_type.__name__}

Error message

All items in `{key}` must be of type {val_type.__name__}

What it means

_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.

Source

Thrown at mlflow/types/llm.py:46

            raise ValueError(
                f"`{key}` must be of type {val_type.__name__}, got {type(value).__name__}"
            )

    def _validate_literal(self, key, allowed_values, required):
        value = getattr(self, key, None)
        if required and value is None:
            raise ValueError(f"`{key}` is required")
        if value is not None and value not in allowed_values:
            raise ValueError(f"`{key}` must be one of {allowed_values}, got {value}")

    def _validate_list(self, key, val_type, required):
        values = getattr(self, key, None)
        if required and values is None:
            raise ValueError(f"`{key}` is required")

        if values is not None:
            if isinstance(values, list) and not all(isinstance(v, val_type) for v in values):
                raise ValueError(f"All items in `{key}` must be of type {val_type.__name__}")
            elif not isinstance(values, list):
                raise ValueError(f"`{key}` must be a list, got {type(values).__name__}")

    def _convert_dataclass(self, key: str, cls: "_BaseDataclass", required=True):
        value = getattr(self, key)
        if value is None:
            if required:
                raise ValueError(f"`{key}` is required")
            return

        if isinstance(value, cls):
            return

        if not isinstance(value, dict):
            raise ValueError(
                f"Expected `{key}` to be either an instance of `{cls.__name__}` or "
                f"a dict matching the schema. Received `{type(value).__name__}`"
            )

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Convert each item to the expected dataclass first: [ChatMessage.from_dict(m) for m in raw_messages]
  2. Ensure every appended item is an instance of the element type named in the message
  3. Validate the list before construction: all(isinstance(v, ExpectedType) for v in values)

Example fix

// before
req = ChatCompletionRequest(messages=[{"role": "user", "content": "hi"}])
// after
req = ChatCompletionRequest(messages=[ChatMessage.from_dict({"role": "user", "content": "hi"})])
Defensive patterns

Strategy: type-guard

Validate before calling

if not all(isinstance(m, ChatMessage) for m in messages):
    messages = [m if isinstance(m, ChatMessage) else ChatMessage.from_dict(m) for m in messages]

Type guard

def is_chat_message_list(v):
    return isinstance(v, list) and all(isinstance(m, ChatMessage) for m in v)

Try / catch

try:
    req = ChatCompletionRequest(messages=messages)
except ValueError as e:
    log.error("Bad message list: %s", e)
    messages = [ChatMessage.from_dict(m) for m in messages]
    req = ChatCompletionRequest(messages=messages)

Prevention

When it happens

Trigger: ChatCompletionRequest(messages=[{"role": "user", "content": "hi"}]) — a dict instead of a ChatMessage instance; mixing parsed and unparsed items in the list.

Common situations: 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.

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


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