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): keep rerank on SigV4 when a Bedrock API key is set
mateo-berri729a95232204
diff --git a/litellm/llms/bedrock/base_aws_llm.py b/litellm/llms/bedrock/base_aws_llm.pyindex db6f2c0d491..4332848e545 100644--- a/litellm/llms/bedrock/base_aws_llm.py+++ b/litellm/llms/bedrock/base_aws_llm.py@@ -1434,9 +1434,12 @@ class BaseAWSLLM:data: str | bytes,headers: dict,api_key: str | None = None,+ supports_bearer_token: bool = True,) -> AWSPreparedRequest:- if api_key is not None:- aws_bearer_token: str | None = api_key+ if not supports_bearer_token:+ aws_bearer_token: str | None = None+ elif api_key is not None:+ aws_bearer_token = api_keyelse:aws_bearer_token = get_secret_str("AWS_BEARER_TOKEN_BEDROCK")diff --git a/litellm/llms/bedrock/rerank/handler.py b/litellm/llms/bedrock/rerank/handler.pyindex 79b70c47a9a..cb0473887ea 100644--- a/litellm/llms/bedrock/rerank/handler.py+++ b/litellm/llms/bedrock/rerank/handler.py@@ -158,6 +158,7 @@ class BedrockRerankHandler(BaseAWSLLM):endpoint_url=proxy_endpoint_url,data=body,headers=headers,+ supports_bearer_token=False,)return BedrockPreparedRequest(diff --git a/tests/test_litellm/llms/bedrock/rerank/test_bedrock_rerank_header_forwarding.py b/tests/test_litellm/llms/bedrock/rerank/test_bedrock_rerank_header_forwarding.pyindex ebe0df2a1c7..dd14b38f07a 100644--- a/tests/test_litellm/llms/bedrock/rerank/test_bedrock_rerank_header_forwarding.py+++ b/tests/test_litellm/llms/bedrock/rerank/test_bedrock_rerank_header_forwarding.py@@ -439,3 +439,33 @@ def test_bedrock_rerank_forwarded_headers_excluded_from_sigv4_signature():f"x-forwarded-for must not be part of the SigV4 signature, got SignedHeaders={signed_headers}")assert headers["x-forwarded-for"] == "203.0.113.5", "forwarded header must still reach Bedrock, unsigned"+++def test_bedrock_rerank_signs_with_sigv4_even_when_bedrock_api_key_is_set(monkeypatch):+ """+ Bedrock API keys are only valid for Bedrock and Bedrock Runtime actions, not for+ Agents for Amazon Bedrock Runtime ones. Rerank is served by bedrock-agent-runtime,+ so it has to keep signing with SigV4 even when AWS_BEARER_TOKEN_BEDROCK is set.+ """+ monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "test-bedrock-api-key")++ handler = BedrockRerankHandler()++ prepared_request = handler._prepare_request(+ model="cohere.rerank-v3-5:0",+ api_base=None,+ extra_headers=None,+ data={"query": test_query, "documents": test_documents},+ optional_params={+ "aws_access_key_id": "test-access-key",+ "aws_secret_access_key": "test-secret-key",+ "aws_region_name": "us-east-1",+ },+ )++ assert prepared_request["endpoint_url"].startswith("https://bedrock-agent-runtime.")++ authorization = prepared_request["prepped"].headers["Authorization"]+ assert authorization.startswith("AWS4-HMAC-SHA256"), (+ f"rerank must sign with SigV4, got Authorization={authorization[:30]}"+ )