Backtestslitellmrun-20261111-220245#47952

Issue #47952 Merged fix PR #48699 Run 11 Nov 2026 · run-20261111-220245 BerriAI/litellm

[Bug]: /v1/messages/count_tokens returns 500 for a search_result block, and for any content block type the local counter does not list

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches fix the issue the same way at the root: a `search_result` TypedDict, a counting branch for title/source/content, a `ValueError` subclass for unknown block types, and a mapping of that subclass to a 400 `invalid_request_error`. All ten independent tests pass on both, so correctness is equal on the issue's scenarios. The differences are in where the 400 is produced and what the client sees. Patch B maps the error inside `count_request_tokens` in proxy_server.py, around the local estimate call, so every caller of the shared counting path (not only the Anthropic route) stops answering 500, and it emits a clean message ("Token counting does not support content blocks of type 'future_block'") carrying the type on the exception and preserving the chain with `from e`. Patch A maps it only in the Anthropic endpoint and returns the internal wrapped string as the client message ("Error getting number of tokens from content list: Invalid content item type: ... default_token_count=None"), which names the type but leaks counter internals; its `raise (X if isinstance(...) else ValueError)(...)` re-raise is also awkward. B's TypedDict includes the documented `cache_control`/`citations` fields consistent with the neighbouring document param and has an explicit `_count_search_result_tokens` that recurses through `_count_content_list` for nested content; A reuses the generic `_count_anthropic_content` and adds the type to the user/tool_result unions, which is reasonable too. On tests, A is slightly stronger: it includes a negative control (an `image` without `source` still yields 500 api_error) and a default_token_count fallback check, while B covers unit, nested, and endpoint-level 200/400 cases without that control. Weighing craft of the fix and user-facing error quality over A's marginally better tests, B is the better patch.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

15m 34s

Wall-clock time in the container

Full test suite

Not run

The harness did not reach the test stage

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

222vs 116 merged

Changed lines, ours vs merged

Description

POST /v1/messages/count_tokens answers HTTP 500 when a message contains a search_result content block, which is a documented Messages content block (Search results). The same happens for any block type the local token counter does not know, so a client that counts a conversation before sending it (as coding agents do for context management) gets a server error instead of a count or a clean refusal.

Expected: count_tokens accepts the same content as /v1/messages and returns {"input_tokens": n} (for search_result, counting its title, source and content text, as the other text-bearing blocks are counted); a block type it cannot count is refused with a 400 invalid_request_error naming the type, per the Anthropic Token counting guide ("supports the same structured list of inputs" as Messages). Never a 500.

