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
Revert "[LLM translation] Add support for bedrock computer use (#12948)" (#13118)
Krish Dholakia1c182919b516
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.pyindex f45ecbd7f14..ec378ddbb85 100644--- a/litellm/llms/bedrock/chat/converse_transformation.py+++ b/litellm/llms/bedrock/chat/converse_transformation.py@@ -49,9 +49,6 @@ from litellm.utils import add_dummy_tool, has_tool_call_blocks, supports_reasonifrom ..common_utils import BedrockError, BedrockModelInfo, get_bedrock_tool_name-# Bedrock Computer Use Tools (Anthropic Claude on Bedrock)-BEDROCK_COMPUTER_USE_TOOLS = ["computer_", "bash_", "text_editor_", "web_search_"]-class AmazonConverseConfig(BaseConfig):"""@@ -221,62 +218,6 @@ class AmazonConverseConfig(BaseConfig):+ self.get_supported_video_types())- def is_computer_use_tool_used(- self, tools: Optional[List[OpenAIChatCompletionToolParam]]- ) -> bool:- """Check if computer use tools are being used in the request."""- if tools is None:- return False- for tool in tools:- if "type" in tool:- tool_type = tool["type"]- for computer_use_prefix in BEDROCK_COMPUTER_USE_TOOLS:- if tool_type.startswith(computer_use_prefix):- return True- return False-- def _transform_computer_use_tools(- self, computer_use_tools: List[OpenAIChatCompletionToolParam]- ) -> List[dict]:- """Transform computer use tools to Bedrock format."""- transformed_tools: List[dict] = []-- for tool in computer_use_tools:- tool_type = tool.get("type", "")-- # Check if this is a computer use tool with the startswith method- is_computer_use_tool = False- for computer_use_prefix in BEDROCK_COMPUTER_USE_TOOLS:- if tool_type.startswith(computer_use_prefix):- is_computer_use_tool = True- break-- transformed_tool: dict = {}- if is_computer_use_tool:- if tool_type.startswith("computer_") and "function" in tool:- # Computer use tool with function format- func = tool["function"]- transformed_tool = {- "type": tool_type,- "name": func.get("name", "computer"),- **func.get("parameters", {})- }- else:- # Direct tools - just need to ensure name is present- transformed_tool = dict(tool)- if "name" not in transformed_tool:- if tool_type.startswith("bash_"):- transformed_tool["name"] = "bash"- elif tool_type.startswith("text_editor_"):- transformed_tool["name"] = "str_replace_editor"- else:- # Pass through other tools as-is- transformed_tool = dict(tool)-- transformed_tools.append(transformed_tool)-- return transformed_tools-def _create_json_tool_call_for_response_format(self,json_schema: Optional[dict] = None,@@ -605,37 +546,9 @@ class AmazonConverseConfig(BaseConfig):self._handle_top_k_value(model, inference_params))- original_tools = inference_params.get("tools", [])-- # Separate computer use tools from regular function tools- computer_use_tools = []- regular_tools = []-- for tool in original_tools:- if "type" in tool:- tool_type = tool["type"]- is_computer_use_tool = False- for computer_use_prefix in BEDROCK_COMPUTER_USE_TOOLS:- if tool_type.startswith(computer_use_prefix):- is_computer_use_tool = True- break- if is_computer_use_tool:- computer_use_tools.append(tool)- else:- regular_tools.append(tool)- else:- regular_tools.append(tool)-- # Process regular function tools using existing logic- bedrock_tools: List[ToolBlock] = _bedrock_tools_pt(regular_tools)-- # Add computer use tools and anthropic_beta if needed- if computer_use_tools:- additional_request_params["anthropic_beta"] = ["computer-use-2024-10-22"]- # Transform computer use tools to proper Bedrock format- transformed_computer_tools = self._transform_computer_use_tools(computer_use_tools)- additional_request_params["tools"] = transformed_computer_tools-+ bedrock_tools: List[ToolBlock] = _bedrock_tools_pt(+ inference_params.pop("tools", [])+ )bedrock_tool_config: Optional[ToolConfigBlock] = Noneif len(bedrock_tools) > 0:tool_choice_values: ToolChoiceValuesBlock = inference_params.pop(diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.pyindex 70d8fa923ed..b4e8ca93c1f 100644--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py@@ -1,7 +1,6 @@import jsonimport osimport sys-import asyncioimport pytestfrom fastapi.testclient import TestClient@@ -12,7 +11,6 @@ sys.path.insert(from unittest.mock import MagicMock, patchimport litellm-from litellm import completion, RateLimitError, ModelResponsefrom litellm.llms.bedrock.chat.converse_transformation import AmazonConverseConfigfrom litellm.types.llms.bedrock import ConverseTokenUsageBlock@@ -270,669 +268,3 @@ def test_get_supported_openai_params_bedrock_converse():assert set(supported_params_without_prefix) == set(supported_params_with_prefix), f"Supported params mismatch for model: {model}. Without prefix: {supported_params_without_prefix}, With prefix: {supported_params_with_prefix}"print(f"✅ Passed for model: {model}")---def test_transform_request_helper_includes_anthropic_beta_and_tools():- """Test _transform_request_helper includes anthropic_beta for computer tools."""- config = AmazonConverseConfig()- system_content_blocks = []- optional_params = {- "anthropic_beta": ["computer-use-2024-10-22"],- "tools": [- {- "type": "computer_20241022",- "name": "computer",- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- }- ],- "some_other_param": 123,- }- data = config._transform_request_helper(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- system_content_blocks=system_content_blocks,- optional_params=optional_params,- messages=None,- )- assert "additionalModelRequestFields" in data- fields = data["additionalModelRequestFields"]- assert "anthropic_beta" in fields- assert fields["anthropic_beta"] == ["computer-use-2024-10-22"]- # Verify computer tool is included- assert "tools" in fields- assert len(fields["tools"]) == 1- assert fields["tools"][0]["type"] == "computer_20241022"---def test_transform_response_with_computer_use_tool():- """Test response transformation with computer use tool call."""- import httpx- from litellm.types.llms.bedrock import ConverseResponseBlock, ConverseTokenUsageBlock- from litellm.llms.bedrock.chat.converse_transformation import AmazonConverseConfig- from litellm.types.utils import ModelResponse-- # Simulate a Bedrock Converse response with a computer-use tool call- response_json = {- "additionalModelResponseFields": {},- "metrics": {"latencyMs": 100.0},- "output": {- "message": {- "role": "assistant",- "content": [- {- "toolUse": {- "toolUseId": "tooluse_123",- "name": "computer",- "input": {- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- },- }- }- ]- }- },- "stopReason": "tool_use",- "usage": {- "inputTokens": 10,- "outputTokens": 5,- "totalTokens": 15,- "cacheReadInputTokenCount": 0,- "cacheReadInputTokens": 0,- "cacheWriteInputTokenCount": 0,- "cacheWriteInputTokens": 0,- },- }- # Mock httpx.Response- class MockResponse:- def json(self):- return response_json- @property- def text(self):- return json.dumps(response_json)-- config = AmazonConverseConfig()- model_response = ModelResponse()- optional_params = {- "tools": [- {- "type": "computer_20241022",- "function": {- "name": "computer",- "parameters": {- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- },- },- }- ]- }- # Call the transformation logic- result = config._transform_response(- model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",- response=MockResponse(),- model_response=model_response,- stream=False,- logging_obj=None,- optional_params=optional_params,- api_key=None,- data=None,- messages=[],- encoding=None,- )- # Check that the tool call is present in the returned message- assert result.choices[0].message.tool_calls is not None- assert len(result.choices[0].message.tool_calls) == 1- tool_call = result.choices[0].message.tool_calls[0]- assert tool_call.function.name == "computer"- args = json.loads(tool_call.function.arguments)- assert args["display_height_px"] == 768- assert args["display_width_px"] == 1024- assert args["display_number"] == 0---def test_transform_response_with_bash_tool():- """Test response transformation with bash tool call."""- import httpx- from litellm.types.llms.bedrock import ConverseResponseBlock, ConverseTokenUsageBlock- from litellm.llms.bedrock.chat.converse_transformation import AmazonConverseConfig- from litellm.types.utils import ModelResponse-- # Simulate a Bedrock Converse response with a bash tool call- response_json = {- "additionalModelResponseFields": {},- "metrics": {"latencyMs": 100.0},- "output": {- "message": {- "role": "assistant",- "content": [- {- "toolUse": {- "toolUseId": "tooluse_456",- "name": "bash",- "input": {- "command": "ls -la *.py"- },- }- }- ]- }- },- "stopReason": "tool_use",- "usage": {- "inputTokens": 8,- "outputTokens": 3,- "totalTokens": 11,- "cacheReadInputTokenCount": 0,- "cacheReadInputTokens": 0,- "cacheWriteInputTokenCount": 0,- "cacheWriteInputTokens": 0,- },- }- # Mock httpx.Response- class MockResponse:- def json(self):- return response_json- @property- def text(self):- return json.dumps(response_json)-- config = AmazonConverseConfig()- model_response = ModelResponse()- optional_params = {- "tools": [- {- "type": "bash_20241022",- "function": {- "name": "bash",- "parameters": {},- },- }- ]- }- # Call the transformation logic- result = config._transform_response(- model="bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0",- response=MockResponse(),- model_response=model_response,- stream=False,- logging_obj=None,- optional_params=optional_params,- api_key=None,- data=None,- messages=[],- encoding=None,- )- # Check that the tool call is present in the returned message- assert result.choices[0].message.tool_calls is not None- assert len(result.choices[0].message.tool_calls) == 1- tool_call = result.choices[0].message.tool_calls[0]- assert tool_call.function.name == "bash"- args = json.loads(tool_call.function.arguments)- assert args["command"] == "ls -la *.py"---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- 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/anthropic.claude-3-5-sonnet-20241022-v2: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"- ]-- 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}")---async def test_bedrock_computer_use_acompletion():- """Test Bedrock computer use with acompletion function."""-- # Test with computer use tool- tools = [- {- "type": "computer_20241022",- "name": "computer",- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- }- ]-- messages = [- {- "role": "user",- "content": [- {- "type": "text",- "text": "Go to the bedrock console"- },- {- "type": "image_url",- "image_url": {- "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="- }- }- ]- }- ]-- try:- response = await litellm.acompletion(- model="bedrock/anthropic.claude-3-5-sonnet-20241022-v2: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"- ]-- 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}")---async def test_transformation_directly():- """Test the transformation directly to verify the request structure."""-- config = AmazonConverseConfig()-- tools = [- {- "type": "computer_20241022",- "name": "computer",- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- },- {- "type": "bash_20241022",- "name": "bash",- }- ]-- messages = [- {- "role": "user",- "content": "run ls command and find all python files"- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Check that anthropic_beta is set correctly for computer use- assert "anthropic_beta" in additional_fields- assert additional_fields["anthropic_beta"] == ["computer-use-2024-10-22"]-- # Check that tools are present- assert "tools" in additional_fields- assert len(additional_fields["tools"]) == 2-- # Verify tool types- tool_types = [tool.get("type") for tool in additional_fields["tools"]]- assert "computer_20241022" in tool_types- assert "bash_20241022" in tool_types---def test_transform_request_helper_includes_anthropic_beta_and_tools_bash():- """Test _transform_request_helper includes anthropic_beta for bash tools."""- config = AmazonConverseConfig()- system_content_blocks = []- optional_params = {- "anthropic_beta": ["computer-use-2024-10-22"],- "tools": [- {- "type": "bash_20241022",- "name": "bash",- }- ],- "some_other_param": 123,- }- data = config._transform_request_helper(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- system_content_blocks=system_content_blocks,- optional_params=optional_params,- messages=None,- )- assert "additionalModelRequestFields" in data- fields = data["additionalModelRequestFields"]- assert "anthropic_beta" in fields- assert fields["anthropic_beta"] == ["computer-use-2024-10-22"]- # Verify bash tool is included- assert "tools" in fields- assert len(fields["tools"]) == 1- assert fields["tools"][0]["type"] == "bash_20241022"---def test_transform_request_with_multiple_tools():- """Test transformation with multiple tools including computer, bash, and function tools."""- config = AmazonConverseConfig()-- # Use the exact payload from the user's error- tools = [- {- "type": "computer_20241022",- "function": {- "name": "computer",- "parameters": {- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- },- },- },- {- "type": "bash_20241022",- "name": "bash",- },- {- "type": "text_editor_20241022",- "name": "str_replace_editor",- },- {- "type": "function",- "function": {- "name": "get_weather",- "description": "Get the current weather in a given location",- "parameters": {- "type": "object",- "properties": {- "location": {- "type": "string",- "description": "The city and state, e.g. San Francisco, CA",- },- "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},- },- "required": ["location"],- },- }- }- ]-- messages = [- {- "role": "user",- "content": "run ls command and find all python files"- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Check that anthropic_beta is set correctly for computer use- assert "anthropic_beta" in additional_fields- assert additional_fields["anthropic_beta"] == ["computer-use-2024-10-22"]-- # Check that tools are present- assert "tools" in additional_fields- assert len(additional_fields["tools"]) == 3 # computer, bash, text_editor tools-- # Verify tool types- tool_types = [tool.get("type") for tool in additional_fields["tools"]]- assert "computer_20241022" in tool_types- assert "bash_20241022" in tool_types- assert "text_editor_20241022" in tool_types-- # Function tools are processed separately and not included in computer use tools- # They would be in toolConfig if present---def test_transform_request_with_computer_tool_only():- """Test transformation with only computer tool."""- config = AmazonConverseConfig()-- tools = [- {- "type": "computer_20241022",- "name": "computer",- "display_height_px": 768,- "display_width_px": 1024,- "display_number": 0,- }- ]-- messages = [- {- "role": "user",- "content": [- {- "type": "text",- "text": "Go to the bedrock console"- },- {- "type": "image_url",- "image_url": {- "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="- }- }- ]- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Check that anthropic_beta is set correctly for computer use- assert "anthropic_beta" in additional_fields- assert additional_fields["anthropic_beta"] == ["computer-use-2024-10-22"]-- # Check that tools are present- assert "tools" in additional_fields- assert len(additional_fields["tools"]) == 1- assert additional_fields["tools"][0]["type"] == "computer_20241022"---def test_transform_request_with_bash_tool_only():- """Test transformation with only bash tool."""- config = AmazonConverseConfig()-- tools = [- {- "type": "bash_20241022",- "name": "bash",- }- ]-- messages = [- {- "role": "user",- "content": "run ls command and find all python files"- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Check that anthropic_beta is set correctly for computer use- assert "anthropic_beta" in additional_fields- assert additional_fields["anthropic_beta"] == ["computer-use-2024-10-22"]-- # Check that tools are present- assert "tools" in additional_fields- assert len(additional_fields["tools"]) == 1- assert additional_fields["tools"][0]["type"] == "bash_20241022"---def test_transform_request_with_text_editor_tool():- """Test transformation with text editor tool."""- config = AmazonConverseConfig()-- tools = [- {- "type": "text_editor_20241022",- "name": "str_replace_editor",- }- ]-- messages = [- {- "role": "user",- "content": "Edit this text file"- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Check that anthropic_beta is set correctly for computer use- assert "anthropic_beta" in additional_fields- assert additional_fields["anthropic_beta"] == ["computer-use-2024-10-22"]-- # Check that tools are present- assert "tools" in additional_fields- assert len(additional_fields["tools"]) == 1- assert additional_fields["tools"][0]["type"] == "text_editor_20241022"---def test_transform_request_with_function_tool():- """Test transformation with function tool."""- config = AmazonConverseConfig()-- tools = [- {- "type": "function",- "function": {- "name": "get_weather",- "description": "Get the current weather in a given location",- "parameters": {- "type": "object",- "properties": {- "location": {- "type": "string",- "description": "The city and state, e.g. San Francisco, CA",- },- "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},- },- "required": ["location"],- },- }- }- ]-- messages = [- {- "role": "user",- "content": "What's the weather like in San Francisco?"- }- ]-- # Transform request- request_data = config.transform_request(- model="anthropic.claude-3-5-sonnet-20241022-v2:0",- messages=messages,- optional_params={"tools": tools},- litellm_params={},- headers={}- )-- # Verify the structure- assert "additionalModelRequestFields" in request_data- additional_fields = request_data["additionalModelRequestFields"]-- # Function tools are not computer use tools, so they don't get anthropic_beta- # They are processed through the regular tool config- assert "toolConfig" in request_data- assert "tools" in request_data["toolConfig"]- assert len(request_data["toolConfig"]["tools"]) == 1- assert request_data["toolConfig"]["tools"][0]["toolSpec"]["name"] == "get_weather"