diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 1f90d375bc2..c1c1f0c27fe 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -715,6 +715,53 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): ) return _tool_choice + @staticmethod + def _is_first_party_anthropic_api_base(api_base: object) -> bool: + """True when api_base is absent or points at the first-party Anthropic API.""" + if not isinstance(api_base, str) or not api_base: + return True + return api_base.startswith("https://api.anthropic.com") + + @staticmethod + def _strip_custom_tool_type_for_compat_endpoints( + optional_params: dict, + litellm_params: dict | None, + ) -> None: + """Drop `type: "custom"` from user-defined tools when the deployment + targets a third-party Anthropic-compatible endpoint (custom api_base). + + `map_openai_params` stamps every mapped tool with `type: "custom"` + (see `_map_tool_helper`). First-party Anthropic accepts that marker, + but compatible endpoints (DeepSeek /anthropic, Z.AI, MiniMax, ...) + reject the request outright: + "Failed to deserialize the JSON body into the target type: + tools[0]: unknown variant `custom`, expected + `web_search_20250305` or `web_search_20260209`" + + The classic untyped {name, description, input_schema} shape is + accepted by the first-party API and every compatible endpoint, so + this only ever widens compatibility. + """ + tools = optional_params.get("tools") + if not isinstance(tools, list) or not tools: + return + api_base = litellm_params.get("api_base") if isinstance(litellm_params, dict) else None + if AnthropicConfig._is_first_party_anthropic_api_base(api_base): + return + stripped_tools = [] + stripped_any = False + for tool in tools: + if isinstance(tool, dict) and tool.get("type") == "custom": + tool = {key: value for key, value in tool.items() if key != "type"} + stripped_any = True + stripped_tools.append(tool) + if stripped_any: + optional_params["tools"] = stripped_tools + litellm.verbose_logger.debug( + "AnthropicCompat: dropped type='custom' from user-defined tools for non-first-party api_base=%s", + api_base, + ) + def _map_tool_helper( self, tool: ChatCompletionToolParam, @@ -1959,6 +2006,20 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if _name_reverse_map and isinstance(litellm_params, dict): litellm_params[ANTHROPIC_TOOL_NAME_REVERSE_MAP_KEY] = _name_reverse_map + # Anthropic-compatible third-party endpoints (e.g. DeepSeek's + # /anthropic, Z.AI, MiniMax) implement an older subset of the tools + # schema: their deserializers reject `type: "custom"` on user-defined + # tools ("unknown variant `custom`, expected `web_search_20250305` or + # `web_search_20260209`"). The typed form is only meaningful for the + # first-party API, so drop the marker when the deployment overrides + # api_base to a non-Anthropic host. The classic untyped + # {name, description, input_schema} shape is accepted everywhere, + # including the first-party API. + self._strip_custom_tool_type_for_compat_endpoints( + optional_params=optional_params, + litellm_params=litellm_params, + ) + # Separate system prompt from rest of message anthropic_system_message_list: Final = self.translate_system_message(messages=messages) # Handling anthropic API Prompt Caching diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py index 633dd1d9460..7d463ee9eef 100644 --- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py +++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py @@ -6451,3 +6451,58 @@ def test_eager_input_streaming_reaches_anthropic_request_tools(): assert result["tools"][0]["eager_input_streaming"] is True assert result["tools"][0]["name"] == "write_file" + + +def _sample_openai_function_tools(): + return [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get the weather", + "parameters": { + "type": "object", + "properties": {"city": {"type": "string"}}, + "required": ["city"], + }, + }, + } + ] + + +def test_custom_tool_type_stripped_for_third_party_api_base(): + """Third-party Anthropic-compatible endpoints (e.g. DeepSeek /anthropic) + reject `type: "custom"` on user-defined tools. When the deployment + overrides api_base to a non-Anthropic host, transform_request must emit + the classic untyped tool shape instead.""" + config = AnthropicConfig() + optional_params = {"tools": config._map_tools(_sample_openai_function_tools())[0]} + assert optional_params["tools"][0]["type"] == "custom" # mapped form today + + result = config.transform_request( + model="claude-sonnet-4-20250514", + messages=[{"role": "user", "content": "Hello"}], + optional_params=optional_params, + litellm_params={"api_base": "https://api.deepseek.com/anthropic"}, + headers={}, + ) + + assert "type" not in result["tools"][0] + assert result["tools"][0]["name"] == "get_weather" + assert result["tools"][0]["input_schema"]["properties"]["city"] == {"type": "string"} + + +def test_custom_tool_type_kept_for_first_party_api_base(): + """First-party requests (no api_base override) keep the typed form.""" + config = AnthropicConfig() + optional_params = {"tools": config._map_tools(_sample_openai_function_tools())[0]} + + result = config.transform_request( + model="claude-sonnet-4-20250514", + messages=[{"role": "user", "content": "Hello"}], + optional_params=optional_params, + litellm_params={}, + headers={}, + ) + + assert result["tools"][0]["type"] == "custom"