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]

fix(bedrock_httpx.py): support 'Auth' header as extra_header

Krrish Dholakia6431af067860
diff --git a/litellm/llms/bedrock_httpx.py b/litellm/llms/bedrock_httpx.py
index 23e7fdc3ec4..db9fc8b47b7 100644
--- a/litellm/llms/bedrock_httpx.py
+++ b/litellm/llms/bedrock_httpx.py
@@ -894,6 +894,10 @@ class BedrockLLM(BaseAWSLLM):
method="POST", url=endpoint_url, data=data, headers=headers
)
sigv4.add_auth(request)
+ if (
+ extra_headers is not None and "Authorization" in extra_headers
+ ): # prevent sigv4 from overwriting the auth header
+ request.headers["Authorization"] = extra_headers["Authorization"]
prepped = request.prepare()
## LOGGING
@@ -1659,6 +1663,10 @@ class BedrockConverseLLM(BaseAWSLLM):
method="POST", url=endpoint_url, data=data, headers=headers
)
sigv4.add_auth(request)
+ if (
+ extra_headers is not None and "Authorization" in extra_headers
+ ): # prevent sigv4 from overwriting the auth header
+ request.headers["Authorization"] = extra_headers["Authorization"]
prepped = request.prepare()
## LOGGING
diff --git a/litellm/llms/sagemaker/sagemaker.py b/litellm/llms/sagemaker/sagemaker.py
index 5e777668999..cbf1a9f62b0 100644
--- a/litellm/llms/sagemaker/sagemaker.py
+++ b/litellm/llms/sagemaker/sagemaker.py
@@ -196,6 +196,11 @@ class SagemakerLLM(BaseAWSLLM):
method="POST", url=api_base, data=encoded_data, headers=headers
)
sigv4.add_auth(request)
+ if (
+ extra_headers is not None and "Authorization" in extra_headers
+ ): # prevent sigv4 from overwriting the auth header
+ request.headers["Authorization"] = extra_headers["Authorization"]
+
prepped_request = request.prepare()
return prepped_request
diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py
index d11f58a3eab..01433b55595 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py
@@ -182,6 +182,11 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
method="POST", url=api_base, data=encoded_data, headers=headers
)
sigv4.add_auth(request)
+ if (
+ extra_headers is not None and "Authorization" in extra_headers
+ ): # prevent sigv4 from overwriting the auth header
+ request.headers["Authorization"] = extra_headers["Authorization"]
+
prepped_request = request.prepare()
return prepped_request
diff --git a/litellm/tests/test_bedrock_completion.py b/litellm/tests/test_bedrock_completion.py
index 90592b499c4..129e0fc6259 100644
--- a/litellm/tests/test_bedrock_completion.py
+++ b/litellm/tests/test_bedrock_completion.py
@@ -945,7 +945,8 @@ async def test_bedrock_extra_headers():
"""
Check if a url with 'modelId' passed in, is created correctly
- Reference: https://github.com/BerriAI/litellm/issues/3805
+ Reference: https://github.com/BerriAI/litellm/issues/3805, https://github.com/BerriAI/litellm/issues/5389#issuecomment-2313677977
+
"""
client = AsyncHTTPHandler()
@@ -958,7 +959,7 @@ async def test_bedrock_extra_headers():
model="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": "What's AWS?"}],
client=client,
- extra_headers={"test": "hello world"},
+ extra_headers={"test": "hello world", "Authorization": "my-test-key"},
)
except Exception as e:
pass
@@ -966,6 +967,10 @@ async def test_bedrock_extra_headers():
print(f"mock_client_post.call_args: {mock_client_post.call_args}")
assert "test" in mock_client_post.call_args.kwargs["headers"]
assert mock_client_post.call_args.kwargs["headers"]["test"] == "hello world"
+ assert (
+ mock_client_post.call_args.kwargs["headers"]["Authorization"]
+ == "my-test-key"
+ )
mock_client_post.assert_called_once()