From 622ee9e4dc62c47320cc7696dbef5afea698775b Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Thu, 1 Oct 2026 09:27:30 +0300 Subject: [PATCH 01/11] fix(bedrock_mantle): strip search_content_types from web search tools Bedrock Mantle rejects nested search_content_types on web_search / web_search_preview tools with a validation_error 400. additional_drop_params only reaches top-level keys, so strip the field in the Mantle chat and Responses transforms and keep web_search_preview among supported Responses tool types. Fixes #43964 Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../bedrock_mantle/chat/transformation.py | 21 +++++ litellm/llms/bedrock_mantle/common_utils.py | 36 +++++++++ .../responses/transformation.py | 16 +++- ...bedrock_mantle_responses_transformation.py | 77 +++++++++++++++++++ .../test_bedrock_mantle_transformation.py | 20 +++++ 5 files changed, 167 insertions(+), 3 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index 590919f1fb0..7356ffa515a 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -21,6 +21,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_5_transformation import is_gpt_reasoning_series_name from litellm.secret_managers.main import get_secret_str @@ -126,6 +127,26 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): verbose_logger.debug("BedrockMantleChatConfig: error checking reasoning support: %s", e) return False + def map_openai_params( + self, + non_default_params: dict, + optional_params: dict, + model: str, + drop_params: bool, + replace_max_completion_tokens_with_max_tokens: bool = True, + ) -> dict: + mapped_params: Final = super().map_openai_params( + non_default_params=non_default_params, + optional_params=optional_params, + model=model, + drop_params=drop_params, + replace_max_completion_tokens_with_max_tokens=replace_max_completion_tokens_with_max_tokens, + ) + tools: Final = mapped_params.get("tools") + if tools is not None: + mapped_params["tools"] = strip_unsupported_web_search_tool_fields(tools) + return mapped_params + def get_model_response_iterator( self, streaming_response: Iterator[str] | AsyncIterator[str] | Any, diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 9892ef6224e..bf5e48c2fec 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -158,3 +158,39 @@ 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_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): + return tools + + rewritten = False + cleaned: list[object] = [] # mutable-ok: build a fresh tools list only when stripping + for tool in tools: + if not isinstance(tool, dict): + cleaned.append(tool) + continue + tool_type = tool.get("type") + if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): + cleaned.append(tool) + continue + if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): + cleaned.append(tool) + continue + rewritten = True + cleaned.append( + {key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS} + ) + return cleaned if rewritten else tools + diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index 3ac29f2d1c1..ccf1cdb9a6e 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -29,6 +29,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_fields, ) from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig from litellm.responses.additional_tools import HoistedAdditionalTools, hoist_additional_tools @@ -52,7 +53,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"}) @@ -115,7 +124,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( @@ -123,7 +132,8 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI list(dropped_types), sorted(_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES), ) - return list(kept) + stripped = strip_unsupported_web_search_tool_fields(list(kept)) + return list(stripped) if isinstance(stripped, list) else list(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 951911ac066..20ca98ff3f8 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 @@ -1863,3 +1863,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 4bf3dd11fa1..575f21b759e 100644 --- a/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py +++ b/tests/unit/llms/bedrock_mantle/test_bedrock_mantle_transformation.py @@ -861,3 +861,23 @@ async def test_mantle_signing_runs_off_the_event_loop(): assert "Authorization" in signed assert probe.served_during_refresh is True + + +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"}] + From 16e2aef6f7879d106947d0d1e0eea8d18132ef87 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Thu, 1 Oct 2026 13:18:53 +0300 Subject: [PATCH 02/11] fix(bedrock_mantle): ruff format and avoid in-place tools mutation Satisfy LiteLLM lint ruff format on common_utils, and return fresh dicts from Mantle chat/Responses map_openai_params when stripping unsupported web-search tool fields (AGENTS.md no-mutation). Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- litellm/llms/bedrock_mantle/chat/transformation.py | 9 ++++++--- litellm/llms/bedrock_mantle/common_utils.py | 6 ++---- litellm/llms/bedrock_mantle/responses/transformation.py | 7 ++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index 7356ffa515a..fc004392468 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -143,9 +143,12 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): replace_max_completion_tokens_with_max_tokens=replace_max_completion_tokens_with_max_tokens, ) tools: Final = mapped_params.get("tools") - if tools is not None: - mapped_params["tools"] = strip_unsupported_web_search_tool_fields(tools) - return mapped_params + if tools is None: + return mapped_params + return { + **mapped_params, + "tools": strip_unsupported_web_search_tool_fields(tools), + } # mutable-ok: map_openai_params contract returns a plain dict def get_model_response_iterator( self, diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index bf5e48c2fec..bdf85582da1 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -159,6 +159,7 @@ 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"}) @@ -189,8 +190,5 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: cleaned.append(tool) continue rewritten = True - cleaned.append( - {key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS} - ) + cleaned.append({key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS}) return cleaned if rewritten else tools - diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index ccf1cdb9a6e..99bb6ab6b20 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -264,8 +264,5 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI tools_list: Final = tools if isinstance(tools, list) else [tools] filtered: Final = self._filter_unsupported_tools(tools_list) if filtered: - params["tools"] = filtered - else: - params.pop("tools", None) - - return params + return {**params, "tools": filtered} # mutable-ok: map_openai_params contract returns a plain dict + return {key: value for key, value in params.items() if key != "tools"} From 6c86d02c021f81b95ef458b724855d85ad2111b8 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Thu, 1 Oct 2026 13:30:38 +0300 Subject: [PATCH 03/11] fix(bedrock_mantle): satisfy type-discipline gate for tools strip Rewrite web-search field stripping functionally (tuple map + list only when rewritten), place mutable-ok on the dict-construction lines, and drop unnecessary list() copies in the Responses filter path. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../bedrock_mantle/chat/transformation.py | 4 +-- litellm/llms/bedrock_mantle/common_utils.py | 36 +++++++++---------- .../responses/transformation.py | 10 ++++-- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index fc004392468..b2df612e16c 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -145,10 +145,10 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): tools: Final = mapped_params.get("tools") if tools is None: return mapped_params - return { + return { # mutable-ok: map_openai_params contract returns a plain dict **mapped_params, "tools": strip_unsupported_web_search_tool_fields(tools), - } # mutable-ok: map_openai_params contract returns a plain dict + } def get_model_response_iterator( self, diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index bdf85582da1..323ab33ea9f 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -164,6 +164,19 @@ _WEB_SEARCH_TOOL_TYPE_PREFIX: Final = "web_search" _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) +def _strip_one_web_search_tool(tool: object) -> object: + if not isinstance(tool, dict): + return tool + tool_type = tool.get("type") + if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): + return tool + if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): + return tool + return { # mutable-ok: OpenAI tool JSON object after dropping Mantle-rejected keys + key: value for key, value in 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. @@ -173,22 +186,9 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: 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): + if not isinstance(tools, (list, tuple)): return tools - - rewritten = False - cleaned: list[object] = [] # mutable-ok: build a fresh tools list only when stripping - for tool in tools: - if not isinstance(tool, dict): - cleaned.append(tool) - continue - tool_type = tool.get("type") - if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): - cleaned.append(tool) - continue - if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): - cleaned.append(tool) - continue - rewritten = True - cleaned.append({key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS}) - return cleaned if rewritten else tools + rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools) + if all(new is old for new, old in zip(rewritten, tools, strict=True)): + return tools + return list(rewritten) # mutable-ok: chat/Responses tools param is a JSON list diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index 99bb6ab6b20..2041606fec6 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -132,8 +132,10 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI list(dropped_types), sorted(_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES), ) - stripped = strip_unsupported_web_search_tool_fields(list(kept)) - return list(stripped) if isinstance(stripped, list) else list(kept) + stripped = strip_unsupported_web_search_tool_fields(kept) + if isinstance(stripped, list): + return stripped + return [*kept] # mutable-ok: inherited filter return type is list[object] @staticmethod def _handle_unsupported_service_tier(params: dict, drop_params: bool) -> dict: @@ -265,4 +267,6 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI filtered: Final = self._filter_unsupported_tools(tools_list) if filtered: return {**params, "tools": filtered} # mutable-ok: map_openai_params contract returns a plain dict - return {key: value for key, value in params.items() if key != "tools"} + return { # mutable-ok: map_openai_params contract returns a plain dict + key: value for key, value in params.items() if key != "tools" + } From 813ffd2cb0d0812746f129ed78a906a4d88388bd Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 2 Oct 2026 13:23:47 +0300 Subject: [PATCH 04/11] fix(bedrock_mantle): silence reportUnknownArgumentType on tools strip Cast bare-dict tool JSON and list/tuple tools sequences so the basedpyright budget gate stops blaming this change for +4 reportUnknownArgumentType. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../llms/bedrock_mantle/chat/transformation.py | 5 +++-- litellm/llms/bedrock_mantle/common_utils.py | 18 +++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index b2df612e16c..c6a254f2191 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -11,7 +11,7 @@ Auth: Bearer token (litellm_params.api_key, BEDROCK_MANTLE_API_KEY, or the """ from collections.abc import AsyncIterator, Iterator -from typing import Any, Final +from typing import Any, Final, cast import httpx @@ -147,7 +147,8 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): return mapped_params return { # mutable-ok: map_openai_params contract returns a plain dict **mapped_params, - "tools": strip_unsupported_web_search_tool_fields(tools), + # cast-ok: mapped_params is a bare dict so .get is Unknown to basedpyright + "tools": strip_unsupported_web_search_tool_fields(cast(object, tools)), } def get_model_response_iterator( diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 323ab33ea9f..6c2febf08cc 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -13,8 +13,8 @@ global state. """ import re -from collections.abc import Mapping -from typing import Final +from collections.abc import Mapping, Sequence +from typing import Final, cast from botocore.exceptions import ( CredentialRetrievalError, @@ -167,13 +167,15 @@ _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) def _strip_one_web_search_tool(tool: object) -> object: if not isinstance(tool, dict): return tool - tool_type = tool.get("type") + # cast-ok: OpenAI tool JSON object; isinstance(dict) does not bind key/value types + typed_tool: Final = cast(dict[str, object], tool) + tool_type = typed_tool.get("type") if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): return tool - if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): + if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool): return tool return { # mutable-ok: OpenAI tool JSON object after dropping Mantle-rejected keys - key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS + key: value for key, value in typed_tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS } @@ -188,7 +190,9 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: """ if not isinstance(tools, (list, tuple)): return tools - rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools) - if all(new is old for new, old in zip(rewritten, tools, strict=True)): + # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright + tools_seq: Final = cast(Sequence[object], tools) + rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) + if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): return tools return list(rewritten) # mutable-ok: chat/Responses tools param is a JSON list From 946ab4ce7265b121ebed9a3ab4b06e0706145feb Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:28:25 +0300 Subject: [PATCH 05/11] fix(bedrock_mantle): drop casts from web_search tools strip Avoid LIT006/LIT013 by mirroring drop_unsupported_tools isinstance(dict) narrowing instead of cast()/off-line cast-ok markers. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../bedrock_mantle/chat/transformation.py | 5 ++--- litellm/llms/bedrock_mantle/common_utils.py | 19 ++++++++----------- .../responses/transformation.py | 2 +- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index c6a254f2191..b2df612e16c 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -11,7 +11,7 @@ Auth: Bearer token (litellm_params.api_key, BEDROCK_MANTLE_API_KEY, or the """ from collections.abc import AsyncIterator, Iterator -from typing import Any, Final, cast +from typing import Any, Final import httpx @@ -147,8 +147,7 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): return mapped_params return { # mutable-ok: map_openai_params contract returns a plain dict **mapped_params, - # cast-ok: mapped_params is a bare dict so .get is Unknown to basedpyright - "tools": strip_unsupported_web_search_tool_fields(cast(object, tools)), + "tools": strip_unsupported_web_search_tool_fields(tools), } def get_model_response_iterator( diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 6c2febf08cc..a13864bc49b 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -13,8 +13,8 @@ global state. """ import re -from collections.abc import Mapping, Sequence -from typing import Final, cast +from collections.abc import Mapping +from typing import Final from botocore.exceptions import ( CredentialRetrievalError, @@ -165,17 +165,16 @@ _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) def _strip_one_web_search_tool(tool: object) -> object: + # Mirror drop_unsupported_tools: isinstance(dict) is enough for .get/.items. if not isinstance(tool, dict): return tool - # cast-ok: OpenAI tool JSON object; isinstance(dict) does not bind key/value types - typed_tool: Final = cast(dict[str, object], tool) - tool_type = typed_tool.get("type") + tool_type: Final = tool.get("type") if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): return tool - if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool): + if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): return tool return { # mutable-ok: OpenAI tool JSON object after dropping Mantle-rejected keys - key: value for key, value in typed_tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS + key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS } @@ -190,9 +189,7 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: """ if not isinstance(tools, (list, tuple)): return tools - # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright - tools_seq: Final = cast(Sequence[object], tools) - rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) - if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): + rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools) + if all(new is old for new, old in zip(rewritten, tools, strict=True)): return tools return list(rewritten) # mutable-ok: chat/Responses tools param is a JSON list diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index 2041606fec6..6e16b975a3a 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -132,7 +132,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI list(dropped_types), sorted(_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES), ) - stripped = strip_unsupported_web_search_tool_fields(kept) + stripped: Final = strip_unsupported_web_search_tool_fields(kept) if isinstance(stripped, list): return stripped return [*kept] # mutable-ok: inherited filter return type is list[object] From c395f3c86b26a86d7d6704ae16b46340acbc4fef Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:38:14 +0300 Subject: [PATCH 06/11] fix(bedrock_mantle): same-line cast-ok for tools strip LIT006/LIT013 need cast-ok on the cast() line itself; off-line markers counted as unused while still burning the cast budget. Restores casts so basedpyright reportUnknownArgumentType stays flat. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../llms/bedrock_mantle/chat/transformation.py | 7 +++++-- litellm/llms/bedrock_mantle/common_utils.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index b2df612e16c..5bfd72489c6 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -11,7 +11,7 @@ Auth: Bearer token (litellm_params.api_key, BEDROCK_MANTLE_API_KEY, or the """ from collections.abc import AsyncIterator, Iterator -from typing import Any, Final +from typing import Any, Final, cast import httpx @@ -145,9 +145,12 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): tools: Final = mapped_params.get("tools") if tools is None: return mapped_params + stripped_tools: Final = strip_unsupported_web_search_tool_fields( + cast(object, tools), # cast-ok: bare dict .get is Unknown to basedpyright + ) return { # mutable-ok: map_openai_params contract returns a plain dict **mapped_params, - "tools": strip_unsupported_web_search_tool_fields(tools), + "tools": stripped_tools, } def get_model_response_iterator( diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index a13864bc49b..0d44a6c86ba 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -13,8 +13,8 @@ global state. """ import re -from collections.abc import Mapping -from typing import Final +from collections.abc import Mapping, Sequence +from typing import Final, cast from botocore.exceptions import ( CredentialRetrievalError, @@ -165,16 +165,16 @@ _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) def _strip_one_web_search_tool(tool: object) -> object: - # Mirror drop_unsupported_tools: isinstance(dict) is enough for .get/.items. if not isinstance(tool, dict): return tool - tool_type: Final = tool.get("type") + typed_tool: Final = cast(dict[str, object], tool) # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types + tool_type: Final = typed_tool.get("type") if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): return tool - if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(tool): + if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool): return tool return { # mutable-ok: OpenAI tool JSON object after dropping Mantle-rejected keys - key: value for key, value in tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS + key: value for key, value in typed_tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS } @@ -189,7 +189,8 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: """ if not isinstance(tools, (list, tuple)): return tools - rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools) - if all(new is old for new, old in zip(rewritten, tools, strict=True)): + tools_seq: Final = cast(Sequence[object], tools) # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright + rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) + if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): return tools return list(rewritten) # mutable-ok: chat/Responses tools param is a JSON list From a29a63ae8313bd90f8b9096074efd45bf76067da Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:45:54 +0300 Subject: [PATCH 07/11] style(bedrock_mantle): ruff format tools strip helpers Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- litellm/llms/bedrock_mantle/common_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 0d44a6c86ba..27e3d4934bc 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -167,7 +167,9 @@ _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) def _strip_one_web_search_tool(tool: object) -> object: if not isinstance(tool, dict): return tool - typed_tool: Final = cast(dict[str, object], tool) # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types + typed_tool: Final = cast( + dict[str, object], tool + ) # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types tool_type: Final = typed_tool.get("type") if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): return tool @@ -189,7 +191,9 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: """ if not isinstance(tools, (list, tuple)): return tools - tools_seq: Final = cast(Sequence[object], tools) # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright + tools_seq: Final = cast( + Sequence[object], tools + ) # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): return tools From 8dbd8f4dc6819e9e88f98abb8ab0fd0007f60d80 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:47:05 +0300 Subject: [PATCH 08/11] fix(bedrock_mantle): keep cast-ok on cast( opener after ruff Ruff wraps long cast() calls; put cast-ok on the cast( line so LIT006 is suppressed and LIT013 does not fire on a trailing comment line. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- litellm/llms/bedrock_mantle/common_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 27e3d4934bc..94854595d01 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -167,9 +167,9 @@ _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) def _strip_one_web_search_tool(tool: object) -> object: if not isinstance(tool, dict): return tool - typed_tool: Final = cast( + typed_tool: Final = cast( # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types dict[str, object], tool - ) # cast-ok: OpenAI tool JSON; isinstance(dict) does not bind key/value types + ) tool_type: Final = typed_tool.get("type") if not isinstance(tool_type, str) or not tool_type.startswith(_WEB_SEARCH_TOOL_TYPE_PREFIX): return tool @@ -191,9 +191,9 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: """ if not isinstance(tools, (list, tuple)): return tools - tools_seq: Final = cast( + tools_seq: Final = cast( # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright Sequence[object], tools - ) # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright + ) rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): return tools From a0bf02c73b97ecfb95c5e9d7676065d62f9d50a9 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:30:43 +0100 Subject: [PATCH 09/11] fix(bedrock_mantle): ruff format map_openai_params merge Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- litellm/llms/bedrock_mantle/chat/transformation.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index 648c7509c4b..329fbb5d5c1 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -144,12 +144,14 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): replace_max_completion_tokens_with_max_tokens: bool = True, ) -> dict[str, object]: # mutable-ok: OpenAILikeChatConfig.map_openai_params signature gpt_reasoning_model: Final = is_gpt_reasoning_series_name(model) - mapped_params: dict[str, object] = super().map_openai_params( # mutable-ok: may remap max_tokens then strip tools # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # OpenAILikeChatConfig types its params and result as bare dicts # mutable-ok: parent returns a dict - non_default_params, - optional_params, - model, - drop_params, - replace_max_completion_tokens_with_max_tokens and not gpt_reasoning_model, + mapped_params: dict[str, object] = ( + super().map_openai_params( # mutable-ok: may remap max_tokens then strip tools # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # OpenAILikeChatConfig types its params and result as bare dicts # mutable-ok: parent returns a dict + non_default_params, + optional_params, + model, + drop_params, + replace_max_completion_tokens_with_max_tokens and not gpt_reasoning_model, + ) ) if gpt_reasoning_model and "max_tokens" in mapped_params: mapped_params = { From 474b60959b776cff44329307e1e7b12f4539d30b Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:23:00 +0100 Subject: [PATCH 10/11] fix(bedrock_mantle): satisfy strict ruff and type-discipline gates Drop the redundant typing.cast in the chat config (mapped_params is already dict[str, object]), keep common_utils' cast behind a reasoned TID251 noqa like the Responses config, bind the reasoning-model rename to a Final local instead of rebinding, and remove mutable-ok markers that suppressed nothing (LIT013). Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../bedrock_mantle/chat/transformation.py | 35 ++++++++----------- litellm/llms/bedrock_mantle/common_utils.py | 8 ++--- .../responses/transformation.py | 8 ++--- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/litellm/llms/bedrock_mantle/chat/transformation.py b/litellm/llms/bedrock_mantle/chat/transformation.py index 329fbb5d5c1..2edb163b29c 100644 --- a/litellm/llms/bedrock_mantle/chat/transformation.py +++ b/litellm/llms/bedrock_mantle/chat/transformation.py @@ -11,7 +11,7 @@ Auth: Bearer token (litellm_params.api_key, BEDROCK_MANTLE_API_KEY, or the """ from collections.abc import AsyncIterator, Iterator -from typing import TYPE_CHECKING, Final, cast +from typing import TYPE_CHECKING, Final import httpx @@ -144,31 +144,26 @@ class BedrockMantleChatConfig(BedrockMantleAuthMixin, OpenAILikeChatConfig): replace_max_completion_tokens_with_max_tokens: bool = True, ) -> dict[str, object]: # mutable-ok: OpenAILikeChatConfig.map_openai_params signature gpt_reasoning_model: Final = is_gpt_reasoning_series_name(model) - mapped_params: dict[str, object] = ( - super().map_openai_params( # mutable-ok: may remap max_tokens then strip tools # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # OpenAILikeChatConfig types its params and result as bare dicts # mutable-ok: parent returns a dict - non_default_params, - optional_params, - model, - drop_params, - replace_max_completion_tokens_with_max_tokens and not gpt_reasoning_model, - ) + mapped_params: Final[dict[str, object]] = super().map_openai_params( # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # OpenAILikeChatConfig types its params and result as bare dicts # mutable-ok: parent returns a dict + non_default_params, + optional_params, + model, + drop_params, + replace_max_completion_tokens_with_max_tokens and not gpt_reasoning_model, ) - if gpt_reasoning_model and "max_tokens" in mapped_params: - mapped_params = { + renamed_params: Final = ( + { param: value for param, value in (("max_completion_tokens", mapped_params["max_tokens"]), *mapped_params.items()) if param != "max_tokens" } - tools = mapped_params.get("tools") - if tools is None: - return mapped_params - stripped_tools: Final = strip_unsupported_web_search_tool_fields( - cast(object, tools), # cast-ok: bare dict .get is Unknown to basedpyright + if gpt_reasoning_model and "max_tokens" in mapped_params + else mapped_params ) - return { # mutable-ok: map_openai_params contract returns a plain dict - **mapped_params, - "tools": stripped_tools, - } + 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 _supports_reasoning(self, model: str) -> bool: try: diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 9d44afeba4e..50f5908b0ea 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -15,7 +15,7 @@ global state. import re from collections.abc import Mapping, Sequence from functools import partial -from typing import Final, Literal, cast +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 @@ -185,9 +185,7 @@ def _strip_one_web_search_tool(tool: object) -> object: return tool if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool): return tool - return { # mutable-ok: OpenAI tool JSON object after dropping Mantle-rejected keys - key: value for key, value in typed_tool.items() if key not in _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS - } + 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: @@ -207,4 +205,4 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) if all(new is old for new, old in zip(rewritten, tools_seq, strict=True)): return tools - return list(rewritten) # mutable-ok: chat/Responses tools param is a JSON list + return list(rewritten) diff --git a/litellm/llms/bedrock_mantle/responses/transformation.py b/litellm/llms/bedrock_mantle/responses/transformation.py index 3cb2809753e..180df763bbd 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -135,7 +135,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI stripped: Final = strip_unsupported_web_search_tool_fields(kept) if isinstance(stripped, list): return stripped - return [*kept] # mutable-ok: inherited filter return type is list[object] + return [*kept] @staticmethod def _handle_unsupported_service_tier(params: dict, drop_params: bool) -> dict: @@ -262,7 +262,5 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI tools_list: Final = tools if isinstance(tools, list) else [tools] filtered: Final = self._filter_unsupported_tools(tools_list) if filtered: - return {**params, "tools": filtered} # mutable-ok: map_openai_params contract returns a plain dict - return { # mutable-ok: map_openai_params contract returns a plain dict - key: value for key, value in params.items() if key != "tools" - } + return {**params, "tools": filtered} + return {key: value for key, value in params.items() if key != "tools"} From 57d9d46b250468a5d11afbc72af4a0c9065d3b5a Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:35:08 +0100 Subject: [PATCH 11/11] fix(bedrock_mantle): keep web-search tool strip within the basedpyright gate Return the typed views (typed_tool / tools_seq) instead of the isinstance-narrowed originals, strip Responses tools per item from the typed kept tuple, and restore main's in-place tools update in map_openai_params so no new Unknown types are introduced. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- litellm/llms/bedrock_mantle/common_utils.py | 21 ++++++++++++------- .../responses/transformation.py | 14 ++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.py index 50f5908b0ea..f35c71a7536 100644 --- a/litellm/llms/bedrock_mantle/common_utils.py +++ b/litellm/llms/bedrock_mantle/common_utils.py @@ -174,17 +174,24 @@ _WEB_SEARCH_TOOL_TYPE_PREFIX: Final = "web_search" _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS: Final = frozenset({"search_content_types"}) -def _strip_one_web_search_tool(tool: object) -> object: +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): - return tool - if not _UNSUPPORTED_WEB_SEARCH_TOOL_FIELDS.intersection(typed_tool): - return tool + 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} @@ -202,7 +209,7 @@ def strip_unsupported_web_search_tool_fields(tools: object) -> object: tools_seq: Final = cast( # cast-ok: isinstance(list|tuple) does not bind element types for basedpyright Sequence[object], tools ) - rewritten: Final = tuple(_strip_one_web_search_tool(tool) for tool in tools_seq) + 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 + 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 180df763bbd..00bb7d4c922 100644 --- a/litellm/llms/bedrock_mantle/responses/transformation.py +++ b/litellm/llms/bedrock_mantle/responses/transformation.py @@ -29,7 +29,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_fields, + strip_unsupported_web_search_tool_keys, ) from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig from litellm.responses.additional_tools import HoistedAdditionalTools, hoist_additional_tools @@ -132,10 +132,7 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI list(dropped_types), sorted(_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES), ) - stripped: Final = strip_unsupported_web_search_tool_fields(kept) - if isinstance(stripped, list): - return stripped - return [*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: @@ -262,5 +259,8 @@ class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPI tools_list: Final = tools if isinstance(tools, list) else [tools] filtered: Final = self._filter_unsupported_tools(tools_list) if filtered: - return {**params, "tools": filtered} - return {key: value for key, value in params.items() if key != "tools"} + params["tools"] = filtered + else: + params.pop("tools", None) + + return params