-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Checked other resources
- This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
- I added a very descriptive title to this issue.
- I searched the LangChain.js documentation with the integrated search.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain.js rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
import dotenv from 'dotenv';
import { createAgent } from 'langchain';
import { tool } from '@langchain/core/tools';
import { z } from 'zod';
import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages';
import { ChatOpenAI } from '@langchain/openai';
dotenv.config();
const agent = createAgent({
model: new ChatOpenAI({
model: 'gpt-5.2',
}),
tools: [
tool(({ a, b }) => String(a + b), {
name: 'add',
description: 'Adds two numbers together',
schema: z.object({
a: z.number(),
b: z.number(),
}),
}),
// get value
tool(
({ key }) => {
return key === 'a' ? 1 : 2;
},
{
name: 'get_value',
description: 'Gets a value for a given key, either "a" or "b"',
schema: z.object({
key: z.string(),
}),
},
),
//print number
tool(
({ value }) => {
console.log('Value:', value);
return 'Printed value to console';
},
{
name: 'print_value',
description: 'Prints a value to the console',
schema: z.object({
value: z.number(),
}),
},
),
],
systemPrompt: `You are a calculator, first, in parallel ask for the values of "a" and "b" using the "get_value" tool. Once you have both values, use the "add" tool to add them together. Finally, use the "print_value" tool to print the result. the explain a summary of what you did and why.`,
});
async function runAgent() {
const response = await agent.invoke({
messages: [
new HumanMessage('Calculate the sum of a and b'),
new AIMessage({
contentBlocks: [
{
type: 'tool_call',
name: 'get_value',
args: {
key: 'a',
},
id: 'call_1',
},
{
type: 'tool_call',
name: 'get_value',
args: {
key: 'b',
},
id: 'call_2',
},
],
}),
new ToolMessage({
tool_call_id: 'call_1',
contentBlocks: [
{
type: 'text',
text: '1',
},
],
}),
new ToolMessage({
tool_call_id: 'call_2',
contentBlocks: [
{
type: 'text',
text: '2',
},
],
}),
],
});
const parts = response.messages.map((v) => [v.type, v.contentBlocks]);
console.log('Agent response:', JSON.stringify(parts, null, 2));
}
runAgent();Error Message and Stack Trace (if applicable)
BadRequestError: 400 Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.
Troubleshooting URL: https://docs.langchain.com/oss/javascript/langchain/errors/INVALID_TOOL_RESULTS/
at APIError.generate (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\openai\src\core\error.ts:72:14)
at OpenAI.makeStatusError (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\openai\src\client.ts:478:28)
at OpenAI.makeRequest (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\openai\src\client.ts:728:24)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async <anonymous> (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\@langchain\openai\src\chat_models\completions.ts:450:18)
at async Object.pRetry (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\@langchain\core\src\utils\p-retry\index.js:236:22)
at async run (C:\Users\specy\Desktop\progetti\discerns\AI-BE\node_modules\p-queue\dist\index.js:163:29) {
status: 400,
headers: Headers {},
requestID: 'req_24ac7f60fba8458ba799d56a77e3d69d',
error: {
message: "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.",
type: 'invalid_request_error',
param: 'messages.[3].role',
code: null
},
code: null,
param: 'messages.[3].role',
type: 'invalid_request_error',
lc_error_code: 'INVALID_TOOL_RESULTS',
pregelTaskId: '02cb5dcf-c44a-5c0b-960e-8c58e8317928'
}
Description
When trying to resume a conversation which has tool calls through the content blocks shown in the code (which should be valid? It does work with ChatAnthropic), i get an invalid tool answer saying there are no messages with that tool call.
System Info
langchain@1.2.18 | MIT | deps: 5 | versions: 381
Windows
node 24