{"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":"warning","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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/token_counter.py#L663-L699","documentation":"A wrapper around token counting of a named field in an Anthropic content block (e.g. 'text' or 'input'): the underlying count call threw (often a nested image/object inside tool_use input, or a non-serializable value). If default_token_count was supplied, that fallback is returned instead; otherwise the error is re-raised wrapped with the field name.","triggerScenarios":"A tool_use block whose 'input' contains nested structures the counter chokes on (e.g. image data inside input), or a field value of an unexpected type - and token_counter was called without default_token_count.","commonSituations":"Complex nested tool inputs (base64 images inside tool arguments); malformed base64 in tool_result content; cost-accounting code that does not pass default_token_count.","solutions":["Inspect the inner exception embedded in the message - it names the field and root cause; fix or normalize that field's value.","Pass default_token_count (e.g. 0 or an estimate) when counting untrusted or complex messages so counting degrades gracefully."],"exampleFix":"# before\nn = litellm.token_counter(model=\"claude-3-5-sonnet\", messages=msgs)  # raises on odd field\n\n# after\nn = litellm.token_counter(model=\"claude-3-5-sonnet\", messages=msgs, default_token_count=0)","handlingStrategy":"fallback","validationCode":"n = litellm.token_counter(model=model, messages=msgs, default_token_count=4)  # degrades instead of raising","typeGuard":null,"tryCatchPattern":"try:\n    n = litellm.token_counter(model=m, messages=msgs)\nexcept ValueError as e:\n    if \"Error counting field\" in str(e):\n        n = litellm.token_counter(model=m, messages=msgs, default_token_count=0)\n    else:\n        raise","preventionTips":["Pass default_token_count whenever counting third-party message payloads.","Keep tool inputs JSON-serializable primitives; avoid embedding images inside tool arguments."],"tags":["token-counter","anthropic","wrapped-error","fallback"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}