From 765af9ab7167bda2db1c960ea2324a134e907706 Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Sun, 15 Nov 2026 12:10:12 -0800 Subject: [PATCH] fix(count_tokens): return 400 for malformed Anthropic server-tool blocks (#51957) Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/litellm_core_utils/token_counter.py | 8 +++++--- .../test_count_tokens_error_shapes_wire.py | 14 ++++++++++++- .../litellm_core_utils/test_token_counter.py | 20 +++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index c0116454ce6..f51050114fe 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -1884,9 +1884,11 @@ def _count_anthropic_content( except Exception as e: if default_token_count is not None: return default_token_count - raise (UnsupportedContentTypeError if isinstance(e, UnsupportedContentTypeError) else ValueError)( - f"Error counting field '{field_name}': {e}" - ) + raise ( + UnsupportedContentTypeError + if server_fields is not None or isinstance(e, UnsupportedContentTypeError) + else ValueError + )(f"Error counting field '{field_name}': {e}") return tokens diff --git a/tests/integration/providers/test_count_tokens_error_shapes_wire.py b/tests/integration/providers/test_count_tokens_error_shapes_wire.py index 59114e4aced..300d5d29049 100644 --- a/tests/integration/providers/test_count_tokens_error_shapes_wire.py +++ b/tests/integration/providers/test_count_tokens_error_shapes_wire.py @@ -113,8 +113,20 @@ def test_local_messages_count_accepts_search_result_text(gateway: Gateway, neste ( ({"type": "future_block", "future_block": {"value": "ignore me"}}, 400, "invalid_request_error", "future_block"), ({"type": "image"}, 500, "api_error", "source"), + ( + {"type": "web_search_tool_result", "tool_use_id": "srv_1", "content": [{"url": "https://example.invalid"}]}, + 400, + "invalid_request_error", + "content", + ), + ( + {"type": "code_execution_tool_result", "tool_use_id": "srv_1", "content": {"type": "future_result"}}, + 400, + "invalid_request_error", + "future_result", + ), ), - ids=("unknown-block", "unrelated-counting-error"), + ids=("unknown-block", "unrelated-counting-error", "server-result-without-type", "unknown-server-result-type"), ) def test_local_messages_count_classifies_only_unsupported_content_as_an_invalid_request( gateway: Gateway, nested: bool, block: JsonValue, status: int, error_type: str, message: str diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index b65f2fc383d..6d2e33e5b2d 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -2638,6 +2638,26 @@ def test_token_counter_counts_server_tool_errors(tool: str) -> None: assert _count_content_list(len, [block], False, None) == len("unavailable") +@pytest.mark.parametrize( + "block", + ( + {"type": "web_search_tool_result", "tool_use_id": "srv_1", "content": [{"url": "https://example.invalid"}]}, + {"type": "code_execution_tool_result", "tool_use_id": "srv_1", "content": {"type": "future_result"}}, + {"type": "web_fetch_tool_result", "tool_use_id": "srv_1", "content": {"url": "https://example.invalid"}}, + ), + ids=("result-without-type", "unknown-result-type", "mapping-without-type"), +) +@pytest.mark.parametrize("nested", (False, True), ids=("assistant-content", "tool-result")) +def test_malformed_server_tool_block_is_unsupported_content(block: Mapping[str, object], nested: bool) -> None: + from litellm.litellm_core_utils.token_counter import UnsupportedContentTypeError, _count_content_list + + content: Final = [{"type": "tool_result", "tool_use_id": "toolu_1", "content": [block]}] if nested else [block] + + with pytest.raises(UnsupportedContentTypeError): + _count_content_list(len, content, False, None) + assert _count_content_list(len, content, False, 7) == 7 + + def test_token_counter_counts_anthropic_search_result_text() -> None: from litellm.litellm_core_utils.token_counter import _count_content_list