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
test(bedrock): cover async header forwarding for converse and invoke
Devin AI726db1a4c118
diff --git a/tests/llm_translation/test_bedrock_moonshot.py b/tests/llm_translation/test_bedrock_moonshot.pyindex c777305d562..61364cf2caa 100644--- a/tests/llm_translation/test_bedrock_moonshot.py+++ b/tests/llm_translation/test_bedrock_moonshot.py@@ -12,6 +12,7 @@ This test suite verifies:"""from base_llm_unit_tests import BaseLLMChatTest+import httpximport pytestimport sysimport os@@ -213,8 +214,6 @@ class TestBedrockMoonshotInvoke(BaseLLMChatTest):``base_invoke_transformation`` so we observe the exact kwargs it iscalled with at stream-wrapper construction time."""- import httpx-from litellm.utils import CustomStreamWrappercaptured: dict = {}diff --git a/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py b/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.pyindex f211cdac475..e8964910c69 100644--- a/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py+++ b/tests/test_litellm/llms/bedrock/chat/test_invoke_handler.py@@ -15,7 +15,7 @@ from litellm.llms.bedrock.chat.invoke_handler import (make_call,make_sync_call,)-from litellm.llms.custom_httpx.http_handler import HTTPHandler+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandlerdef test_transform_thinking_blocks_with_redacted_content():@@ -298,7 +298,6 @@ def test_make_sync_call_honors_explicit_stream_chunk_size():def test_invoke_streaming_forwards_bedrock_response_headers():- """Streaming callers need `x-amzn-requestid` to correlate a LiteLLM request with AWS support."""response = MagicMock()response.status_code = 200response.iter_bytes = MagicMock(return_value=iter([]))@@ -318,3 +317,29 @@ def test_invoke_streaming_forwards_bedrock_response_headers():assert stream._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-789"++async def test_async_invoke_streaming_forwards_bedrock_response_headers():+ async def _no_bytes(chunk_size=None):+ return+ yield b""++ response = MagicMock()+ response.status_code = 200+ response.aiter_bytes = _no_bytes+ response.headers = httpx.Headers({"x-amzn-requestid": "req-987"})+ client = AsyncHTTPHandler()+ client.post = AsyncMock(return_value=response)++ stream = await litellm.acompletion(+ model="bedrock/invoke/anthropic.claude-haiku-4-5-20251001-v1:0",+ messages=[{"role": "user", "content": "hi"}],+ stream=True,+ client=client,+ aws_access_key_id="fake",+ aws_secret_access_key="fake",+ aws_region_name="us-east-1",+ )++ assert stream._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-987"+diff --git a/tests/test_litellm/llms/chat/test_converse_handler.py b/tests/test_litellm/llms/chat/test_converse_handler.pyindex 89ab29122d7..6f8a2788c38 100644--- a/tests/test_litellm/llms/chat/test_converse_handler.py+++ b/tests/test_litellm/llms/chat/test_converse_handler.py@@ -1,7 +1,7 @@import jsonimport osimport sys-from unittest.mock import MagicMock+from unittest.mock import AsyncMock, MagicMockimport httpximport pytest@@ -10,7 +10,7 @@ import litellmfrom litellm.llms.bedrock.chat import BedrockConverseLLMfrom litellm.llms.bedrock.chat.converse_handler import make_sync_callfrom litellm.llms.bedrock.common_utils import _get_all_bedrock_regions-from litellm.llms.custom_httpx.http_handler import HTTPHandler+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandlersys.path.insert(0, os.path.abspath("../../../../..")@@ -213,9 +213,6 @@ def _converse_response_body() -> dict:def test_converse_completion_forwards_bedrock_response_headers():- """Bedrock returns x-amzn-requestid on every converse call, which customers need to- correlate proxy requests with AWS support cases, so it must reach the caller as- llm_provider-x-amzn-requestid."""mock_response = MagicMock()mock_response.status_code = 200mock_response.json = MagicMock(return_value=_converse_response_body())@@ -257,6 +254,54 @@ def test_converse_streaming_forwards_bedrock_response_headers():assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-456"+async def test_async_converse_completion_forwards_bedrock_response_headers():+ mock_response = MagicMock()+ mock_response.status_code = 200+ mock_response.json = MagicMock(return_value=_converse_response_body())+ mock_response.text = json.dumps(_converse_response_body())+ mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-abc"})+ client = AsyncHTTPHandler()+ client.post = AsyncMock(return_value=mock_response)++ response = await litellm.acompletion(+ model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",+ messages=[{"role": "user", "content": "hi"}],+ client=client,+ aws_access_key_id="fake",+ aws_secret_access_key="fake",+ aws_region_name="us-east-1",+ )++ assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-abc"+++async def test_async_converse_streaming_forwards_bedrock_response_headers():+ async def _no_bytes(chunk_size=None):+ return+ yield b""++ mock_response = MagicMock()+ mock_response.status_code = 200+ mock_response.aiter_bytes = _no_bytes+ mock_response.headers = httpx.Headers({"x-amzn-requestid": "req-def"})+ client = AsyncHTTPHandler()+ client.post = AsyncMock(return_value=mock_response)++ response = await litellm.acompletion(+ model="bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0",+ messages=[{"role": "user", "content": "hi"}],+ stream=True,+ client=client,+ aws_access_key_id="fake",+ aws_secret_access_key="fake",+ aws_region_name="us-east-1",+ )++ assert response._hidden_params["additional_headers"]["llm_provider-x-amzn-requestid"] == "req-def"++def test_completion_plumbs_stream_chunk_size_through_converse():iter_bytes_spy = _stream_completion_with_spied_iter_bytes(model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0"