{"record":{"id":"6284f6b8065923ae","repo":"BerriAI/litellm","slug":"unsupported-tool-call-tool-call-must-contain-a-f","errorCode":null,"errorMessage":"Unsupported tool call {tool_call} must contain a function key","messagePattern":"Unsupported tool call (.+?) must contain a function key","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":423,"sourceCode":"    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,\n    default_token_count: int | None,\n) -> int:\n    \"\"\"\n    Count the number of tokens in a list of messages.","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L405-L441","documentation":"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.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:423 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Each tool_call must contain a 'function' key with name/arguments.","Fix the tool call dict structure in the message."],"exampleFix":null,"handlingStrategy":"validation","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"}