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

LangChain AgentExecutor调用报错:输入键不符或缺失input变量

问题根源

你遇到的报错核心是AgentExecutor与Agent的输入变量不匹配:Agent默认只接受input单一输入键,但你需要传入chat_history,同时EntityMemory的配置和Prompt模板没有对齐多输入要求。

解决步骤

  1. 更新Agent的Prompt模板,包含chat_history变量
    必须在Prompt模板中明确加入{chat_history}占位符,同时将chat_history列为输入变量,让Agent知道需要处理这个参数:

    import { PromptTemplate } from "langchain/prompts";
    
    const agentPrompt = PromptTemplate.fromTemplate(`
    你是数据库答疑机器人,结合以下聊天历史和当前问题,用工具查询数据库后回答:
    
    聊天历史:{chat_history}
    当前问题:{input}
    
    严格按要求格式输出思考与回答。
    `);
    // 验证输入变量是否包含input和chat_history
    console.log(agentPrompt.inputVariables); // 应输出 ["chat_history", "input"]
    
  2. 配置Agent接受多输入键
    创建Agent时(比如用createSqlAgent),显式指定inputKeys为["input", "chat_history"],告知Agent需要处理这两个输入:

    const agent = await createSqlAgent({
      llm: new ChatOpenAI({ temperature: 0, modelName: "gpt-3.5-turbo" }),
      db: yourSqlDatabase,
      tools: yourDatabaseTools,
      inputKeys: ["input", "chat_history"], // 关键配置
    });
    
  3. 正确调用executor.call(),无需手动传入chat_history
    EntityMemory会自动通过loadMemoryVariables()方法将聊天历史注入输入,你只需传入input即可:

    const response = await executor.call({ input: question });
    console.log(response.output);
    

    若要手动传入特定实体历史,需暂时禁用记忆的自动注入,或确保传入的chat_history键与Agent输入变量完全匹配,同时保持Agent的多输入配置。

  4. 对齐EntityMemory的memoryKey
    确认EntityMemory的memoryKey与Prompt模板中的占位符一致,默认是history,如果用chat_history需显式设置:

    import { EntityMemory } from "langchain/memory";
    
    const entityMemory = new EntityMemory({
      llm: new ChatOpenAI({ temperature: 0 }),
      memoryKey: "chat_history", // 与Prompt中的{chat_history}对应
    });
    

报错原因复盘

  • 传入{input: question, chat_history: ...}时:Agent的Prompt模板仅定义了input一个输入变量,无法识别第二个键,因此抛出“One input key expected”。
  • 移除input后:Agent必须的input变量缺失,触发“Missing value for input variable input”。
  • 使用executor.run()时:该方法仅支持单一输入变量的Chain,你的Agent需要多输入,因此报错“expects multiple inputs, cannot use 'run'”。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:07:32