From ac06e2f3b4d66675013d820574db3227816bda04 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Mon, 25 May 2026 19:19:57 +0530 Subject: [PATCH] fix(token_counter): guard None input_schema in Anthropic tool fallback Use `or {}` instead of `.get(..., {})` so explicit null parameters do not raise AttributeError when formatting function definitions for token counting. Co-authored-by: Cursor --- litellm/litellm_core_utils/token_counter.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 5e6df8d9d95..819bdaf7645 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -769,15 +769,20 @@ def _format_function_definitions(tools): function = tool.get("function") if function is None: # Anthropic tool shape → OpenAI function dict for token counting. + params = tool.get("input_schema") or tool.get("parameters") or {} + if not isinstance(params, dict): + params = {} function = { "name": tool.get("name"), "description": tool.get("description"), - "parameters": tool.get("input_schema", tool.get("parameters", {})), + "parameters": params, } if function_description := function.get("description"): lines.append(f"// {function_description}") function_name = function.get("name") - parameters = function.get("parameters", {}) + parameters = function.get("parameters") or {} + if not isinstance(parameters, dict): + parameters = {} properties = parameters.get("properties") if properties and properties.keys(): lines.append(f"type {function_name} = (_: {{")