BerriAI/litellm · error · ValueError

Unknown Anthropic content type: '{content_type}'

Error message

Unknown Anthropic content type: '{content_type}'

What it means

An Anthropic content block's 'type' is not one of the two tool-related types this validator recognizes ('tool_use' or 'tool_result'). Since the helper only runs for tool-shaped blocks, an unknown type means malformed or misplaced content - e.g. text/image blocks misrouted, or typos like 'tool-use'.

Source

Thrown at litellm/litellm_core_utils/token_counter.py:627

def _validate_anthropic_content(content: Mapping[str, Any]) -> type:
    """
    Validate and determine which Anthropic TypedDict applies.

    Returns the corresponding TypedDict class if recognized, otherwise raises.
    """
    content_type: Final = content.get("type")
    if not content_type:
        raise ValueError("Anthropic content missing required field: 'type'")

    mapping: Final = {
        "tool_use": AnthropicMessagesToolUseParam,
        "tool_result": AnthropicMessagesToolResultParam,
    }

    expected_cls: Final = mapping.get(content_type)
    if expected_cls is None:
        raise ValueError(f"Unknown Anthropic content type: '{content_type}'")

    missing: Final = [k for k in getattr(expected_cls, "__required_keys__", set()) if k not in content]
    if missing:
        raise ValueError(f"Missing required fields in {content_type} block: {', '.join(missing)}")

    return expected_cls


def _count_anthropic_content(
    content: Mapping[str, Any],
    count_function: TokenCounterFunction,
    use_default_image_token_count: bool,
    default_token_count: int | None,
) -> int:
    """
    Count tokens in Anthropic-specific content blocks (tool_use, tool_result, etc.).

    Uses TypedDict definitions from litellm.types.llms.anthropic to determine

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Use the exact Anthropic snake_case types: 'tool_use' and 'tool_result' for tool blocks; 'text' and 'image' for others.
  2. Validate your builder against the official Anthropic messages schema.

Example fix

# before
block = {"type": "tool-use", "id": "t1", "name": "get_weather", "input": {}}

# after
block = {"type": "tool_use", "id": "t1", "name": "get_weather", "input": {}}
Defensive patterns

Strategy: validation

Validate before calling

KNOWN = {"tool_use", "tool_result", "text", "image", "thinking"}
if block.get("type") not in KNOWN:
    raise ValueError("bad content type " + repr(block.get("type")))

Type guard

def is_known_anthropic_tool_type(block: dict) -> bool:
    return block.get("type") in ("tool_use", "tool_result")

Prevention

When it happens

Trigger: A block with type='tool-use' (hyphen instead of underscore), type='toolcall', or a text/image block incorrectly dispatched into the tool-block validator on the token-counting path.

Common situations: Hand-written Anthropic message builders guessing at type names; converting from other vendor schemas with different discriminators; case errors like 'ToolUse'.

Related errors


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