BerriAI/litellm · error · ValueError

Unsupported tool call

Error message

Unsupported tool call {tool_call} must contain a function key

What it means

Tool-call token counting guard: an entry in the 'tool_calls' list lacks a 'function' key, so it is not a valid OpenAI tool call and its tokens cannot be counted. Fires on incomplete or malformed tool_call dicts.

Solutions

  1. Each tool_call must contain a 'function' key with name/arguments.
  2. Fix the tool call dict structure in the message.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:423 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/6284f6b8065923ae. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/token_counter.py:423

    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,
    default_token_count: int | None,
) -> int:
    """
    Count the number of tokens in a list of messages.

View on GitHub (pinned to 77b7c6c40c)