Observed on LiteLLM 1.105.1 / vLLM 0.31.0 dev build, 2026-10-22. (On 1.105.1 the body is FastAPI's {"detail": ...}; #46815 moves it to the Anthropic envelope in 1.105.3 / 1.106.2, but the status stays whatever the route maps a ValueError to.)

Config

model_list:
  - model_name: glm-5.3
    litellm_params:
      model: hosted_vllm/zai-org/GLM-5.3
      api_base: http://<vllm-host>:8000/v1

LiteLLM Version

v1.105.1

Steps to Repro

  1. A search_result block:
curl -s -w '\nHTTP %{http_code}\n' http://localhost:4000/v1/messages/count_tokens \
  -H "Authorization: Bearer $LITELLM_API_KEY" -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' \
  -d '{"model":"glm-5.3","messages":[{"role":"user","content":[{"type":"search_result","source":"https://docs.example.invalid/notes","title":"Project notes","content":[{"type":"text","text":"The code word is 7f3a."}]},{"type":"text","text":"Reply with only the code word you were given, or NONE."}]}]}'

HTTP 500:

{"detail":{"error":"Internal server error: Error getting number of tokens from content list: Invalid content item type: search_result. Expected str or dict with 'type' field (text, image_url, image, document, file, tool_use, tool_result, thinking, redacted_thinking, tool_reference)., default_token_count=None"}}
  1. A block type the counter does not list (a stand-in for a block introduced by a newer client):
curl -s -w '\nHTTP %{http_code}\n' http://localhost:4000/v1/messages/count_tokens \
  -H "Authorization: Bearer $LITELLM_API_KEY" -H 'anthropic-version: 2023-06-01' -H 'content-type: application/json' \
  -d '{"model":"glm-5.3","messages":[{"role":"user","content":[{"type":"text","text":"Reply with the single word: ok"},{"type":"future_block","future_block":{"value":"ignore me"}}]}]}'

HTTP 500, same message with Invalid content item type: future_block.

  1. Expected: (1) HTTP 200 {"input_tokens": <n>}; (2) HTTP 400 {"type":"error","error":{"type":"invalid_request_error","message":"... future_block ..."}}. Control on the same proxy: the same request with a document, image, tool_result or redacted_thinking block instead returns 200 with a count.

Possible cause (a lead, not a confirmed cause): _count_content_list() in litellm/litellm_core_utils/token_counter.py handles text, image_url, image, document, file, tool_use, tool_result, thinking, redacted_thinking and tool_reference, and raises ValueError("Invalid content item type: ...") for anything else; the route turns that into a 500. On main the list is the same. A search_result branch (count title, source and the text in content), and mapping the remaining ValueError to a 400 rather than a 500, would cover both cases. We have not run main, where #46772 added a 400 for blocks the OpenAI and Gemini count conversions cannot handle; it is not clear to us whether that path is taken for a locally counted hosted_vllm deployment.

Related: #44784 (open, same error for compaction blocks), #47858 (open, tool_addition / tool_removal), #46772 (merged), #46815 (merged, error envelope).

Which part of LiteLLM is this about?

Proxy core: startup, config, health checks, endpoints

How are you deploying?

Other

The two patches

Our patch+211 −11
litellm/litellm_core_utils/token_counter.pyindex edb5ecf0f8..33b38ca497 100644@@ -52,6 +52,7 @@ from litellm.types.llms.anthropic import (     AnthropicContentParamSourceUrl,     AnthropicMessagesDocumentParam,     AnthropicMessagesImageParam,+    AnthropicMessagesSearchResultParam,     AnthropicMessagesTextParam,     AnthropicMessagesToolResultParam,     AnthropicMessagesToolUseParam,@@ -67,6 +68,17 @@ from litellm.types.llms.openai import ( from litellm.types.utils import Message, SelectTokenizerResponse  +class UncountableContentError(ValueError):+    """A content block whose type the local token counter does not know how to count.++    The request names a block type the counter can't price, so callers answer it as the+    client's invalid request rather than as a counting failure of their own."""++    def __init__(self, message: str, content_type: str) -> None:+        super().__init__(message)+        self.content_type: Final = content_type++ def get_modified_max_tokens(     model: str,     base_model: str,@@ -975,6 +987,21 @@ def _count_file_tokens(     return name_tokens + _count_opaque_document_tokens(file_data, count_function, use_default_image_token_count)  +def _count_search_result_tokens(+    search_result: AnthropicMessagesSearchResultParam,+    count_function: TokenCounterFunction,+    use_default_image_token_count: bool,+    default_token_count: int | None,+) -> int:+    """An Anthropic `search_result` block puts its `title`, `source` and `content` text in the prompt."""+    metadata_tokens: Final = sum(+        count_function(text) for text in (search_result.get("title"), search_result.get("source")) if text+    )+    return metadata_tokens + _count_content_list(+        count_function, search_result.get("content") or (), use_default_image_token_count, default_token_count+    )++ def _count_anthropic_content(     content: Mapping[str, object],     count_function: TokenCounterFunction,@@ -1020,6 +1047,8 @@ def _count_anthropic_content(         except Exception as e:             if default_token_count is not None:                 return default_token_count+            if isinstance(e, UncountableContentError):+                raise UncountableContentError(f"Error counting field '{field_name}': {e}", e.content_type) from e             raise ValueError(f"Error counting field '{field_name}': {e}")     return tokens @@ -1034,6 +1063,7 @@ def _count_content_list(         | AnthropicMessagesTextParam         | AnthropicMessagesImageParam         | AnthropicMessagesDocumentParam+        | AnthropicMessagesSearchResultParam     ],     use_default_image_token_count: bool,     default_token_count: int | None,@@ -1092,18 +1122,31 @@ def _count_content_list(                 tool_name = str(c.get("tool_name") or "")                 if tool_name:                     num_tokens += count_function(tool_name)+            elif c["type"] == "search_result":+                num_tokens += _count_search_result_tokens(+                    c,+                    count_function,+                    use_default_image_token_count,+                    default_token_count,+                )             else:                 content_type = c.get("type", type(c).__name__) if isinstance(c, dict) else type(c).__name__-                raise ValueError(+                raise UncountableContentError(                     f"Invalid content item type: {content_type}. "                     f"Expected str or dict with 'type' field "                     f"(text, image_url, image, document, file, tool_use, tool_result, thinking, redacted_thinking, "-                    f"tool_reference)."+                    f"tool_reference, search_result).",+                    str(content_type),                 )         return num_tokens     except Exception as e:         if default_token_count is not None:             return default_token_count+        if isinstance(e, UncountableContentError):+            raise UncountableContentError(+                f"Error getting number of tokens from content list: {e}, default_token_count={default_token_count}",+                e.content_type,+            ) from e         raise ValueError(             f"Error getting number of tokens from content list: {e}, default_token_count={default_token_count}"         )litellm/proxy/proxy_server.pyindex 3b36ea5afc..d8b2e35492 100644@@ -100,7 +100,7 @@ from litellm.litellm_core_utils.litellm_logging import ( ) from litellm.litellm_core_utils.safe_json_dumps import safe_dumps from litellm.litellm_core_utils.safe_json_loads import safe_json_loads-from litellm.litellm_core_utils.token_counter import offload_token_count+from litellm.litellm_core_utils.token_counter import UncountableContentError, offload_token_count from litellm.llms.bedrock.count_tokens.kimi_k3 import (     HUGGING_FACE_SOURCE,     KimiK3Encodings,@@ -15443,14 +15443,22 @@ async def count_request_tokens(             tokenizer_type="huggingface_tokenizer",             count_source="local_estimate",         )-    estimate: Final = await _local_token_estimate(-        model=model_to_use,-        prompt=prompt if counted_messages is None else None,-        messages=counted_messages,-        tools=counted_tools,-        custom_tokenizer=custom_tokenizer,-        provider_failure=ladder,-    )+    try:+        estimate: Final = await _local_token_estimate(+            model=model_to_use,+            prompt=prompt if counted_messages is None else None,+            messages=counted_messages,+            tools=counted_tools,+            custom_tokenizer=custom_tokenizer,+            provider_failure=ladder,+        )+    except UncountableContentError as e:+        raise ProxyException(+            message=f"Token counting does not support content blocks of type '{e.content_type}'",+            type="invalid_request_error",+            param="messages",+            code=400,+        ) from e     template: Final = (         0 if kimi_k3_messages is None else kimi_k3_template_tokens(len(kimi_k3_messages), bool(counted_tools))     )litellm/types/llms/anthropic.pyindex ba422a0a90..eb3261604f 100644@@ -341,6 +341,17 @@ class AnthropicMessagesDocumentParam(TypedDict, total=False):     citations: CitationsObject | None  +class AnthropicMessagesSearchResultParam(TypedDict, total=False):+    """https://docs.anthropic.com/en/docs/build-with-claude/search-results"""++    type: Required[Literal["search_result"]]+    source: Required[str]+    title: Required[str]+    content: Required[list[AnthropicMessagesTextParam]]+    cache_control: ChatCompletionCachedContent | None+    citations: CitationsObject | None++ class AnthropicMessagesToolResultContent(TypedDict, total=False):     type: Required[Literal["text"]]     text: Required[str]tests/unit/litellm_core_utils/test_token_counter.pyindex 9921bbe343..fd82b8c9ef 100644@@ -1691,6 +1691,70 @@ def test_count_content_list_rejects_unknown_type():     assert "tool_reference" in message  +def test_count_content_list_flags_an_unknown_type_as_uncountable_content():+    from litellm.litellm_core_utils.token_counter import UncountableContentError, _count_content_list++    with pytest.raises(UncountableContentError) as exc_info:+        _count_content_list(+            count_function=len,+            content_list=[+                {"type": "tool_result", "tool_use_id": "toolu_1", "content": [{"type": "future_block"}]}+            ],+            use_default_image_token_count=False,+            default_token_count=None,+        )++    assert exc_info.value.content_type == "future_block"+++def test_count_content_list_counts_search_result_title_source_and_content():+    from litellm.litellm_core_utils.token_counter import _count_content_list++    search_result = {+        "type": "search_result",+        "source": "https://docs.example.invalid/notes",+        "title": "Project notes",+        "content": [{"type": "text", "text": "The code word is 7f3a."}],+        "citations": {"enabled": True},+    }++    tokens = _count_content_list(+        count_function=len,+        content_list=[search_result],+        use_default_image_token_count=False,+        default_token_count=None,+    )++    assert tokens == len("Project notes") + len("https://docs.example.invalid/notes") + len("The code word is 7f3a.")+++def test_token_counter_counts_a_search_result_inside_a_tool_result():+    messages = [+        {+            "role": "user",+            "content": [+                {+                    "type": "tool_result",+                    "tool_use_id": "toolu_1",+                    "content": [+                        {+                            "type": "search_result",+                            "source": "https://docs.example.invalid/notes",+                            "title": "Project notes",+                            "content": [{"type": "text", "text": "The code word is 7f3a."}],+                        }+                    ],+                }+            ],+        }+    ]+    empty_tool_result = [{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": []}]}]++    assert token_counter_new(model="gpt-4o", messages=messages) > token_counter_new(+        model="gpt-4o", messages=empty_tool_result+    )++ @pytest.mark.parametrize(     "source",     [tests/unit/proxy/anthropic_endpoints/test_endpoints.pyindex c7bb05e5d3..4794ae124d 100644@@ -5,6 +5,7 @@ Test for anthropic_endpoints/endpoints.py, focusing on handling dictionary objec import json import logging import unittest+from collections.abc import Mapping from typing import Final from unittest.mock import AsyncMock, MagicMock, patch @@ -615,6 +616,79 @@ async def test_count_tokens_drops_malformed_tools_the_counted_deployment_drops_e     assert counter.await_args.kwargs["request"].tools is None  +def _hosted_vllm_router() -> Router:+    return Router(+        model_list=[+            {+                "model_name": "glm-local",+                "litellm_params": {"model": "hosted_vllm/zai-org/GLM-5.3", "api_base": "http://vllm.invalid:8000/v1"},+            }+        ]+    )+++async def _count_locally(body: Mapping[str, object]) -> object:+    import litellm.proxy.anthropic_endpoints.endpoints as ep+    import litellm.proxy.proxy_server as proxy_server+    from litellm.proxy._types import UserAPIKeyAuth++    request: Final = MagicMock()+    request.headers = {}+    with (+        patch.object(ep, "read_request_body", new=AsyncMock(return_value=body)),  # test-quality-ok: endpoint reads the body via a module function; no injection seam+        patch.object(proxy_server, "llm_router", new=_hosted_vllm_router()),  # test-quality-ok: module global imported at call time; no injection seam on this route+    ):+        return await ep.count_tokens(request=request, user_api_key_dict=UserAPIKeyAuth())+++_INSTRUCTION_BLOCK: Final = {"type": "text", "text": "Reply with only the code word you were given, or NONE."}+++@pytest.mark.asyncio+async def test_count_tokens_counts_a_search_result_block_for_a_locally_counted_deployment() -> None:+    search_result: Final = {+        "type": "search_result",+        "source": "https://docs.example.invalid/notes",+        "title": "Project notes",+        "content": [{"type": "text", "text": "The code word is 7f3a."}],+    }++    with_search_result: Final = await _count_locally(+        {"model": "glm-local", "messages": [{"role": "user", "content": [search_result, _INSTRUCTION_BLOCK]}]}+    )+    without_search_result: Final = await _count_locally(+        {"model": "glm-local", "messages": [{"role": "user", "content": [_INSTRUCTION_BLOCK]}]}+    )++    assert isinstance(with_search_result, dict) and isinstance(without_search_result, dict)+    assert with_search_result["input_tokens"] > without_search_result["input_tokens"]+++@pytest.mark.asyncio+@pytest.mark.parametrize(+    "content",+    [+        [{"type": "text", "text": "Reply with the single word: ok"}, {"type": "future_block", "future_block": {"value": "ignore me"}}],+        [+            {+                "type": "tool_result",+                "tool_use_id": "toolu_1",+                "content": [{"type": "future_block", "future_block": {"value": "ignore me"}}],+            }+        ],+    ],+    ids=["top_level", "inside_tool_result"],+)+async def test_count_tokens_answers_400_naming_a_block_type_it_cannot_count(content: list[object]) -> None:+    response: Final = await _count_locally({"model": "glm-local", "messages": [{"role": "user", "content": content}]})++    assert getattr(response, "status_code", None) == 400+    body: Final = json.loads(response.body)+    assert body["type"] == "error"+    assert body["error"]["type"] == "invalid_request_error"+    assert "future_block" in body["error"]["message"]++ class TestEventLoggingBatchEndpoint:     """Test the stubbed event logging batch endpoint"""  
Merged fix · PR #48699+111 −5
litellm/litellm_core_utils/token_counter.pyindex edb5ecf0f8d..6d50e4b065e 100644@@ -55,6 +55,7 @@ from litellm.types.llms.anthropic import (     AnthropicMessagesTextParam,     AnthropicMessagesToolResultParam,     AnthropicMessagesToolUseParam,+    AnthropicSearchResultParam, ) from litellm.types.llms.openai import (     AllMessageValues,@@ -67,6 +68,10 @@ from litellm.types.llms.openai import ( from litellm.types.utils import Message, SelectTokenizerResponse  +class UnsupportedContentTypeError(ValueError):+    pass++ def get_modified_max_tokens(     model: str,     base_model: str,@@ -865,6 +870,7 @@ def _validate_anthropic_content(content: Mapping[str, object]) -> type:     mapping: Final = {         "tool_use": AnthropicMessagesToolUseParam,         "tool_result": AnthropicMessagesToolResultParam,+        "search_result": AnthropicSearchResultParam,     }      expected_cls: Final = mapping.get(content_type) if isinstance(content_type, str) else None@@ -1020,7 +1026,9 @@ def _count_anthropic_content(         except Exception as e:             if default_token_count is not None:                 return default_token_count-            raise ValueError(f"Error counting field '{field_name}': {e}")+            raise (UnsupportedContentTypeError if isinstance(e, UnsupportedContentTypeError) else ValueError)(+                f"Error counting field '{field_name}': {e}"+            )     return tokens  @@ -1034,6 +1042,7 @@ def _count_content_list(         | AnthropicMessagesTextParam         | AnthropicMessagesImageParam         | AnthropicMessagesDocumentParam+        | AnthropicSearchResultParam     ],     use_default_image_token_count: bool,     default_token_count: int | None,@@ -1068,7 +1077,7 @@ def _count_content_list(                     count_function,                     use_default_image_token_count,                 )-            elif c["type"] in ("tool_use", "tool_result"):+            elif c["type"] in ("tool_use", "tool_result", "search_result"):                 num_tokens += _count_anthropic_content(                     c,                     count_function,@@ -1094,17 +1103,18 @@ def _count_content_list(                     num_tokens += count_function(tool_name)             else:                 content_type = c.get("type", type(c).__name__) if isinstance(c, dict) else type(c).__name__-                raise ValueError(+                raise UnsupportedContentTypeError(                     f"Invalid content item type: {content_type}. "                     f"Expected str or dict with 'type' field "                     f"(text, image_url, image, document, file, tool_use, tool_result, thinking, redacted_thinking, "-                    f"tool_reference)."+                    f"tool_reference, search_result)."                 )         return num_tokens     except Exception as e:         if default_token_count is not None:             return default_token_count-        raise ValueError(+        error_type: Final = UnsupportedContentTypeError if isinstance(e, UnsupportedContentTypeError) else ValueError+        raise error_type(             f"Error getting number of tokens from content list: {e}, default_token_count={default_token_count}"         ) litellm/proxy/anthropic_endpoints/endpoints.pyindex 3d2f89be15f..13a1844a6de 100644@@ -18,6 +18,7 @@ from litellm.anthropic_interface.exceptions import (     AnthropicExceptionMapping, ) from litellm.integrations.custom_guardrail import ModifyResponseException+from litellm.litellm_core_utils.token_counter import UnsupportedContentTypeError from litellm.litellm_core_utils.tool_fields import first_validation_error from litellm.llms.anthropic.messages.context_management import (     AnthropicContextManagementError,@@ -413,6 +414,10 @@ async def count_tokens(             messages_format="anthropic",             **_count_tokens_body_options(data, auto_drop_params),         )+    except UnsupportedContentTypeError as e:+        return _anthropic_error_json_response(+            ProxyException(message=str(e), type="invalid_request_error", param="messages", code=400), request+        )     except HTTPException as e:         return _anthropic_error_json_response(proxy_exception_from_http_exception(e, {}), request)     except ProxyException as e:litellm/types/llms/anthropic.pyindex ba422a0a901..de83e6ea66e 100644@@ -209,6 +209,13 @@ class AnthropicMessagesTextParam(TypedDict, total=False):     prompt_cache_breakpoint: ReadOnly[PromptCacheBreakpoint]  +class AnthropicSearchResultParam(TypedDict, total=False):+    type: Required[ReadOnly[Literal["search_result"]]]+    source: Required[ReadOnly[str]]+    title: Required[ReadOnly[str]]+    content: Required[ReadOnly[Iterable[AnthropicMessagesTextParam]]]++ class AnthropicMessagesToolUseParam(TypedDict, total=False):     type: Required[Literal["tool_use"]]     id: str@@ -357,6 +364,7 @@ class AnthropicMessagesToolResultParam(TypedDict, total=False):             AnthropicMessagesToolResultContent             | AnthropicMessagesImageParam             | AnthropicMessagesDocumentParam+            | AnthropicSearchResultParam             | ToolReference         ]     )@@ -368,6 +376,7 @@ AnthropicMessagesUserMessageValues = (     | AnthropicMessagesImageParam     | AnthropicMessagesToolResultParam     | AnthropicMessagesDocumentParam+    | AnthropicSearchResultParam     | AnthropicMessagesContainerUploadParam ) tests/integration/providers/test_count_tokens_error_shapes_wire.pyindex 200f5e81673..a6c3a6dc62f 100644@@ -68,6 +68,60 @@ def _never_called(request: Request) -> Reply:     raise AssertionError(f"count reached the provider without a key: {request.target}")  +@pytest.mark.parametrize("nested", (False, True), ids=("user-content", "tool-result"))+def test_local_messages_count_accepts_search_result_text(gateway: Gateway, nested: bool) -> None:+    search: Final[JsonValue] = {+        "type": "search_result",+        "source": "https://example.invalid/notes",+        "title": "Project notes",+        "content": [{"type": "text", "text": "The code word is 7f3a."}],+        "citations": {"enabled": True},+    }+    content: Final = [{"type": "tool_result", "tool_use_id": "toolu_1", "content": [search]}] if nested else [search]+    text: Final[list[JsonValue]] = [+        {"type": "text", "text": "https://example.invalid/notes"},+        {"type": "text", "text": "Project notes"},+        {"type": "text", "text": "The code word is 7f3a."},+    ]+    equivalent: Final = [{"type": "tool_result", "tool_use_id": "toolu_1", "content": text}] if nested else text+    with gateway.scenario() as scenario:+        model: Final = scenario.model(model="hosted_vllm/zai-org/GLM-5.3", api_base="http://127.0.0.1:9/v1")+        counted: Final = gateway.request(+            "POST", "/v1/messages/count_tokens", {"model": model, "messages": [{"role": "user", "content": content}]}+        )+        control: Final = gateway.request(+            "POST", "/v1/messages/count_tokens", {"model": model, "messages": [{"role": "user", "content": equivalent}]}+        )+        assert counted.status_code == control.status_code == 200, (counted.text, control.text)+        tokens: Final = _JSON_OBJECT.validate_json(counted.content)["input_tokens"]+        assert tokens == _JSON_OBJECT.validate_json(control.content)["input_tokens"], (counted.text, control.text)+++@pytest.mark.parametrize("nested", (False, True), ids=("user-content", "tool-result"))+@pytest.mark.parametrize(+    ("block", "status", "error_type", "message"),+    (+        ({"type": "future_block", "future_block": {"value": "ignore me"}}, 400, "invalid_request_error", "future_block"),+        ({"type": "image"}, 500, "api_error", "source"),+    ),+    ids=("unknown-block", "unrelated-counting-error"),+)+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+) -> None:+    content: Final = [{"type": "tool_result", "tool_use_id": "toolu_1", "content": [block]}] if nested else [block]+    with gateway.scenario() as scenario:+        model: Final = scenario.model(model="hosted_vllm/zai-org/GLM-5.3", api_base="http://127.0.0.1:9/v1")+        response: Final = gateway.request(+            "POST", "/v1/messages/count_tokens", {"model": model, "messages": [{"role": "user", "content": content}]}+        )+        assert response.status_code == status, response.text+        payload: Final = _JSON_OBJECT.validate_json(response.content)+        error: Final = payload["error"]+        assert payload["type"] == "error" and isinstance(error, dict), payload+        assert error["type"] == error_type and message in str(error["message"]), payload++ @pytest.mark.parametrize(     ("deployment", "missing"),     (tests/unit/litellm_core_utils/test_token_counter.pyindex 9921bbe343a..92f971f64e9 100644@@ -1825,6 +1825,34 @@ def test_anthropic_image_block_with_empty_base64_data():     assert tokens > 0  +def test_token_counter_counts_anthropic_search_result_text() -> None:+    from litellm.litellm_core_utils.token_counter import _count_content_list++    search_result: Final = {+        "type": "search_result",+        "source": "https://example.invalid/notes",+        "title": "Project notes",+        "content": [{"type": "text", "text": "The code word is 7f3a."}],+        "citations": {"enabled": True},+    }++    assert _count_content_list(len, [search_result], False, None) == len(+        "https://example.invalid/notesProject notesThe code word is 7f3a."+    )+++@pytest.mark.parametrize("nested", (False, True))+def test_unknown_content_type_remains_a_value_error_with_an_optional_fallback(nested: bool) -> None:+    from litellm.litellm_core_utils.token_counter import _count_content_list++    unknown: Final = {"type": "future_block"}+    content: Final = [{"type": "tool_result", "tool_use_id": "toolu_1", "content": [unknown]}] if nested else [unknown]++    with pytest.raises(ValueError, match="Invalid content item type: future_block"):+        _count_content_list(len, content, False, None)+    assert _count_content_list(len, content, False, 7) == 7++ def test_anthropic_image_block_without_source_raises():     """An `image` block with no `source` raises, matching the OpenAI `image_url`-without-`url` behavior."""     from litellm.litellm_core_utils.token_counter import _count_content_list