Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge origin/litellm_internal_staging into litellm_mantle_codex_additional_tools (resolve overlap with #33228 hoist)

mateo-berrifadec17a0f45
diff --cc litellm/llms/bedrock_mantle/responses/transformation.py
index 04cab10f2e3,eb818b26f1f..08579b6bf0d
--- a/litellm/llms/bedrock_mantle/responses/transformation.py
+++ b/litellm/llms/bedrock_mantle/responses/transformation.py
@@@ -43,8 -45,8 +46,10 @@@ _BASE_SUFFIXES_TO_STRIP =
# Per Bedrock Mantle Responses API validation errors.
_BEDROCK_MANTLE_SUPPORTED_RESPONSE_TOOL_TYPES = frozenset({"function", "mcp", "custom", "namespace", "tool_search"})
+_BEDROCK_MANTLE_SUPPORTED_SERVICE_TIERS = frozenset({"auto", "default"})
+
+ _CODEX_ADDITIONAL_TOOLS_INPUT_ITEM_TYPE = "additional_tools"
+
class BedrockMantleResponsesAPIConfig(BedrockMantleAuthMixin, OpenAIResponsesAPIConfig):
def __init__(
@@@ -119,28 -121,70 +124,92 @@@
return kept
+ @staticmethod
+ def _handle_unsupported_service_tier(params: dict, drop_params: bool) -> dict:
+ service_tier = params.get("service_tier")
+ if service_tier is None or service_tier in _BEDROCK_MANTLE_SUPPORTED_SERVICE_TIERS:
+ return params
+ if not drop_params:
+ raise litellm.utils.UnsupportedParamsError(
+ status_code=400,
+ message=(
+ f"bedrock_mantle does not support service_tier={service_tier!r}; the Bedrock Mantle "
+ "Responses API only accepts 'auto' or 'default'. Set `drop_params: true` (litellm_settings "
+ "or this deployment's litellm_params) to have LiteLLM drop it, or remove service_tier from "
+ "the client (Codex CLI sends it when a speed tier is set in ~/.codex/config.toml)."
+ ),
+ )
+ verbose_logger.warning(
+ "Bedrock Mantle Responses API: dropping unsupported service_tier %r (supported: %s).",
+ service_tier,
+ sorted(_BEDROCK_MANTLE_SUPPORTED_SERVICE_TIERS),
+ )
+ return {key: value for key, value in params.items() if key != "service_tier"}
+
+ def transform_responses_api_request(
+ self,
+ model: str,
+ input: "str | ResponseInputParam",
+ response_api_optional_request_params: dict,
+ litellm_params: GenericLiteLLMParams,
+ headers: dict,
+ ) -> dict:
+ remaining_input, hoisted_tools = self._hoist_codex_additional_tools(input)
+ request_params = (
+ {
+ **response_api_optional_request_params,
+ "tools": [
+ *(response_api_optional_request_params.get("tools") or []),
+ *hoisted_tools,
+ ],
+ }
+ if hoisted_tools
+ else response_api_optional_request_params
+ )
+ return super().transform_responses_api_request(
+ model=model,
+ input=remaining_input,
+ response_api_optional_request_params=request_params,
+ litellm_params=litellm_params,
+ headers=headers,
+ )
+
+ @staticmethod
+ def _is_codex_additional_tools_item(item: Any) -> bool:
+ return isinstance(item, dict) and item.get("type") == _CODEX_ADDITIONAL_TOOLS_INPUT_ITEM_TYPE
+
+ @staticmethod
+ def _tools_of_additional_tools_item(item: "dict[str, Any]") -> "list[Any]":
+ tools = item.get("tools")
+ return tools if isinstance(tools, list) else []
+
+ @classmethod
+ def _hoist_codex_additional_tools(
+ cls,
+ input: "str | ResponseInputParam",
+ ) -> "tuple[str | ResponseInputParam, list[Any]]":
+ """Codex's "responses lite" wire mode ships tool definitions inside
+ `input` as {"type": "additional_tools", "role": "developer",
+ "tools": [...]} items. api.openai.com accepts that item type; Mantle
+ rejects the whole request with 400 "Invalid 'input': value did not
+ match any expected variant" but accepts the same tools at the top
+ level, so move them there and strip the items from `input`.
+ """
+ if not isinstance(input, list):
+ return input, []
+ additional_tools_items = [item for item in input if cls._is_codex_additional_tools_item(item)]
+ if not additional_tools_items:
+ return input, []
+ remaining_input = [item for item in input if not cls._is_codex_additional_tools_item(item)]
+ hoisted_tools = [tool for item in additional_tools_items for tool in cls._tools_of_additional_tools_item(item)]
+ verbose_logger.debug(
+ "Bedrock Mantle Responses API: hoisting %d tool(s) out of %d 'additional_tools' input item(s) "
+ "into the top-level tools param (Mantle rejects that input item type).",
+ len(hoisted_tools),
+ len(additional_tools_items),
+ )
+ return remaining_input, cls._filter_unsupported_tools(hoisted_tools)
+
def map_openai_params(
self,
response_api_optional_params: ResponsesAPIOptionalRequestParams,
diff --cc tests/test_litellm/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py
index 7a3a84be65a,a40e2e33809..4dd1c663de0
--- a/tests/test_litellm/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py
+++ b/tests/test_litellm/llms/bedrock_mantle/test_bedrock_mantle_responses_transformation.py
@@@ -373,127 -373,144 +373,265 @@@ class TestBedrockMantleResponsesTools
assert "web_search" in str(mock_warning.call_args)
+def _codex_exec_tool():
+ return {
+ "type": "custom",
+ "name": "exec",
+ "description": "Run JavaScript code to orchestrate/compose tool calls",
+ "format": {
+ "type": "grammar",
+ "syntax": "lark",
+ "definition": "start: SOURCE\nSOURCE: /[\\s\\S]+/",
+ },
+ }
+
+
+def _codex_wait_tool():
+ return {
+ "type": "function",
+ "name": "wait",
+ "strict": False,
+ "parameters": {
+ "type": "object",
+ "properties": {"cell_id": {"type": "string"}},
+ "required": ["cell_id"],
+ "additionalProperties": False,
+ },
+ }
+
+
+class TestBedrockMantleServiceTier:
+ @pytest.mark.parametrize("tier", ["priority", "flex"])
+ def test_unsupported_service_tier_dropped_when_drop_params_true(self, tier):
+ cfg = BedrockMantleResponsesAPIConfig()
+ params = cfg.map_openai_params(
+ response_api_optional_params={"service_tier": tier},
+ model="openai.gpt-5.5",
+ drop_params=True,
+ )
+ assert "service_tier" not in params
+
+ @pytest.mark.parametrize("tier", ["priority", "flex"])
+ def test_unsupported_service_tier_raises_when_drop_params_false(self, tier):
+ cfg = BedrockMantleResponsesAPIConfig()
+ with pytest.raises(litellm.UnsupportedParamsError) as excinfo:
+ cfg.map_openai_params(
+ response_api_optional_params={"service_tier": tier},
+ model="openai.gpt-5.5",
+ drop_params=False,
+ )
+ assert tier in str(excinfo.value)
+ assert "drop_params" in str(excinfo.value)
+
+ @pytest.mark.parametrize("drop_params", [True, False])
+ @pytest.mark.parametrize("tier", ["auto", "default"])
+ def test_supported_service_tier_kept(self, tier, drop_params):
+ cfg = BedrockMantleResponsesAPIConfig()
+ params = cfg.map_openai_params(
+ response_api_optional_params={"service_tier": tier},
+ model="openai.gpt-5.5",
+ drop_params=drop_params,
+ )
+ assert params["service_tier"] == tier
+
+ def test_absent_service_tier_untouched(self):
+ cfg = BedrockMantleResponsesAPIConfig()
+ params = cfg.map_openai_params(
+ response_api_optional_params={"stream": True},
+ model="openai.gpt-5.5",
+ drop_params=False,
+ )
+ assert "service_tier" not in params
+ assert params["stream"] is True
+
+ def test_drop_logged_at_warning_level(self):
+ from unittest.mock import patch
+
+ cfg = BedrockMantleResponsesAPIConfig()
+ with patch(
+ "litellm.llms.bedrock_mantle.responses.transformation.verbose_logger.warning"
+ ) as mock_warning:
+ cfg.map_openai_params(
+ response_api_optional_params={"service_tier": "priority"},
+ model="openai.gpt-5.5",
+ drop_params=True,
+ )
+ assert mock_warning.call_count == 1
+ assert "priority" in str(mock_warning.call_args)
+
+
+class TestBedrockMantleCodexRequestEndToEnd:
+ def test_codex_priority_tier_request_becomes_mantle_acceptable(self):
+ cfg = BedrockMantleResponsesAPIConfig()
+ params = cfg.map_openai_params(
+ response_api_optional_params={
+ "service_tier": "priority",
+ "stream": True,
+ "store": False,
+ "tool_choice": "auto",
+ "parallel_tool_calls": False,
+ "tools": [_codex_exec_tool(), _codex_wait_tool()],
+ },
+ model="openai.gpt-5.5",
+ drop_params=True,
+ )
+ body = cfg.transform_responses_api_request(
+ model="openai.gpt-5.5",
+ input=[
+ {
+ "type": "message",
+ "role": "user",
+ "content": [{"type": "input_text", "text": "hi"}],
+ }
+ ],
+ response_api_optional_request_params=params,
+ litellm_params=GenericLiteLLMParams(),
+ headers={},
+ )
+ assert "service_tier" not in body
+ assert [tool["name"] for tool in body["tools"]] == ["exec", "wait"]
+ assert body["stream"] is True
+ assert body["tool_choice"] == "auto"
+
+
+ class TestBedrockMantleCodexAdditionalTools:
+ """Codex CLI's "responses lite" wire mode ships tool definitions inside
+ `input` as {"type": "additional_tools", "role": "developer", "tools": [...]}
+ items instead of the top-level `tools` param. api.openai.com accepts that
+ item; Mantle 400s the whole request with "Invalid 'input': value did not
+ match any expected variant" but accepts the same tools at the top level
+ (verified against bedrock-mantle.us-east-2.api.aws with openai.gpt-5.6-sol),
+ so the config must hoist them."""
+
+ _USER_MESSAGE = {
+ "type": "message",
+ "role": "user",
+ "content": [{"type": "input_text", "text": "Say hi in one word."}],
+ }
+ _DEVELOPER_MESSAGE = {
+ "type": "message",
+ "role": "developer",
+ "content": [{"type": "input_text", "text": "You are Codex."}],
+ }
+ _CODEX_TOOLS = [
+ {"type": "custom", "name": "exec", "format": {"type": "grammar", "syntax": "lark", "definition": "start: X"}},
+ {"type": "function", "name": "wait", "parameters": {"type": "object"}},
+ {"type": "namespace", "name": "collaboration", "tools": [{"type": "function", "name": "spawn_agent"}]},
+ ]
+
+ def _transform(self, input, params=None):
+ cfg = BedrockMantleResponsesAPIConfig()
+ return cfg.transform_responses_api_request(
+ model="openai.gpt-5.6-sol",
+ input=input,
+ response_api_optional_request_params=params if params is not None else {},
+ litellm_params=GenericLiteLLMParams(),
+ headers={},
+ )
+
+ def test_additional_tools_item_hoisted_to_top_level_tools(self):
+ body = self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer", "tools": self._CODEX_TOOLS},
+ self._DEVELOPER_MESSAGE,
+ self._USER_MESSAGE,
+ ]
+ )
+ assert body["input"] == [self._DEVELOPER_MESSAGE, self._USER_MESSAGE]
+ assert body["tools"] == self._CODEX_TOOLS
+
+ def test_hoisted_tools_append_after_existing_tools(self):
+ existing_tool = {"type": "function", "name": "preexisting"}
+ body = self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer", "tools": self._CODEX_TOOLS},
+ self._USER_MESSAGE,
+ ],
+ params={"tools": [existing_tool]},
+ )
+ assert body["tools"] == [existing_tool, *self._CODEX_TOOLS]
+
+ def test_unsupported_hoisted_tool_types_are_dropped(self):
+ body = self._transform(
+ input=[
+ {
+ "type": "additional_tools",
+ "role": "developer",
+ "tools": [
+ {"type": "web_search"},
+ {"type": "function", "name": "wait"},
+ ],
+ },
+ self._USER_MESSAGE,
+ ]
+ )
+ assert body["tools"] == [{"type": "function", "name": "wait"}]
+
+ def test_item_stripped_even_when_no_hoisted_tool_survives(self):
+ body = self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer", "tools": [{"type": "web_search"}]},
+ self._USER_MESSAGE,
+ ]
+ )
+ assert body["input"] == [self._USER_MESSAGE]
+ assert "tools" not in body
+
+ def test_multiple_additional_tools_items_merge_in_order(self):
+ first = {"type": "function", "name": "first"}
+ second = {"type": "function", "name": "second"}
+ body = self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer", "tools": [first]},
+ self._USER_MESSAGE,
+ {"type": "additional_tools", "role": "developer", "tools": [second]},
+ ]
+ )
+ assert body["input"] == [self._USER_MESSAGE]
+ assert body["tools"] == [first, second]
+
+ def test_string_input_passes_through(self):
+ body = self._transform(input="hello")
+ assert body["input"] == "hello"
+ assert "tools" not in body
+
+ def test_input_without_additional_tools_is_unchanged(self):
+ codex_agentic_items = [
+ self._USER_MESSAGE,
+ {"type": "reasoning", "summary": [], "encrypted_content": "gAAAA=="},
+ {"type": "function_call", "name": "wait", "arguments": "{}", "call_id": "call_1"},
+ {"type": "function_call_output", "call_id": "call_1", "output": "done"},
+ ]
+ body = self._transform(input=list(codex_agentic_items))
+ assert body["input"] == codex_agentic_items
+ assert "tools" not in body
+
+ def test_malformed_additional_tools_item_without_tools_list_is_stripped(self):
+ body = self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer"},
+ self._USER_MESSAGE,
+ ]
+ )
+ assert body["input"] == [self._USER_MESSAGE]
+ assert "tools" not in body
+
+ def test_hoist_is_logged_at_debug_level(self):
+ from unittest.mock import patch
+
+ with patch(
+ "litellm.llms.bedrock_mantle.responses.transformation.verbose_logger.debug"
+ ) as mock_debug:
+ self._transform(
+ input=[
+ {"type": "additional_tools", "role": "developer", "tools": self._CODEX_TOOLS},
+ self._USER_MESSAGE,
+ ]
+ )
+ assert mock_debug.call_count == 1
+ assert "additional_tools" in str(mock_debug.call_args)
+
+
class TestBedrockMantleResponsesRegistry:
def test_registry_returns_config_for_gpt_5_5(self, local_cost_map):
# gpt-5.x advertises /v1/responses in supported_endpoints (capability)