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(factory.py): handle openai function message having tool call id

Krrish Dholakiadf7ffb5595d1
diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py
index 2cadfed6eb3..7fce1929678 100644
--- a/litellm/llms/prompt_templates/factory.py
+++ b/litellm/llms/prompt_templates/factory.py
@@ -1135,8 +1135,9 @@ def convert_to_anthropic_tool_result(message: dict) -> AnthropicMessagesToolResu
return anthropic_tool_result
if message["role"] == "function":
content = message.get("content") # type: ignore
+ tool_call_id = message.get("tool_call_id") or str(uuid.uuid4())
anthropic_tool_result = AnthropicMessagesToolResultParam(
- type="tool_result", tool_use_id=str(uuid.uuid4()), content=content
+ type="tool_result", tool_use_id=tool_call_id, content=content
)
return anthropic_tool_result
diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py
index 693110bc0da..d2ecd609272 100644
--- a/litellm/tests/test_completion.py
+++ b/litellm/tests/test_completion.py
@@ -4405,6 +4405,3 @@ def test_moderation():
output = response.results[0]
print(output)
return output
-
-
-# test_moderation()
diff --git a/litellm/tests/test_prompt_factory.py b/litellm/tests/test_prompt_factory.py
index 0dc0dbca20b..f7a715a2204 100644
--- a/litellm/tests/test_prompt_factory.py
+++ b/litellm/tests/test_prompt_factory.py
@@ -219,3 +219,44 @@ def test_base64_image_input(url, expected_media_type):
response = convert_to_anthropic_image_obj(openai_image_url=url)
assert response["media_type"] == expected_media_type
+
+
+def test_anthropic_messages_tool_call():
+ messages = [
+ {
+ "role": "user",
+ "content": "Would development of a software platform be under ASC 350-40 or ASC 985?",
+ },
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_call_id": "bc8cb4b6-88c4-4138-8993-3a9d9cd51656",
+ "tool_calls": [
+ {
+ "id": "bc8cb4b6-88c4-4138-8993-3a9d9cd51656",
+ "function": {
+ "arguments": '{"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": null}',
+ "name": "TaskPlanningTool",
+ },
+ "type": "function",
+ }
+ ],
+ },
+ {
+ "role": "function",
+ "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":null}',
+ "name": "TaskPlanningTool",
+ "tool_call_id": "bc8cb4b6-88c4-4138-8993-3a9d9cd51656",
+ },
+ ]
+
+ translated_messages = anthropic_messages_pt(
+ messages, model="claude-3-sonnet-20240229", llm_provider="anthropic"
+ )
+
+ print(translated_messages)
+
+ assert (
+ translated_messages[-1]["content"][0]["tool_use_id"]
+ == "bc8cb4b6-88c4-4138-8993-3a9d9cd51656"
+ )