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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(bedrock_mantle): register a Bedrock runtime passthrough config so /bedrock/model/<deployment>/invoke works
mateo-berri68ad575fc210
diff --git a/litellm/llms/bedrock/passthrough/transformation.py b/litellm/llms/bedrock/passthrough/transformation.pyindex 0ce2e6f60d3..d0a3c37ffb3 100644--- a/litellm/llms/bedrock/passthrough/transformation.py+++ b/litellm/llms/bedrock/passthrough/transformation.py@@ -1,4 +1,5 @@import json+from collections.abc import Mappingfrom typing import TYPE_CHECKING, Final, Optional, castfrom httpx import Response@@ -93,6 +94,9 @@ class BedrockPassthroughConfig(BaseAWSLLM, BedrockModelInfo, BedrockEventStreamDendpoint_url,)+ def get_bedrock_bearer_token(self, litellm_params: Mapping[str, object]) -> str | None:+ return None+def sign_request(self,headers: dict,@@ -109,6 +113,7 @@ class BedrockPassthroughConfig(BaseAWSLLM, BedrockModelInfo, BedrockEventStreamDrequest_data=request_data or {},api_base=api_base,model=model,+ api_key=self.get_bedrock_bearer_token(optional_params),)def logging_non_streaming_response(diff --git a/litellm/llms/bedrock_mantle/common_utils.py b/litellm/llms/bedrock_mantle/common_utils.pyindex 889361cd808..d877fbb4e09 100644--- a/litellm/llms/bedrock_mantle/common_utils.py+++ b/litellm/llms/bedrock_mantle/common_utils.py@@ -13,6 +13,7 @@ global state."""import re+from collections.abc import Mappingfrom typing import Finalfrom botocore.exceptions import (@@ -31,30 +32,39 @@ BEDROCK_MANTLE_DEFAULT_REGION: Final = "us-east-1"MANTLE_HOST_RE: Final = re.compile(r"^https?://bedrock-mantle\.([^/.]+)\.api\.aws", re.IGNORECASE)+def resolve_mantle_bearer_token(api_key: str | None) -> str | None:+ return api_key or get_secret_str("BEDROCK_MANTLE_API_KEY") or get_secret_str("AWS_BEARER_TOKEN_BEDROCK")+++def resolve_mantle_region(params: Mapping[str, object]) -> str:+ region: Final = params.get("aws_region_name")+ if isinstance(region, str) and region:+ BaseAWSLLM._validate_aws_region_name(region)+ return region+ api_base: Final = params.get("api_base")+ base: Final = (api_base if isinstance(api_base, str) else None) or get_secret_str("BEDROCK_MANTLE_API_BASE")+ if base:+ match: Final = MANTLE_HOST_RE.match(base.rstrip("/"))+ if match:+ return match.group(1)+ return (+ get_secret_str("BEDROCK_MANTLE_REGION")+ or get_secret_str("AWS_REGION_NAME")+ or get_secret_str("AWS_REGION")+ or BEDROCK_MANTLE_DEFAULT_REGION+ )++class BedrockMantleAuthMixin:_aws_signer: BaseAWSLLM@staticmethoddef _resolve_bearer_token(api_key: str | None) -> str | None:- return api_key or get_secret_str("BEDROCK_MANTLE_API_KEY") or get_secret_str("AWS_BEARER_TOKEN_BEDROCK")+ return resolve_mantle_bearer_token(api_key)@staticmethoddef _resolve_region(params: dict) -> str:- region: Final = params.get("aws_region_name")- if region:- BaseAWSLLM._validate_aws_region_name(region)- return region- base: Final = params.get("api_base") or get_secret_str("BEDROCK_MANTLE_API_BASE")- if base:- match: Final = MANTLE_HOST_RE.match(base.rstrip("/"))- if match:- return match.group(1)- return (- get_secret_str("BEDROCK_MANTLE_REGION")- or get_secret_str("AWS_REGION_NAME")- or get_secret_str("AWS_REGION")- or BEDROCK_MANTLE_DEFAULT_REGION- )+ return resolve_mantle_region(params)def sign_request(self,diff --git a/litellm/llms/bedrock_mantle/passthrough/transformation.py b/litellm/llms/bedrock_mantle/passthrough/transformation.pynew file mode 100644index 00000000000..1393ac7c6e7--- /dev/null+++ b/litellm/llms/bedrock_mantle/passthrough/transformation.py@@ -0,0 +1,44 @@+from collections.abc import Mapping+from typing import Final, Literal++from litellm.llms.bedrock.passthrough.transformation import BedrockPassthroughConfig+from litellm.llms.bedrock_mantle.common_utils import (+ MANTLE_HOST_RE,+ resolve_mantle_bearer_token,+ resolve_mantle_region,+)+++class BedrockMantlePassthroughConfig(BedrockPassthroughConfig):+ """Native Bedrock runtime passthrough (InvokeModel, Converse) for deployments declared as bedrock_mantle.++ The Mantle host only serves the OpenAI-compatible surface, so a Mantle api_base lends its region and the+ request itself goes to bedrock-runtime, signed with the deployment's Bearer token or SigV4 credentials.+ """++ def _get_aws_region_name(+ self,+ optional_params: Mapping[str, object],+ model: str | None = None,+ model_id: str | None = None,+ ) -> str:+ return resolve_mantle_region(optional_params)++ def get_runtime_endpoint(+ self,+ api_base: str | None,+ aws_bedrock_runtime_endpoint: str | None,+ aws_region_name: str,+ endpoint_type: Literal["runtime", "agent", "agentcore"] | None = "runtime",+ ) -> tuple[str, str]:+ is_mantle_host: Final = api_base is not None and MANTLE_HOST_RE.match(api_base.rstrip("/")) is not None+ return super().get_runtime_endpoint(+ api_base=None if is_mantle_host else api_base,+ aws_bedrock_runtime_endpoint=aws_bedrock_runtime_endpoint,+ aws_region_name=aws_region_name,+ endpoint_type=endpoint_type,+ )++ def get_bedrock_bearer_token(self, litellm_params: Mapping[str, object]) -> str | None:+ api_key: Final = litellm_params.get("api_key")+ return resolve_mantle_bearer_token(api_key if isinstance(api_key, str) else None)diff --git a/litellm/passthrough/main.py b/litellm/passthrough/main.pyindex 8a2ee2a3af8..4b30afb2f98 100644--- a/litellm/passthrough/main.py+++ b/litellm/passthrough/main.py@@ -199,7 +199,7 @@ def llm_passthrough_route(api_key=api_key,)- litellm_params_dict: Final = get_litellm_params(**kwargs)+ litellm_params_dict: Final = get_litellm_params(api_key=api_key, api_base=api_base, **kwargs)if client is None:from litellm.llms.custom_httpx.http_handler import (diff --git a/litellm/utils.py b/litellm/utils.pyindex 012e8785321..5cbd0519032 100644--- a/litellm/utils.py+++ b/litellm/utils.py@@ -8610,6 +8610,12 @@ class ProviderConfigManager:)return BedrockPassthroughConfig()+ elif LlmProviders.BEDROCK_MANTLE == provider:+ from litellm.llms.bedrock_mantle.passthrough.transformation import (+ BedrockMantlePassthroughConfig,+ )++ return BedrockMantlePassthroughConfig()elif LlmProviders.VLLM == provider or LlmProviders.HOSTED_VLLM == provider:from litellm.llms.vllm.passthrough.transformation import (VLLMPassthroughConfig,diff --git a/tests/test_litellm/llms/bedrock_mantle/passthrough/test_bedrock_mantle_passthrough_transformation.py b/tests/test_litellm/llms/bedrock_mantle/passthrough/test_bedrock_mantle_passthrough_transformation.pynew file mode 100644index 00000000000..b7f9e492e14--- /dev/null+++ b/tests/test_litellm/llms/bedrock_mantle/passthrough/test_bedrock_mantle_passthrough_transformation.py@@ -0,0 +1,152 @@+import json+from unittest.mock import MagicMock, patch++import httpx+import pytest+from botocore.credentials import Credentials++from litellm.llms.bedrock.passthrough.transformation import BedrockPassthroughConfig+from litellm.llms.bedrock_mantle.passthrough.transformation import BedrockMantlePassthroughConfig+from litellm.llms.custom_httpx.http_handler import HTTPHandler+from litellm.passthrough.main import llm_passthrough_route+from litellm.types.utils import LlmProviders+from litellm.utils import ProviderConfigManager++MANTLE_API_BASE = "https://bedrock-mantle.us-east-2.api.aws"+INVOKE_ENDPOINT = "model/us.openai.gpt-5.6-sol/invoke"+REQUEST_BODY = {"messages": [{"role": "user", "content": "say pong"}], "max_completion_tokens": 64}+++def no_ambient_aws(monkeypatch):+ for name in (+ "AWS_BEARER_TOKEN_BEDROCK",+ "BEDROCK_MANTLE_API_KEY",+ "BEDROCK_MANTLE_API_BASE",+ "BEDROCK_MANTLE_REGION",+ "AWS_BEDROCK_RUNTIME_ENDPOINT",+ "AWS_REGION_NAME",+ "AWS_REGION",+ "AWS_DEFAULT_REGION",+ ):+ monkeypatch.delenv(name, raising=False)+++def test_bedrock_mantle_registers_its_own_bedrock_passthrough_config():+ config = ProviderConfigManager.get_provider_passthrough_config(+ model="us.openai.gpt-5.6-sol", provider=LlmProviders.BEDROCK_MANTLE+ )+ assert isinstance(config, BedrockMantlePassthroughConfig)+ assert isinstance(config, BedrockPassthroughConfig)+++def test_mantle_api_base_only_lends_its_region_to_the_runtime_url(no_ambient_aws):+ url, base_url = BedrockMantlePassthroughConfig().get_complete_url(+ api_base=MANTLE_API_BASE,+ api_key=None,+ model="us.openai.gpt-5.6-sol",+ endpoint=INVOKE_ENDPOINT,+ request_query_params=None,+ litellm_params={"api_base": MANTLE_API_BASE},+ )+ assert str(url) == f"https://bedrock-runtime.us-east-2.amazonaws.com/{INVOKE_ENDPOINT}"+ assert base_url == "https://bedrock-runtime.us-east-2.amazonaws.com"+++def test_explicit_region_and_non_mantle_api_base_are_kept(no_ambient_aws):+ vpc_endpoint = "https://vpce-0123.bedrock-runtime.us-east-1.vpce.amazonaws.com"+ url, base_url = BedrockMantlePassthroughConfig().get_complete_url(+ api_base=vpc_endpoint,+ api_key=None,+ model="us.openai.gpt-5.6-sol",+ endpoint=INVOKE_ENDPOINT,+ request_query_params=None,+ litellm_params={"api_base": vpc_endpoint, "aws_region_name": "us-east-1"},+ )+ assert str(url) == f"{vpc_endpoint}/{INVOKE_ENDPOINT}"+ assert base_url == vpc_endpoint+++def test_region_falls_back_to_the_mantle_default_without_any_hint(no_ambient_aws):+ url, _ = BedrockMantlePassthroughConfig().get_complete_url(+ api_base=None,+ api_key=None,+ model="us.openai.gpt-5.6-sol",+ endpoint=INVOKE_ENDPOINT,+ request_query_params=None,+ litellm_params={},+ )+ assert str(url) == f"https://bedrock-runtime.us-east-1.amazonaws.com/{INVOKE_ENDPOINT}"+++ ("litellm_params", "env", "expected_bearer"),+ [+ ({"api_key": "deployment-bedrock-api-key"}, {}, "deployment-bedrock-api-key"),+ ({}, {"BEDROCK_MANTLE_API_KEY": "mantle-env-key"}, "mantle-env-key"),+ ({}, {"AWS_BEARER_TOKEN_BEDROCK": "aws-env-key"}, "aws-env-key"),+ ],+)+def test_sign_request_uses_the_deployment_bearer_token(no_ambient_aws, monkeypatch, litellm_params, env, expected_bearer):+ for name, value in env.items():+ monkeypatch.setenv(name, value)+ headers, body = BedrockMantlePassthroughConfig().sign_request(+ headers={},+ litellm_params=litellm_params,+ request_data=REQUEST_BODY,+ api_base=f"https://bedrock-runtime.us-east-1.amazonaws.com/{INVOKE_ENDPOINT}",+ model="us.openai.gpt-5.6-sol",+ )+ assert headers["Authorization"] == f"Bearer {expected_bearer}"+ assert body is not None+ assert json.loads(body) == REQUEST_BODY+++def test_sign_request_falls_back_to_sigv4_scoped_to_the_mantle_region(no_ambient_aws):+ config = BedrockMantlePassthroughConfig()+ with patch.object(config, "get_credentials", return_value=Credentials("AKIA", "secret")):+ headers, body = config.sign_request(+ headers={},+ litellm_params={"api_base": MANTLE_API_BASE},+ request_data=REQUEST_BODY,+ api_base=f"https://bedrock-runtime.us-east-2.amazonaws.com/{INVOKE_ENDPOINT}",+ model="us.openai.gpt-5.6-sol",+ )+ assert headers["Authorization"].startswith("AWS4-HMAC-SHA256 Credential=AKIA/")+ assert "/us-east-2/bedrock/aws4_request" in headers["Authorization"]+ assert body is not None+ assert json.loads(body) == REQUEST_BODY+++ ("route_kwargs", "env", "expected_bearer"),+ [+ ({"api_key": "deployment-bedrock-api-key"}, {}, "deployment-bedrock-api-key"),+ ({}, {"BEDROCK_MANTLE_API_KEY": "mantle-env-key"}, "mantle-env-key"),+ ],+)+def test_invoke_passthrough_route_reaches_bedrock_runtime_for_a_mantle_deployment(+ no_ambient_aws, monkeypatch, route_kwargs, env, expected_bearer+):+ for name, value in env.items():+ monkeypatch.setenv(name, value)+ client = HTTPHandler()+ with (+ patch.object(client.client, "send", return_value=MagicMock(status_code=200)),+ patch.object(client.client, "build_request", wraps=client.client.build_request) as build_request,+ ):+ response = llm_passthrough_route(+ model="bedrock_mantle/us.openai.gpt-5.6-sol",+ endpoint=INVOKE_ENDPOINT,+ method="POST",+ api_base=MANTLE_API_BASE,+ json=dict(REQUEST_BODY),+ client=client,+ litellm_logging_obj=MagicMock(),+ **route_kwargs,+ )+ assert response.status_code == 200+ sent = build_request.call_args.kwargs+ assert str(sent["url"]) == f"https://bedrock-runtime.us-east-2.amazonaws.com/{INVOKE_ENDPOINT}"+ assert sent["headers"]["Authorization"] == f"Bearer {expected_bearer}"+ assert json.loads(sent["content"]) == REQUEST_BODY