BerriAI/litellm · error · ValueError
Anthropic content missing required field: 'type'
Error message
Anthropic content missing required field: 'type'
What it means
Anthropic content validation guard: a content dict used for Anthropic tool-use token counting lacks a 'type' field, so the appropriate TypedDict cannot be determined. Fires on malformed tool_use/tool_result content blocks.
Solutions
- Add the required 'type' field to each Anthropic content block.
- Use blocks like {"type": "text", "text": "..."}.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:618 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/8f3577659803b9bf.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/token_counter.py:618
raise ValueError("Empty image_url string is not valid.")
return calculate_img_tokens(
data=image_url,
mode="auto",
use_default_image_token_count=use_default_image_token_count,
)
else:
raise ValueError(f"Invalid image_url type: {type(image_url).__name__}. Expected str or dict with 'url' field.")
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(View on GitHub (pinned to 77b7c6c40c)