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(anthropic.py): fix translation from /v1/messages format to openai format

Krrish Dholakia4ac0afef2cbb
diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py
index 78888cf4adc..20c74fb3d50 100644
--- a/litellm/llms/anthropic.py
+++ b/litellm/llms/anthropic.py
@@ -287,6 +287,9 @@ class AnthropicConfig:
if user_message is not None:
new_messages.append(user_message)
+ if len(new_user_content_list) > 0:
+ new_messages.append({"role": "user", "content": new_user_content_list})
+
if len(tool_message_list) > 0:
new_messages.extend(tool_message_list)
diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yaml
index 0388b4b94a6..6c50147340f 100644
--- a/litellm/proxy/_new_secret_config.yaml
+++ b/litellm/proxy/_new_secret_config.yaml
@@ -1,11 +1,7 @@
model_list:
- - model_name: "gpt-4"
+ - model_name: "claude-3-5-sonnet-20240620"
litellm_params:
- model: "gpt-4"
- api_key: "bad_key"
- - model_name: "gpt-4o"
- litellm_params:
- model: "gpt-4o"
+ model: "claude-3-5-sonnet-20240620"
litellm_settings:
max_internal_user_budget: 0.001
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index c9e9e3420e5..5dc2586d39d 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -5373,7 +5373,13 @@ async def anthropic_response(
litellm.adapters = [{"id": "anthropic", "adapter": anthropic_adapter}]
global user_temperature, user_request_timeout, user_max_tokens, user_api_base
- data: dict = {**anthropic_data, "adapter_id": "anthropic"}
+ body = await request.body()
+ body_str = body.decode()
+ try:
+ request_data: dict = ast.literal_eval(body_str)
+ except Exception:
+ request_data = json.loads(body_str)
+ data: dict = {**request_data, "adapter_id": "anthropic"}
try:
data["model"] = (
general_settings.get("completion_model", None) # server default
diff --git a/litellm/tests/test_anthropic_completion.py b/litellm/tests/test_anthropic_completion.py
index 3611a44caef..20260556ec2 100644
--- a/litellm/tests/test_anthropic_completion.py
+++ b/litellm/tests/test_anthropic_completion.py
@@ -183,3 +183,96 @@ async def test_anthropic_router_completion_e2e():
assert isinstance(response, AnthropicResponse)
assert response.model == "gpt-3.5-turbo"
+
+
+def test_anthropic_tool_calling_translation():
+ kwargs = {
+ "model": "claude-3-5-sonnet-20240620",
+ "messages": [
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": "Would development of a software platform be under ASC 350-40 or ASC 985?",
+ }
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "37d6f703-cbcc-497d-95a1-2aa24a114adc",
+ "name": "TaskPlanningTool",
+ "input": {
+ "completed_steps": [],
+ "next_steps": [
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Research ASC 350-40 to understand its scope and applicability to software development.",
+ },
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Research ASC 985 to understand its scope and applicability to software development.",
+ },
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Compare the scopes of ASC 350-40 and ASC 985 to determine which is more applicable to software platform development.",
+ },
+ ],
+ "learnings": [],
+ "potential_issues": [
+ "The distinction between the two standards might not be clear-cut for all types of software development.",
+ "There might be specific circumstances or details about the software platform that could affect which standard applies.",
+ ],
+ "missing_info": [
+ "Specific details about the type of software platform being developed (e.g., for internal use or for sale).",
+ "Whether the entity developing the software is also the end-user or if it's being developed for external customers.",
+ ],
+ "done": False,
+ "required_formatting": None,
+ },
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "eb7023b1-5ee8-43b8-b90f-ac5a23d37c31",
+ "content": {
+ "completed_steps": [],
+ "next_steps": [
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Research ASC 350-40 to understand its scope and applicability to software development.",
+ },
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Research ASC 985 to understand its scope and applicability to software development.",
+ },
+ {
+ "tool_name": "AccountingResearchTool",
+ "description": "Compare the scopes of ASC 350-40 and ASC 985 to determine which is more applicable to software platform development.",
+ },
+ ],
+ "formatting_step": None,
+ },
+ }
+ ],
+ },
+ ],
+ }
+
+ from litellm.adapters.anthropic_adapter import anthropic_adapter
+
+ translated_params = anthropic_adapter.translate_completion_input_params(
+ kwargs=kwargs
+ )
+
+ print(translated_params["messages"])
+
+ assert len(translated_params["messages"]) > 0
+ assert translated_params["messages"][1]["role"] == "user"