-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anthropic
models cannot call tools without parameters
#7872
Comments
Hey @Domiii! I'm here to help you with any bugs, questions, or contributions. Let's dive into this issue together! A similar issue has been discussed in the LangChain.js repository, specifically in the thread titled "ChatAnthropic streamEvents not working with bindTools" which is currently unsolved [1]. The error you're encountering, "messages.1.content.1.tool_use.input: Input should be a valid dictionary," is due to the export function _convertLangChainToolCallToAnthropic(
toolCall: ToolCall
): AnthropicToolResponse {
if (toolCall.id === undefined) {
throw new Error(`Anthropic requires all tool calls to have an "id".`);
}
return {
type: "tool_use",
id: toolCall.id,
name: toolCall.name,
input: typeof toolCall.args === 'object' ? toolCall.args : {}, // Ensure input is a valid object
};
} This modification ensures that if To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
@dosu: Your response is entirely unrelated. Please remove it: My bug has a very specific error message that happens when providing tools that have empty |
This is a tough nut to crack, could really use your help on this, @jacoblee93. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
@dosu: You are almost right about the bug location, but there is a twist:
|
To address the issue where
Here's the relevant code snippet with the fix: export function _convertMessagesToAnthropicPayload(
messages: BaseMessage[]
): AnthropicMessageCreateParams {
const mergedMessages = _ensureMessageContents(messages);
let system;
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === "system") {
system = messages[0].content;
}
const conversationMessages =
system !== undefined ? mergedMessages.slice(1) : mergedMessages;
const formattedMessages = conversationMessages.map((message) => {
let role;
if (message._getType() === "human") {
role = "user" as const;
} else if (message._getType() === "ai") {
role = "assistant" as const;
} else if (message._getType() === "tool") {
role = "user" as const;
} else if (message._getType() === "system") {
throw new Error(
"System messages are only permitted as the first passed message."
);
} else {
throw new Error(`Message type "${message._getType()}" is not supported.`);
}
if (isAIMessage(message) && !!message.tool_calls?.length) {
if (typeof message.content === "string") {
if (message.content.trim() === "") {
return {
role,
content: message.tool_calls.map(
_convertLangChainToolCallToAnthropic
),
};
} else {
return {
role,
content: [
{ type: "text", text: message.content },
...message.tool_calls.map(_convertLangChainToolCallToAnthropic),
],
};
}
} else {
const { content } = message;
const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>
content.find(
(contentPart) =>
(contentPart.type === "tool_use" ||
contentPart.type === "input_json_delta") &&
contentPart.id === toolCall.id
)
);
if (hasMismatchedToolCalls) {
console.warn(
`The "tool_calls" field on a message is only respected if content is a string.`
);
}
return {
role,
content: _formatContent(message.content),
};
}
} else {
return {
role,
content: _formatContent(message.content),
};
}
});
return {
messages: mergeMessages(formattedMessages),
system,
} as AnthropicMessageCreateParams;
} This fix ensures that To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
We should find out what the actual control flow is in case of a non-empty schema (which works fine) and make sure that the two are in line with one another. |
Hey @Domiii! I see you're diving deep into LangChain's internals, but I'm just a bot here to help with bugs, technical questions, and engineering knowledge. Your query is a bit outside my wheelhouse. If you think I should be able to tackle this, feel free to reach out to the Dosu Team. They'll know what to do! 😊 To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
I'm having the exact same issue when using ChatAnthropic model
|
Checked other resources
Example Code
My code has a lot of boilerplate already, but:
createReactAgent
with the latestsonnet
model.streamEvents
on said agent.import { tool } from "@langchain/core/tools"
) that hasschema
zod.object({ })
.@langchain/anthropic
, but still it still happens.Error Message and Stack Trace (if applicable)
Debugging + Investigation
Full Error
Investigation
input: ""
.input: {}
.Bad
tool_use
messagecontents
input: {}
:input: ""
in this code in_makeMessageChunkFromAnthropicEvent
inutils/message_output.ts
:Description
(I explained everything above)
System Info
node@18
npm
The text was updated successfully, but these errors were encountered: