Skip to content

Commit 1d21782

Browse files
committed
improve anthropic tool calling
1 parent 071125a commit 1d21782

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

llms/extensions/providers/anthropic.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ async def chat(self, chat, context=None):
8383

8484
content = message.get("content", "")
8585
if isinstance(content, str):
86-
if anthropic_message["content"]:
87-
# If we have thinking, we must use blocks for text
88-
anthropic_message["content"].append({"type": "text", "text": content})
86+
if anthropic_message["content"] or message.get("tool_calls"):
87+
# If we have thinking or tools, we must use blocks for text
88+
if content:
89+
anthropic_message["content"].append({"type": "text", "text": content})
8990
else:
9091
anthropic_message["content"] = content
9192
elif isinstance(content, list):
@@ -108,6 +109,24 @@ async def chat(self, chat, context=None):
108109
}
109110
)
110111

112+
# Handle tool_calls
113+
if "tool_calls" in message and message["tool_calls"]:
114+
# specific check for content being a string and not empty, because we might have converted it above
115+
if isinstance(anthropic_message["content"], str):
116+
anthropic_message["content"] = []
117+
if content:
118+
anthropic_message["content"].append({"type": "text", "text": content})
119+
120+
for tool_call in message["tool_calls"]:
121+
function = tool_call.get("function", {})
122+
tool_use = {
123+
"type": "tool_use",
124+
"id": tool_call.get("id"),
125+
"name": function.get("name"),
126+
"input": json.loads(function.get("arguments", "{}")),
127+
}
128+
anthropic_message["content"].append(tool_use)
129+
111130
anthropic_request["messages"].append(anthropic_message)
112131

113132
# Handle max_tokens (required by Anthropic, uses max_tokens not max_completion_tokens)

0 commit comments

Comments
 (0)