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} = (_: {{")