From 3c5cb293d64c689c761342b75950d3cde9abbb37 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 13 Oct 2026 15:15:29 +0800 Subject: [PATCH] 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}}"