{"record":{"id":"f573ac46f5351da0","repo":"BerriAI/litellm","slug":"unexpected-key-key-r-expected-tool-calls-or","errorCode":null,"errorMessage":"Unexpected key {key!r}; expected 'tool_calls' or 'function_call'","messagePattern":"Unexpected key (.+?); expected 'tool_calls' or 'function_call'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":431,"sourceCode":"    `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.\n\n    Args:\n        params (_MessageCountParams): The parameters for counting tokens.\n        messages (List[AllMessageValues]): The list of messages to count tokens in.\n        use_default_image_token_count (bool): When True, will NOT make a GET request to the image URL and instead return the default image dimensions.\n        default_token_count (Optional[int]): The default number of tokens to return for a message block, if an error occurs.\n    \"\"\"\n    num_tokens = 0","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L413-L449","documentation":"Token counting dispatcher guard: _count_function_call_tokens was called with a key other than 'tool_calls' or 'function_call', meaning the caller routed an unrecognized message field into tool-call counting — an internal dispatch contract violation.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:431 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove the unexpected key; only 'tool_calls' or 'function_call' are expected there.","Check the message dict for misspelled keys."],"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"}