diff --git a/litellm/llms/anthropic/count_tokens/transformation.py b/litellm/llms/anthropic/count_tokens/transformation.py index c2d87c641be..385744d2556 100644 --- a/litellm/llms/anthropic/count_tokens/transformation.py +++ b/litellm/llms/anthropic/count_tokens/transformation.py @@ -405,5 +405,5 @@ class AnthropicCountTokensConfig: if "role" not in message: raise ValueError(f"Message {i} must have a 'role' field") - if "content" not in message: + if "content" not in message and "tool_calls" not in message: raise ValueError(f"Message {i} must have a 'content' field") diff --git a/tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py b/tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py index 63f149fabe4..7923af52128 100644 --- a/tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py +++ b/tests/unit/llms/anthropic/test_anthropic_count_tokens_transformation.py @@ -747,3 +747,37 @@ async def test_counter_sends_the_structured_output_tool_choice_and_thinking_chat inference["tool_choice"], inference["tools"], ) + + +@pytest.mark.parametrize("config_type", (AnthropicCountTokensConfig, AzureAIAnthropicCountTokensConfig)) +def test_validate_request_accepts_a_tool_call_turn_without_content( + config_type: type[AnthropicCountTokensConfig], +) -> None: + """OpenAI leaves ``content`` optional on an assistant ``tool_calls`` turn, and an SDK dump that drops + None values leaves the key out, so the shape the chat translation already counts is not rejected here.""" + validated: Final = config_type().validate_request( + model="claude-opus-5-5", + messages=[ + {"role": "user", "content": "Weather in SF?"}, + { + "role": "assistant", + "tool_calls": [ + {"id": "call-1", "type": "function", "function": {"name": "get_weather", "arguments": "{}"}} + ], + }, + ], + ) + + assert validated is None + + +@pytest.mark.parametrize("config_type", (AnthropicCountTokensConfig, AzureAIAnthropicCountTokensConfig)) +def test_validate_request_still_requires_content_on_a_plain_turn( + config_type: type[AnthropicCountTokensConfig], +) -> None: + """A turn that carries neither content nor tool calls has nothing to count and is still refused.""" + with pytest.raises(ValueError, match="Message 1 must have a 'content' field"): + config_type().validate_request( + model="claude-opus-5-5", + messages=[{"role": "user", "content": "Weather in SF?"}, {"role": "assistant"}], + )