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
Merge branch 'litellm_internal_staging' of https://github.com/BerriAI/litellm into litellm_lit5458_rerank_sigv4_bearer_fix
mateo-berrie47e989341b4
diff --cc tests/test_litellm/llms/bedrock/rerank/test_bedrock_rerank_header_forwarding.pyindex f9760cd1dcc,253edad57e9..2ea61b5e978--- 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@@@ -403,61 -402,30 +403,90 @@@ def test_bedrock_rerank_extra_headers_apytest.fail(f"Failed to merge and forward headers: {str(e)}")+def test_bedrock_rerank_forwarded_headers_excluded_from_sigv4_signature():+ """+ A forwarded header like x-forwarded-for can be rewritten between LiteLLM+ signing the request and AWS receiving it (e.g. by an intermediate load+ balancer), which invalidates the signature if that header was part of+ the signed set. It must still reach Bedrock, just unsigned.+ """+ handler = BedrockRerankHandler()++ prepared_request = handler._prepare_request(+ model="cohere.rerank-v3-5:0",+ api_base=None,+ extra_headers={"x-forwarded-for": "203.0.113.5"},+ 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",+ },+ )++ headers = prepared_request["prepped"].headers+ signed_headers = headers["Authorization"].split("SignedHeaders=")[1].split(",")[0].split(";")++ assert "x-forwarded-for" not in signed_headers, (+ 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]}"+ )+++++ @pytest.mark.asyncio+ async def test_bedrock_rerank_records_llm_api_duration():+ """The bedrock rerank handler must feed httpx timing into the logging obj, so the+ proxy can emit x-litellm-overhead-duration-ms / x-litellm-timing-* on /rerank."""+ import httpx++ def handle(request: httpx.Request) -> httpx.Response:+ return httpx.Response(200, json=bedrock_rerank_response)++ client = AsyncHTTPHandler()+ client.client = httpx.AsyncClient(transport=httpx.MockTransport(handle))++ with patch( # test-quality-ok: boto credential lookup needs live AWS; the HTTP boundary is already a MockTransport+ "litellm.llms.bedrock.rerank.handler.BedrockRerankHandler._get_boto_credentials_from_optional_params",+ return_value=create_mock_credentials(),+ ):+ response = await litellm.arerank(+ model="bedrock/arn:aws:bedrock:us-east-1::foundation-model/cohere.rerank-v3-5:0",+ query=test_query,+ documents=test_documents,+ top_n=3,+ client=client,+ aws_region_name="us-east-1",+ )++ assert response._hidden_params["litellm_overhead_time_ms"] is not None+ assert response._hidden_params["_response_ms"] >= response._hidden_params["litellm_overhead_time_ms"]