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]

test: stop bedrock tool acompletion tests from making real network calls (#34644)

ryan-crabbe-berri998a37241765
diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
index f832a4087ec..cca4f4232f2 100644
--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
@@ -1093,53 +1093,57 @@ def test_transform_response_with_structured_response_calling_tool():
)
+def _mock_converse_response() -> MagicMock:
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.json.return_value = {
+ "output": {"message": {"role": "assistant", "content": [{"text": "ok"}]}},
+ "stopReason": "end_turn",
+ "usage": {"inputTokens": 10, "outputTokens": 5, "totalTokens": 15},
+ }
+ mock_response.text = json.dumps(mock_response.json.return_value)
+ return mock_response
+
+
+async def _acompletion_captured_request_body(tools: list, messages: list) -> dict:
+ from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
+
+ client = AsyncHTTPHandler()
+ with patch.object(client, "post", return_value=_mock_converse_response()) as mock_post:
+ response = await litellm.acompletion(
+ model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
+ messages=messages,
+ tools=tools,
+ aws_access_key_id="fake-access-key",
+ aws_secret_access_key="fake-secret-key",
+ aws_region_name="us-west-2",
+ client=client,
+ )
+
+ assert response.choices[0].message.content == "ok"
+ mock_post.assert_called_once()
+ assert mock_post.call_args.kwargs["url"].endswith("/converse")
+ return json.loads(mock_post.call_args.kwargs["data"])
+
+
@pytest.mark.asyncio
async def test_bedrock_bash_tool_acompletion():
- """Test Bedrock with bash tool for ls command using acompletion."""
-
- # Test with bash tool instead of computer tool
+ """Bash tool rides acompletion into the converse request body without any network call."""
tools = [
{
"type": "bash_20241022",
"name": "bash",
}
]
-
messages = [{"role": "user", "content": "run ls command and find all python files"}]
- try:
- response = await litellm.acompletion(
- model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
- messages=messages,
- tools=tools,
- # Using dummy API key - test should fail with auth error, proving request formatting works
- api_key="dummy-key-for-testing",
- )
- # If we get here, something's wrong - we expect an auth error
- assert False, "Expected authentication error but got successful response"
- except Exception as e:
- error_str = str(e).lower()
-
- # Check if it's an expected authentication/credentials error
- auth_error_indicators = [
- "credentials",
- "authentication",
- "unauthorized",
- "access denied",
- "aws",
- "region",
- "profile",
- "token",
- "invalid",
- "signature",
- ]
+ request_body = await _acompletion_captured_request_body(tools=tools, messages=messages)
- if any(auth_error in error_str for auth_error in auth_error_indicators):
- # This is expected - request formatting succeeded, auth failed as expected
- assert True
- else:
- # Unexpected error - might be tool handling issue
- pytest.fail(f"Unexpected error (might be tool handling issue): {e}")
+ additional_fields = request_body["additionalModelRequestFields"]
+ assert additional_fields["tools"] == [{"type": "bash_20241022", "name": "bash"}]
+ assert "anthropic_beta" in additional_fields
+ assert request_body["messages"][0]["content"][0]["text"] == "run ls command and find all python files"
@pytest.mark.asyncio
@@ -1172,39 +1176,16 @@ async def test_bedrock_computer_use_acompletion():
}
]
- try:
- response = await litellm.acompletion(
- model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
- messages=messages,
- tools=tools,
- # Using dummy API key - test should fail with auth error, proving request formatting works
- api_key="dummy-key-for-testing",
- )
- # If we get here, something's wrong - we expect an auth error
- assert False, "Expected authentication error but got successful response"
- except Exception as e:
- error_str = str(e).lower()
-
- # Check if it's an expected authentication/credentials error
- auth_error_indicators = [
- "credentials",
- "authentication",
- "unauthorized",
- "access denied",
- "aws",
- "region",
- "profile",
- "token",
- "invalid",
- "signature",
- ]
+ request_body = await _acompletion_captured_request_body(tools=tools, messages=messages)
- if any(auth_error in error_str for auth_error in auth_error_indicators):
- # This is expected - request formatting succeeded, auth failed as expected
- assert True
- else:
- # Unexpected error - might be tool handling issue
- pytest.fail(f"Unexpected error (might be tool handling issue): {e}")
+ additional_fields = request_body["additionalModelRequestFields"]
+ assert additional_fields["anthropic_beta"] == ["computer-use-2025-01-24"]
+ computer_tools = [tool for tool in additional_fields["tools"] if tool.get("type") == "computer_20250124"]
+ assert computer_tools[0]["display_height_px"] == 768
+ assert computer_tools[0]["display_width_px"] == 1024
+ image_blocks = [block for block in request_body["messages"][0]["content"] if "image" in block]
+ assert image_blocks[0]["image"]["format"] == "png"
+ assert image_blocks[0]["image"]["source"]["bytes"]
@pytest.mark.asyncio