{"record":{"id":"fca3eea2737b5c1a","repo":"BerriAI/litellm","slug":"error-counting-field-field-name-e","errorCode":null,"errorMessage":"Error counting field '{field_name}': {e}","messagePattern":"Error counting field '(.+?)': (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":681,"sourceCode":"        field_value = content.get(field_name)\n        if field_value is None:\n            continue\n        try:\n            if isinstance(field_value, str):\n                tokens += count_function(field_value)\n            elif isinstance(field_value, list):\n                tokens += _count_content_list(\n                    count_function,\n                    field_value,\n                    use_default_image_token_count,\n                    default_token_count,\n                )\n            elif isinstance(field_value, dict):\n                tokens += count_function(str(field_value))\n        except Exception as e:\n            if default_token_count is not None:\n                return default_token_count\n            raise ValueError(f\"Error counting field '{field_name}': {e}\")\n    return tokens\n\n\ndef _count_content_list(\n    count_function: TokenCounterFunction,\n    content_list: OpenAIMessageContent,\n    use_default_image_token_count: bool,\n    default_token_count: int | None,\n) -> int:\n    \"\"\"\n    Recursively count tokens from a list of content blocks.\n    \"\"\"\n    try:\n        num_tokens = 0\n        for c in content_list:\n            if isinstance(c, str):\n                num_tokens += count_function(c)\n            elif c[\"type\"] == \"text\":","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L663-L699","documentation":"Token counting error wrapper: while counting tokens for a named field of an Anthropic tool_use/tool_result block, an exception occurred; it is wrapped with the field name so callers can locate the offending part of the message instead of getting a bare failure.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:681 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix the malformed field named in the error; its token counting failed.","Validate the message content types before counting tokens."],"exampleFix":null,"handlingStrategy":"try-catch","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"}