You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added proper message formatting for tool calls and results in chat history when using Anthropic agent. This makes Anthropic agent handle tool history consistently with OpenAI agent, which already had this functionality.
Problem Solved
Previously, when providing chat history with tool calls to Anthropic agent, it would throw API errors about incorrect message format. The same chat history worked fine with OpenAI agent. This has been fixed by implementing correct message transformation for Anthropic's specific format requirements.
Error Before Fix
When trying to use chat history with tool calls in Anthropic agent, the following error was thrown:
error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.1.content.1.tool_use.input: Input should be a valid dictionary"}}
at new AnthropicError (path/to/project/node_modules/.pnpm/@anthropic-ai+sdk@0.32.1_encoding@0.1.13/node_modules/@anthropic-ai/sdk/error.mjs:7:9)
at new APIError (path/to/project/node_modules/.pnpm/@anthropic-ai+sdk@0.32.1_encoding@0.1.13/node_modules/@anthropic-ai/sdk/error.mjs:86:9)
This happened because Anthropic's API expects tool calls in a specific format, different from the standard LlamaIndex format that worked with OpenAI.
Chat History with Tool Calls (now works with both agents)
constchatHistory: ChatMessage[]=[{role: "user",content: "What's the weather in London?",},{role: "assistant",content: "Let me check the weather.",options: {toolCall: [{id: "call_123",name: "weather",input: JSON.stringify({location: "London"}),},],},},{role: "tool",content: "The weather in London is sunny, +20°C",options: {toolResult: {id: "call_123",},},},];
Implementation Details
Added proper message format transformation in Anthropic provider
Maintains consistent chat history format across providers while handling provider-specific requirements internally
Tool calls and results are now correctly recognized by Anthropic agent
Testing
Added comprehensive tests to verify:
Correct transformation of tool call messages for Anthropic format
Proper handling of tool results
Consistent behavior with OpenAI agent's existing functionality
Usage Example
Here's a complete example demonstrating the fixed functionality:
import{Anthropic,ChatMessage,FunctionTool}from"llamaindex";import{AnthropicAgent}from"llamaindex/agent/anthropic";// Chat history with a previous tool callconstchatHistory: ChatMessage[]=[{role: "user",content: "What's the weather in London?",},{role: "assistant",content: "Let me check the weather.",options: {toolCall: [{id: "call_123",name: "weather",input: JSON.stringify({location: "London, UK"}),},],},},{role: "tool",content: "The weather in London is sunny, +20°C",options: {toolResult: {id: "call_123",},},},];// Create agent with the weather tool and chat historyconstagent=newAnthropicAgent({llm: newAnthropic({model: "claude-3-opus"}),tools: [FunctionTool.from<{location: string}>((query)=>`The weather in ${query.location} is sunny`,{name: "weather",description: "Get the weather",parameters: {type: "object",properties: {location: {type: "string",description: "The location to get the weather for",},},required: ["location"],},},),],
chatHistory,});// Agent now correctly processes the chat history and demonstrates awareness of the tool call arguments while not making extra tool calls.constresponse=awaitagent.chat({message: "What exact location did you use in the weather tool?",});
The text was updated successfully, but these errors were encountered:
Feature Overview
Added proper message formatting for tool calls and results in chat history when using Anthropic agent. This makes Anthropic agent handle tool history consistently with OpenAI agent, which already had this functionality.
Problem Solved
Previously, when providing chat history with tool calls to Anthropic agent, it would throw API errors about incorrect message format. The same chat history worked fine with OpenAI agent. This has been fixed by implementing correct message transformation for Anthropic's specific format requirements.
Error Before Fix
When trying to use chat history with tool calls in Anthropic agent, the following error was thrown:
This happened because Anthropic's API expects tool calls in a specific format, different from the standard LlamaIndex format that worked with OpenAI.
Chat History with Tool Calls (now works with both agents)
Implementation Details
Testing
Added comprehensive tests to verify:
Usage Example
Here's a complete example demonstrating the fixed functionality:
The text was updated successfully, but these errors were encountered: