fix(anthropic): tool_choice format and tool response parsing (#5537)

Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
Hrushikesh Yadav
2026-06-15 17:28:45 +05:30
committed by GitHub
co-authored by kartik-mem0
parent 4492e75d04
commit b55c51e004
5 changed files with 345 additions and 11 deletions
+15 -2
View File
@@ -35,7 +35,7 @@ class AnthropicLLM(LLMBase):
super().__init__(config)
if not self.config.model:
self.config.model = "claude-3-5-sonnet-20240620"
self.config.model = "claude-sonnet-4-6"
api_key = self.config.api_key or os.getenv("ANTHROPIC_API_KEY")
self.client = anthropic.Anthropic(api_key=api_key)
@@ -106,7 +106,20 @@ class AnthropicLLM(LLMBase):
if tools: # TODO: Remove tools if no issues found with new memory addition logic
params["tools"] = tools
params["tool_choice"] = tool_choice
params["tool_choice"] = {"type": tool_choice}
response = self.client.messages.create(**params)
return self._parse_response(response, tools)
def _parse_response(self, response, tools):
if tools:
result = {"content": None, "tool_calls": []}
for block in response.content:
if block.type == "text":
result["content"] = block.text
elif block.type == "tool_use":
result["tool_calls"].append(
{"name": block.name, "arguments": block.input}
)
return result
return response.content[0].text