BerriAI/litellm · error · ValueError

Unsupported type for key tool_calls in message

Error message

Unsupported type {type(value)} for key tool_calls in message {message}

What it means

Tool-call token counting guard: the message's 'tool_calls' key holds a value that is not a list, so it cannot be iterated as OpenAI-style tool calls. Fires on malformed assistant messages whose tool_calls field has the wrong shape.

Solutions

  1. tool_calls in the message must be a list of tool call dicts, not the received type.
  2. Fix the message structure to match the OpenAI tool_calls schema.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:419 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/eb3c8a8ae3bdca2f. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/token_counter.py:419


def _count_function_call_tokens(
    key: str,
    value: Any,
    message: Mapping[str, Any],
    count_function: TokenCounterFunction,
) -> int:
    """
    Count tokens contributed by an assistant message's tool/function call payload.

    Handles both the modern `tool_calls` list and the legacy OpenAI
    `function_call` dict. Only the `arguments` string is counted (matching the
    existing tool_calls behavior); names are accounted for elsewhere via the
    tool/function definitions and `tool_choice`.
    """
    if key == "tool_calls":
        if not isinstance(value, list):
            raise ValueError(f"Unsupported type {type(value)} for key tool_calls in message {message}")
        total = 0
        for tool_call in value:
            if "function" not in tool_call:
                raise ValueError(f"Unsupported tool call {tool_call} must contain a function key")
            function_arguments = tool_call["function"].get("arguments", "")
            total += count_function(str(function_arguments))
        return total
    if key == "function_call":
        if not isinstance(value, Mapping):
            raise ValueError(f"Unsupported type {type(value)} for key function_call in message {message}")
        return count_function(str(value.get("arguments", "")))
    raise ValueError(f"Unexpected key {key!r}; expected 'tool_calls' or 'function_call'")


def _count_messages(
    params: _MessageCountParams,
    messages: list[AllMessageValues],
    use_default_image_token_count: bool,

View on GitHub (pinned to 77b7c6c40c)