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>
This commit is contained in:
Kerry Lu 2026-11-15 12:10:12 -08:00 • committed by GitHub
parent 60716572a4
commit 765af9ab71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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