From ba987b927dfc5323c8ac3e367ffa81312261b5d3 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 13 Oct 2026 14:04:20 +0800 Subject: [PATCH 1/5] test(token_counter): cover array tool params with no items schema An MCP tool can declare {"type": "array"} with no items sub-schema, and JSON Schema permits it, so token_counter must not index a missing key. Parametrizes the three tool shapes that reach _format_type and asserts the count matches the equivalent empty-items tool, plus the rendering itself and the non-object items forms draft 4 and draft 6 allow Signed-off-by: Delweng --- .../litellm_core_utils/test_token_counter.py | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index 194d7995735..80750d4a76e 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -9,7 +9,7 @@ import subprocess import sys import threading import time -from collections.abc import Callable, Mapping +from collections.abc import Callable, Mapping, Sequence from concurrent.futures import Future, ThreadPoolExecutor, wait from itertools import accumulate, chain from pathlib import Path @@ -31,6 +31,7 @@ from litellm.constants import HUGGINGFACE_TOKENIZER_LOAD_RETRY_SECONDS, TOKEN_CO from litellm.litellm_core_utils.asyncify import asyncify from litellm.litellm_core_utils.token_counter import ( _encoding_count, + _format_type, _get_exact_count_function, _get_extrapolating_count_function, _get_tiktoken_count_function, @@ -696,6 +697,70 @@ def test_token_counter_counts_the_tools_system_turn_only_without_a_system_messag assert system_cost_with_tools == system_cost_without_tools - implicit_system_turn_tokens +def _openai_tool_with_properties(properties: Mapping[str, object]) -> Sequence[Mapping[str, object]]: + return ( + { + "type": "function", + "function": { + "name": "list_things", + "description": "List the things", + "parameters": {"type": "object", "properties": properties}, + }, + }, + ) + + +def _anthropic_tool_with_properties(properties: Mapping[str, object]) -> Sequence[Mapping[str, object]]: + return ( + { + "name": "list_things", + "description": "List the things", + "input_schema": {"type": "object", "properties": properties}, + }, + ) + + +def _object_nested_tool_with_properties(properties: Mapping[str, object]) -> Sequence[Mapping[str, object]]: + return _openai_tool_with_properties({"group": {"type": "object", "properties": properties}}) + + +@pytest.mark.parametrize( + "tool_shape", + [ + pytest.param(_openai_tool_with_properties, id="openai_function_parameters"), + pytest.param(_anthropic_tool_with_properties, id="anthropic_input_schema"), + pytest.param(_object_nested_tool_with_properties, id="array_nested_in_object_property"), + ], +) +def test_token_counter_counts_array_parameter_without_items_schema( + tool_shape: Callable[[Mapping[str, object]], Sequence[Mapping[str, object]]], +): + messages: Final = [{"role": "user", "content": "What's on the list?"}] + omitted_items: Final = tool_shape({"things": {"type": "array", "description": "The things on the list"}}) + empty_items: Final = tool_shape( + {"things": {"type": "array", "description": "The things on the list", "items": {}}} + ) + + assert token_counter_new( + model="gpt-4o", messages=messages, tools=omitted_items + ) == token_counter_new(model="gpt-4o", messages=messages, tools=empty_items) + + +def test_format_type_renders_array_without_items_like_empty_items(): + assert _format_type({"type": "array"}, 0) == _format_type({"type": "array", "items": {}}, 0) == "any[]" + + +@pytest.mark.parametrize( + "items", + [ + pytest.param([{"type": "string"}], id="draft4_tuple_validation"), + pytest.param(True, id="draft6_boolean_true_schema"), + pytest.param(False, id="draft6_boolean_false_schema"), + ], +) +def test_format_type_renders_non_object_array_items_as_any_array(items: object): + assert _format_type({"type": "array", "items": items}, 0) == "any[]" + class NeedsToleranceUpdateError(Exception): """Custom exception to mark tests that have improved""" From 3c5cb293d64c689c761342b75950d3cde9abbb37 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 13 Oct 2026 15:15:29 +0800 Subject: [PATCH 2/5] fix(token_counter): count array tool params without an items schema JSON Schema lets an array omit items and MCP tool definitions do exactly that, so indexing props['items'] raised KeyError and turned a token count into a 500 on POST /v1/messages/count_tokens. Render a missing items as the same any[] an empty items already produced, and drop the comment claiming items was required. The new guard keeps a non-object items, which draft 4 tuple validation and draft 6 boolean schemas both allow, rendering as any instead of raising AttributeError Signed-off-by: Delweng --- litellm/litellm_core_utils/token_counter.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 8aded69aed2..31b4a81160f 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -1171,13 +1171,16 @@ def _format_object_parameters(parameters, indent): def _format_type(props, indent): + if not isinstance(props, Mapping): + return "any" type: Final = props.get("type") if type == "string": if "enum" in props: return " | ".join([f'"{item}"' for item in props["enum"]]) return "string" elif type == "array": - # items is required, OpenAI throws an error if it's missing + if "items" not in props: + return f"{_format_type({}, indent)}[]" return f"{_format_type(props['items'], indent)}[]" elif type == "object": return f"{{\n{_format_object_parameters(props, indent + 2)}\n}}" From 05c8637ce6bfbc672db57048e7c073e4a59dac0c Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 13 Oct 2026 15:46:28 +0800 Subject: [PATCH 3/5] fix(token_counter): return a constant for an array param with no items The previous commit reached the missing items case by recursing on an empty dict. That dict has no type key, so _format_type falls through to its else branch and returns the literal any, which makes the recursive call indistinguishable from the constant while handing an untyped empty dict to an unannotated parameter basedpyright counts that call as reportUnknownArgumentType, and scripts/type_check_gate.py fails any rule whose codebase-wide total grows past its merge-base count, with only reportAny and reportExplicitAny exempt. The codebase-wide total for this file now sits one below the merge-base count at 9621a60d27 instead of matching it, and the self-recursion is gone Signed-off-by: Delweng --- litellm/litellm_core_utils/token_counter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 31b4a81160f..8b8ea2a5ad3 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -1180,7 +1180,7 @@ def _format_type(props, indent): return "string" elif type == "array": if "items" not in props: - return f"{_format_type({}, indent)}[]" + return "any[]" return f"{_format_type(props['items'], indent)}[]" elif type == "object": return f"{{\n{_format_object_parameters(props, indent + 2)}\n}}" From a5648c66e12a894cf30bd13666cfebd35b5837ca Mon Sep 17 00:00:00 2001 From: Delweng Date: Sun, 18 Oct 2026 13:54:44 +0800 Subject: [PATCH 4/5] test(proxy): drop the array fixture that no longer breaks the local estimate The framed-estimate test asserts that when the provider count fails and the local estimate also cannot read the request, the proxy answers with the provider's error. Its array_schema_without_items fixture only provoked that second failure because an untyped array raised KeyError, which the previous commit stopped doing, so the request now counts successfully and the assert that the error surfaces no longer applies. The boolean_subschema_tool fixture covers the same contract and still fails the local estimate, so the coverage is unchanged. Signed-off-by: Delweng --- tests/unit/proxy/test_proxy_token_counter.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/unit/proxy/test_proxy_token_counter.py b/tests/unit/proxy/test_proxy_token_counter.py index fa0c9eda02d..337ec95f5c3 100644 --- a/tests/unit/proxy/test_proxy_token_counter.py +++ b/tests/unit/proxy/test_proxy_token_counter.py @@ -1944,15 +1944,6 @@ async def test_framing_keeps_the_400_for_blocks_the_provider_counter_cannot_conv }, id="boolean_subschema_tool", ), - pytest.param( - { - "messages": [{"role": "user", "content": "What is new?"}], - "tools": [ - {"name": "search", "input_schema": {"type": "object", "properties": {"tags": {"type": "array"}}}} - ], - }, - id="array_schema_without_items", - ), pytest.param( {"messages": [], "contents": [{"role": "user", "parts": [{"text": "What is new?"}]}]}, id="google_contents", From af59a823b7bcc4be90b36fc5927ae57d3905c225 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 23 Oct 2026 21:02:13 +0800 Subject: [PATCH 5/5] style(test): match the formatter on the rebased token counter tests Fixes the two spots the conflict resolution left off-form: the blank line before the tool-shape helpers and the wrapping of the equality assertion. No test content changes. --- tests/unit/litellm_core_utils/test_token_counter.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index 80750d4a76e..f07961817db 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -697,6 +697,7 @@ def test_token_counter_counts_the_tools_system_turn_only_without_a_system_messag assert system_cost_with_tools == system_cost_without_tools - implicit_system_turn_tokens + def _openai_tool_with_properties(properties: Mapping[str, object]) -> Sequence[Mapping[str, object]]: return ( { @@ -737,13 +738,11 @@ def test_token_counter_counts_array_parameter_without_items_schema( ): messages: Final = [{"role": "user", "content": "What's on the list?"}] omitted_items: Final = tool_shape({"things": {"type": "array", "description": "The things on the list"}}) - empty_items: Final = tool_shape( - {"things": {"type": "array", "description": "The things on the list", "items": {}}} - ) + empty_items: Final = tool_shape({"things": {"type": "array", "description": "The things on the list", "items": {}}}) - assert token_counter_new( - model="gpt-4o", messages=messages, tools=omitted_items - ) == token_counter_new(model="gpt-4o", messages=messages, tools=empty_items) + assert token_counter_new(model="gpt-4o", messages=messages, tools=omitted_items) == token_counter_new( + model="gpt-4o", messages=messages, tools=empty_items + ) def test_format_type_renders_array_without_items_like_empty_items():