{"record":{"id":"243cabdba456baa8","repo":"BerriAI/litellm","slug":"invalid-content-item-type-content-type-expecte","errorCode":null,"errorMessage":"Invalid content item type: {content_type}. Expected str or dict with 'type' field (text, image_url, tool_use, tool_result, thinking, tool_reference).","messagePattern":"Invalid content item type: (.+?)\\. Expected str or dict with 'type' field \\(text, image_url, tool_use, tool_result, thinking, tool_reference\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":730,"sourceCode":"                # Claude extended thinking content block\n                # Count the thinking text and skip signature (opaque signature blob)\n                thinking_text = str(c.get(\"thinking\", \"\"))\n                if thinking_text:\n                    num_tokens += count_function(thinking_text)\n            elif c[\"type\"] == \"tool_reference\":\n                # Anthropic tool-search reference block: a lightweight pointer to\n                # a deferred tool, e.g. {\"type\": \"tool_reference\", \"tool_name\": ...}.\n                # The full tool definition is counted via the `tools` param, so we\n                # only count the referenced name here. Without this branch,\n                # token_counter raises on tool-search traffic; on the streaming\n                # anthropic_messages path that nulls response_cost and causes the\n                # proxy to drop the SpendLogs row entirely (silent cost undercount).\n                tool_name = str(c.get(\"tool_name\") or \"\")\n                if tool_name:\n                    num_tokens += count_function(tool_name)\n            else:\n                content_type = c.get(\"type\", type(c).__name__) if isinstance(c, dict) else type(c).__name__\n                raise ValueError(\n                    f\"Invalid content item type: {content_type}. \"\n                    f\"Expected str or dict with 'type' field (text, image_url, tool_use, tool_result, thinking, tool_reference).\"\n                )\n        return num_tokens\n    except Exception as e:\n        if default_token_count is not None:\n            return default_token_count\n        raise ValueError(\n            f\"Error getting number of tokens from content list: {e}, default_token_count={default_token_count}\"\n        )\n\n\ndef _format_function_definitions(tools):\n    \"\"\"Formats tool definitions in the format that OpenAI appears to use.\n    Based on https://github.com/forestwanglin/openai-java/blob/main/jtokkit/src/main/java/xyz/felh/openai/jtokkit/utils/TikTokenUtils.java\n    \"\"\"\n    lines: Final = []\n    lines.append(\"namespace functions {\")","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L712-L748","documentation":"Content-list counting guard: a content item's 'type' is not one of the supported kinds (text, image_url, tool_use, tool_result, thinking, tool_reference), so its tokens cannot be counted. Fires on unknown or malformed content items in messages.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:730 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use content items that are strings or dicts with a valid 'type' field.","Remove or convert the unsupported content item."],"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"}