You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LangGraphJs中将Agent添加到Graph?类型不匹配报错求助

LangGraphJS 嵌套Agent工作流报错问题

问题场景

我用LangGraphJS构建了如下工作流:

const workflow = new StateGraph(MessagesAnnotation)
  .addNode('fooAgent', fooAgent)
  .addNode('barAgent', barAgent)
  .addEdge(START, 'fooAgent')
  .addEdge('fooAgent', 'barAgent')
  .addEdge('barAgent', END);
const graph = workflow.compile({
  checkpointer: checkpointSaver,
});

运行时触发以下错误:

file:///Users/user/code/foobar/node_modules/@langchain/core/dist/language_models/chat_models.js:64
        return chatGeneration.message;
                              ^

TypeError: Cannot read properties of undefined (reading 'message')
    at ChatOpenAI.invoke (file:///Users/user/code/foobar/node_modules/@langchain/core/dist/language_models/chat_models.js:64:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async RunnableSequence.invoke (file:///Users/user/code/foobar/node_modules/@langchain/core/dist/runnables/base.js:1280:27)
    at async RunnableCallable.callModel [as func] (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/prebuilt/react_agent_executor.js:227:29)
    at async RunnableCallable.invoke (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/utils.js:79:27)
    at async RunnableSequence.invoke (file:///Users/user/code/foobar/node_modules/@langchain/core/dist/runnables/base.js:1274:33)
    at async _runWithRetry (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/pregel/retry.js:67:22)
    at async PregelRunner._executeTasksWithRetry (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/pregel/runner.js:208:33)
    at async PregelRunner.tick (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/pregel/runner.js:45:40)
    at async CompiledStateGraph._runLoop (file:///Users/user/code/foobar/node_modules/@langchain/langgraph/dist/pregel/index.js:1015:17) {
  pregelTaskId: 'c9848e05-19d9-5587-b2b4-4728ad808c34'
}

Node.js v23.3.0

问题分析

我之前错误地认为addNode可以直接接收Agent,实际存在类型不匹配:

  • 通过createReactAgent创建的Agent类型为CompiledStateGraph(参考@langchain/langgraph/dist/prebuilt/react_agent_executor.d.ts)
  • addNode要求的参数类型为RunnableLike(参考@langchain/core/dist/runnables/base.d.ts)

我想到两种解决方向:

  • 方案1:使用其他方法将Agent图添加到工作流图中
  • 方案2:将Agent图转换为符合要求的节点后再添加到工作流图中

请问这两种方案是否可行?

补充代码

Agent初始化代码:

const checkpointSaver = new MemorySaver();
const fooAgent = createReactAgent({
  llm,
  tools,
  checkpointSaver,
});
/* 为Agent添加特定能力的代码 */
const barAgent = createReactAgent({
  llm,
  tools,
  checkpointSaver,
});
/* 为Agent添加特定能力的代码 */

工作流执行代码:

const inputs = { messages: [new HumanMessage('Please do the following tasks ...')] }
const stream = await graph.stream(inputs, { streamMode: 'values' });
for await (const { messages } of stream) {
  console.log(messages);
}

内容的提问来源于stack exchange,提问作者bguiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:43:13