{"record":{"id":"eb3c8a8ae3bdca2f","repo":"BerriAI/litellm","slug":"unsupported-type-type-value-for-key-tool-calls","errorCode":null,"errorMessage":"Unsupported type {type(value)} for key tool_calls in message {message}","messagePattern":"Unsupported type (.+?) for key tool_calls in message (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":419,"sourceCode":"\n\ndef _count_function_call_tokens(\n    key: str,\n    value: Any,\n    message: Mapping[str, Any],\n    count_function: TokenCounterFunction,\n) -> int:\n    \"\"\"\n    Count tokens contributed by an assistant message's tool/function call payload.\n\n    Handles both the modern `tool_calls` list and the legacy OpenAI\n    `function_call` dict. Only the `arguments` string is counted (matching the\n    existing tool_calls behavior); names are accounted for elsewhere via the\n    tool/function definitions and `tool_choice`.\n    \"\"\"\n    if key == \"tool_calls\":\n        if not isinstance(value, list):\n            raise ValueError(f\"Unsupported type {type(value)} for key tool_calls in message {message}\")\n        total = 0\n        for tool_call in value:\n            if \"function\" not in tool_call:\n                raise ValueError(f\"Unsupported tool call {tool_call} must contain a function key\")\n            function_arguments = tool_call[\"function\"].get(\"arguments\", \"\")\n            total += count_function(str(function_arguments))\n        return total\n    if key == \"function_call\":\n        if not isinstance(value, Mapping):\n            raise ValueError(f\"Unsupported type {type(value)} for key function_call in message {message}\")\n        return count_function(str(value.get(\"arguments\", \"\")))\n    raise ValueError(f\"Unexpected key {key!r}; expected 'tool_calls' or 'function_call'\")\n\n\ndef _count_messages(\n    params: _MessageCountParams,\n    messages: list[AllMessageValues],\n    use_default_image_token_count: bool,","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L401-L437","documentation":"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.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:419 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["tool_calls in the message must be a list of tool call dicts, not the received type.","Fix the message structure to match the OpenAI tool_calls schema."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}