BerriAI/litellm · warning · ValueError

Error getting number of tokens from content list: {e}, defau

Error message

Error getting number of tokens from content list: {e}, default_token_count={default_token_count}

What it means

Catch-all for any exception raised while counting a content list: the original error is wrapped with the default_token_count value for context. It usually wraps one of the deeper ValueErrors (invalid image_url, invalid content item type, encoder failure on exotic text). If default_token_count was provided, that value is returned instead of raising.

Source

Thrown at litellm/litellm_core_utils/token_counter.py:738

                # The full tool definition is counted via the `tools` param, so we
                # only count the referenced name here. Without this branch,
                # token_counter raises on tool-search traffic; on the streaming
                # anthropic_messages path that nulls response_cost and causes the
                # proxy to drop the SpendLogs row entirely (silent cost undercount).
                tool_name = str(c.get("tool_name") or "")
                if tool_name:
                    num_tokens += count_function(tool_name)
            else:
                content_type = c.get("type", type(c).__name__) if isinstance(c, dict) else type(c).__name__
                raise ValueError(
                    f"Invalid content item type: {content_type}. "
                    f"Expected str or dict with 'type' field (text, image_url, tool_use, tool_result, thinking, tool_reference)."
                )
        return num_tokens
    except Exception as e:
        if default_token_count is not None:
            return default_token_count
        raise ValueError(
            f"Error getting number of tokens from content list: {e}, default_token_count={default_token_count}"
        )


def _format_function_definitions(tools):
    """Formats tool definitions in the format that OpenAI appears to use.
    Based on https://github.com/forestwanglin/openai-java/blob/main/jtokkit/src/main/java/xyz/felh/openai/jtokkit/utils/TikTokenUtils.java
    """
    lines: Final = []
    lines.append("namespace functions {")
    lines.append("")
    for tool in tools:
        if not isinstance(tool, dict):
            continue
        function = tool.get("function")
        if not isinstance(function, dict):
            # Anthropic tool shape → OpenAI function dict for token counting.
            params = tool.get("input_schema") or tool.get("parameters") or {}

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Read the wrapped inner error (embedded in the message) and fix that specific block.
  2. Call token_counter with default_token_count=<int> so malformed blocks fall back to an estimate.
  3. Validate and sanitize message content before counting.

Example fix

# before
n = litellm.token_counter(model=m, messages=msgs)  # raises wrapped error

# after
try:
    n = litellm.token_counter(model=m, messages=msgs)
except ValueError as e:
    log.warning("token count failed, estimating: %s", e)
    n = litellm.token_counter(model=m, messages=msgs, default_token_count=4)
Defensive patterns

Strategy: fallback

Validate before calling

n = litellm.token_counter(model=model, messages=msgs, default_token_count=4)

Try / catch

try:
    n = litellm.token_counter(model=m, messages=msgs)
except ValueError as e:
    log.warning("token counting degraded: %s", e)
    n = estimate_tokens(msgs)  # len(content)//4 heuristic or retry with default_token_count

Prevention

When it happens

Trigger: Any token_counter(messages=[...]) call where a content block fails deeper validation and no default_token_count was given - bad image detail, unknown block type, or an encoder exception on unusual unicode.

Common situations: Proxy cost-accounting paths that count arbitrary user traffic; untrusted or multimodal request bodies; token counting right after a provider introduces new content features.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/043ed5109f00c465. Report an issue: GitHub.