BerriAI/litellm · warning · ValueError
Error counting field '{field_name}': {e}
Error message
Error counting field '{field_name}': {e} What it means
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.
Source
Thrown at litellm/litellm_core_utils/token_counter.py:681
field_value = content.get(field_name)
if field_value is None:
continue
try:
if isinstance(field_value, str):
tokens += count_function(field_value)
elif isinstance(field_value, list):
tokens += _count_content_list(
count_function,
field_value,
use_default_image_token_count,
default_token_count,
)
elif isinstance(field_value, dict):
tokens += count_function(str(field_value))
except Exception as e:
if default_token_count is not None:
return default_token_count
raise ValueError(f"Error counting field '{field_name}': {e}")
return tokens
def _count_content_list(
count_function: TokenCounterFunction,
content_list: OpenAIMessageContent,
use_default_image_token_count: bool,
default_token_count: int | None,
) -> int:
"""
Recursively count tokens from a list of content blocks.
"""
try:
num_tokens = 0
for c in content_list:
if isinstance(c, str):
num_tokens += count_function(c)
elif c["type"] == "text":View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before n = litellm.token_counter(model="claude-3-5-sonnet", messages=msgs) # raises on odd field # after n = litellm.token_counter(model="claude-3-5-sonnet", messages=msgs, default_token_count=0)
Defensive patterns
Strategy: fallback
Validate before calling
n = litellm.token_counter(model=model, messages=msgs, default_token_count=4) # degrades instead of raising
Try / catch
try:
n = litellm.token_counter(model=m, messages=msgs)
except ValueError as e:
if "Error counting field" in str(e):
n = litellm.token_counter(model=m, messages=msgs, default_token_count=0)
else:
raise Prevention
- Pass default_token_count whenever counting third-party message payloads.
- Keep tool inputs JSON-serializable primitives; avoid embedding images inside tool arguments.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Error getting number of tokens from content list: {e}, defau
- Anthropic content missing required field: 'type'
- Unknown Anthropic content type: '{content_type}'
- Missing required fields in {content_type} block: {', '.join(
- WebSearchInterception: missing follow-up messages
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/fca3eea2737b5c1a.
Report an issue: GitHub.