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 <delweng@gmail.com>
This commit is contained in:
Delweng 2026-10-13 15:15:29 +08:00
parent ba987b927d
commit 3c5cb293d6

View file

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