diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index 9e168f14401..980a1d8c0eb 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -23,6 +23,7 @@ from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM from litellm.llms.bedrock_mantle.common_utils import ( BEDROCK_MANTLE_DEFAULT_REGION, BedrockMantleAuthMixin, + strip_unsupported_web_search_tool_fields, ) from litellm.llms.openai.chat.gpt_reasoning_transformation import get_effort_level, is_gpt_reasoning_series_name from litellm.secret_managers.main import get_secret_str @@ -197,13 +198,19 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): drop_params, replace_max_completion_tokens_with_max_tokens and not gpt_reasoning_model, ) - if not gpt_reasoning_model or "max_tokens" not in mapped_params: - return mapped_params - return { - param: value - for param, value in (("max_completion_tokens", mapped_params["max_tokens"]), *mapped_params.items()) - if param != "max_tokens" - } + renamed_params: Final = ( + { + param: value + for param, value in (("max_completion_tokens", mapped_params["max_tokens"]), *mapped_params.items()) + if param != "max_tokens" + } + if gpt_reasoning_model and "max_tokens" in mapped_params + else mapped_params + ) + tools: Final = renamed_params.get("tools") + if tools is None: + return renamed_params + return {**renamed_params, "tools": strip_unsupported_web_search_tool_fields(tools)} def transform_request( self, diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 75e34cf3ae5..f35c71a7536 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -13,9 +13,9 @@ global state. """ import re -from collections.abc import Mapping +from collections.abc import Mapping, Sequence from functools import partial -from typing import Final, Literal +from typing import Final, Literal, cast # noqa: TID251 # untyped OpenAI tool JSON after isinstance narrowing from litellm.litellm_core_utils.optional_imports import ensure_optional_import from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM, SignsRequestsWithAWS @@ -168,3 +168,48 @@ def mantle_base_segment(model: str | None, model_cost: dict) -> str: """ entry: Final = model_cost.get(f"bedrock_mantle/{split_mantle_region_prefix(model)[1]}", {}) if model else {} return "openai/v1" if entry.get("use_openai_responses_path") is True else "v1" + + +_WEB_SEARCH_TOOL_TYPE_PREFIX: Final = "web_search" +_UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) + + +def strip_unsupported_web_search_tool_keys(tool: object) -> object: + """Drop Mantle-rejected keys (e.g. ``search_content_types``) from one web-search tool. + + Non-dict tools, non-web-search tool types, and tools with nothing to drop are + returned as the same object. + """ + if not isinstance(tool, dict): + return tool + typed_tool: Final = cast( # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types + dict[str, object], tool + ) + tool_type: Final = typed_tool.get("type") + if ( + not isinstance(tool_type, str) + or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX) + or not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool) + ): + return typed_tool + return {key: value for key, value in typed_tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS} + + +def strip_unsupported_web_search_tool_fields(tools: object) -> object: + """Drop Mantle-rejected nested fields from web-search tool definitions. + + Bedrock Mantle accepts ``web_search`` tools but rejects OpenAI/Codex fields such + as ``search_content_types`` (validation_error 400). ``additional_drop_params`` only + reaches top-level request keys, so nested tool fields need an explicit strip. + Non-dict tools and non-web-search tool types are left unchanged. Returns the + original object when nothing needs rewriting so callers can keep identity. + """ + if not isinstance(tools, (list, tuple)): + return tools + tools_seq: Final = cast( # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright + Sequence[object], tools + ) + rewritten: Final = tuple(strip_unsupported_web_search_tool_keys(tool) for tool in tools_seq) + if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): + return tools_seq + return list(rewritten) diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index 3532cfbd963..9180bcdbd70 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -31,6 +31,7 @@ from litellm.llms.bedrock.common_utils import BedrockError from litellm.llms.bedrock_mantle.common_utils import ( MANTLE_HOST_RE, BedrockMantleAuthMixin, + strip_unsupported_web_search_tool_keys, ) from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig from litellm.responses.additional_tools import HoistedAdditionalTools, hoist_additional_tools @@ -54,7 +55,15 @@ _BASE_SUFFIXES_TO_STRIP: Final = ( # Per Bedrock Mantle Responses API validation errors. _BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES: Final = frozenset( - {"function", "mcp", "custom", "namespace", "tool_search", "web_search"} + { + "function", + "mcp", + "custom", + "namespace", + "tool_search", + "web_search", + "web_search_preview", + } ) _BEDROCK_MANTLE_SUPPORTED_SERVICE_TIERS: Final = frozenset({"auto", "default"}) @@ -120,7 +129,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI @staticmethod def _filter_unsupported_tools(tools: "Sequence[object]") -> "list[object]": - """Keep only tool types Mantle's Responses API accepts.""" + """Keep only tool types Mantle's Responses API accepts and strip rejected fields.""" kept, dropped_types = drop_unsupported_tools(tools, _BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES) if dropped_types: verbose_logger.warning( @@ -128,7 +137,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI list(dropped_types), sorted(_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES), ) - return list(kept) + return [strip_unsupported_web_search_tool_keys(tool) for tool in kept] @staticmethod def _handle_unsupported_service_tier(params: dict, drop_params: bool) -> dict: diff --git a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py index a2289167ad4..8173015bd66 100644 --- a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py +++ b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py @@ -1895,3 +1895,80 @@ class TestBedrockMantleResponsesPricing: def test_models_registered(self, local_cost_map): assert "bedrock_mantle/openai.gpt-5.5" in litellm.bedrock_mantle_models assert "bedrock_mantle/openai.gpt-5.4" in litellm.bedrock_mantle_models + + +class TestBedrockMantleResponsesStripSearchContentTypes: + """Codex/OpenAI may send search_content_types on web_search tools; Mantle 400s.""" + + def test_map_openai_params_strips_search_content_types_from_web_search(self): + cfg = BedrockMantleResponsesAPIConfig() + params = cfg.map_openai_params( + response_api_optional_params={ + "tools": [ + { + "type": "web_search", + "external_web_access": False, + "search_content_types": ["text"], + } + ] + }, + model="openai.gpt-5.6-sol", + drop_params=False, + ) + assert params["tools"] == [{"type": "web_search", "external_web_access": False}] + + def test_map_openai_params_strips_search_content_types_from_web_search_preview(self): + cfg = BedrockMantleResponsesAPIConfig() + params = cfg.map_openai_params( + response_api_optional_params={ + "tools": [{"type": "web_search_preview", "search_content_types": ["text"]}] + }, + model="openai.gpt-5.6-sol", + drop_params=False, + ) + assert params["tools"] == [{"type": "web_search_preview"}] + + def test_other_web_search_options_are_preserved(self): + cfg = BedrockMantleResponsesAPIConfig() + tool = {"type": "web_search", "external_web_access": False} + params = cfg.map_openai_params( + response_api_optional_params={"tools": [tool]}, + model="openai.gpt-5.6-sol", + drop_params=False, + ) + assert params["tools"] == [tool] + + def test_non_web_search_tools_keep_unrelated_fields(self): + cfg = BedrockMantleResponsesAPIConfig() + tool = {"type": "function", "name": "exec_command", "search_content_types": ["text"]} + params = cfg.map_openai_params( + response_api_optional_params={"tools": [tool]}, + model="openai.gpt-5.6-sol", + drop_params=False, + ) + assert params["tools"] == [tool] + + def test_stripped_tool_reaches_outbound_body(self): + cfg = BedrockMantleResponsesAPIConfig() + params = cfg.map_openai_params( + response_api_optional_params={ + "tools": [ + { + "type": "web_search_preview", + "search_content_types": ["text"], + } + ] + }, + model="openai.gpt-5.6-sol", + drop_params=False, + ) + body = cfg.transform_responses_api_request( + model="openai.gpt-5.6-sol", + input="What did AWS announce today?", + response_api_optional_request_params=params, + litellm_params=GenericLiteLLMParams(), + headers={}, + ) + assert body["tools"] == [{"type": "web_search_preview"}] + assert "search_content_types" not in body["tools"][0] + diff --git a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py index f252d9886d3..75c05522624 100644 --- a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py +++ b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py @@ -1254,3 +1254,22 @@ def test_mantle_signing_reports_only_missing_aws_dependency(missing): assert error.value.__cause__ is failure else: assert error.value is failure + + +class TestBedrockMantleChatStripSearchContentTypes: + def test_map_openai_params_strips_search_content_types(self): + cfg = BedrockMantleChatConfig() + mapped = cfg.map_openai_params( + non_default_params={ + "tools": [ + { + "type": "web_search_preview", + "search_content_types": ["text"], + } + ] + }, + optional_params={}, + model="openai.gpt-5.5", + drop_params=False, + ) + assert mapped["tools"] == [{"type": "web_search_preview"}]