BerriAI/litellm · error · ValueError

Message {i} must have a 'role' field

Error message

Message {i} must have a 'role' field

What it means

Converse-format validation requiring each message dict to contain a 'role' key (e.g. 'user'/'assistant'). The message index i is included so the offending turn can be located.

Source

Thrown at litellm/llms/bedrock/count_tokens/transformation.py:274

            raise ValueError("model parameter is required")

        input_type: Final = self._detect_input_type(request_data)

        if input_type == "converse":
            # Validate Converse format (messages-based)
            messages: Final = request_data.get("messages", [])
            if not messages:
                raise ValueError("messages parameter is required for Converse input")

            if not isinstance(messages, list):
                raise ValueError("messages must be a list")

            for i, message in enumerate(messages):
                if not isinstance(message, dict):
                    raise ValueError(f"Message {i} must be a dictionary")

                if "role" not in message:
                    raise ValueError(f"Message {i} must have a 'role' field")

                if "content" not in message:
                    raise ValueError(f"Message {i} must have a 'content' field")
        else:
            # For InvokeModel format, we need at least some content to count tokens
            # The content structure varies by model, so we do minimal validation
            if len(request_data) <= 1:  # Only has 'model' field
                raise ValueError("Request must contain content to count tokens")

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Add role to each message: {'role': 'user'|'assistant', 'content': [...]}
  2. When importing from another provider's schema, map its speaker field to 'role'

Example fix

# before
{'content': [{'text': 'hello'}]}

# after
{'role': 'user', 'content': [{'text': 'hello'}]}
Defensive patterns

Strategy: validation

Validate before calling

VALID_ROLES = {"user", "assistant"}
for i, m in enumerate(req["messages"]):
    if "role" not in m:
        raise ValueError(f"Message {i} must have a 'role' field")

Type guard

def has_role(m: dict) -> bool:
    return isinstance(m, dict) and m.get("role") in {"user", "assistant"}

Prevention

When it happens

Trigger: messages = [{'content': [...]}] — content present but role omitted; or role stored under a different key like 'speaker'/'author' by a custom serializer.

Common situations: Hand-built message dicts, conversion from other vendors' formats (some omit role), or typos like 'Role'.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/a11fe007a7036703. Report an issue: GitHub.