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

Amazon Lex报Invalid Lambda Response错误:无法将Lex数据存入DynamoDB

问题原因分析与解决方案

从你提供的对话状态和Lambda代码来看,核心问题是你的Lambda函数违背了Amazon Lex的对话流程预期:

当前Lex的dialogState是ElicitSlot,说明它还在收集必填槽位(QB的值为null,正是需要用户补充的槽位),但你的Lambda函数不管当前对话处于什么阶段,直接调用close()返回了Close类型的对话动作——Lex此时期望的是继续引导用户填写缺失的槽位,而不是直接关闭对话,这就导致了"Invalid Lambda Response"错误。

另外,你的代码还存在一个潜在问题:在storeRegistration中直接将所有槽位存入DynamoDB,但此时QB是null,这会导致DynamoDB存入无效数据(如果QB是表中的必填属性,还会触发写入失败)。


修复步骤

1. 添加槽位引导的辅助函数

首先,添加一个用于生成ElicitSlot类型响应的函数,让Lambda可以在槽位未收集完成时返回符合Lex预期的响应:

function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message) {
  return {
    sessionAttributes,
    dialogAction: {
      type: 'ElicitSlot',
      intentName,
      slots,
      slotToElicit,
      message,
    },
  };
}

2. 修改主Handler的逻辑,根据对话状态分支处理

在主Handler中,先判断当前的dialogState,只有当所有槽位收集完成(dialogState为ReadyForFulfillment)时,才执行DynamoDB存储操作;否则继续引导用户补充缺失的槽位:

exports.handler = (event, context, callback) => {
  console.log(event);
  try {
    // 只有当所有槽位收集完成时,才执行存储
    if (event.dialogState === 'ReadyForFulfillment') {
      storeRegistration(event, (response) => { callback(null, response) });
    } else {
      // 槽位未收集完成,返回引导响应
      const message = {
        contentType: 'PlainText',
        content: `Could you please provide the answer for ${event.slotToElicit}?`
      };
      callback(null, elicitSlot(
        event.sessionAttributes,
        event.intentName,
        event.slots,
        event.slotToElicit,
        message
      ));
    }
  } catch (err) {
    callback(err);
  }
};

3. 给存储函数添加兜底检查

为了避免意外情况(比如Lex的状态判断出现偏差),在storeRegistration中添加必填槽位的检查,确保所有需要的槽位都有值后再写入DynamoDB:

function storeRegistration(intent, callback) {
  // 定义所有必填槽位
  const requiredSlots = ['QA', 'QB', 'Session'];
  // 检查是否有缺失的槽位
  const missingSlots = requiredSlots.filter(slot => !intent.slots[slot]);

  if (missingSlots.length > 0) {
    // 如果还有缺失槽位,返回引导响应
    callback(elicitSlot(
      intent.sessionAttributes,
      intent.intentName,
      intent.slots,
      missingSlots[0],
      {
        contentType: 'PlainText',
        content: `Please provide the value for ${missingSlots[0]}.`
      }
    ));
    return;
  }

  // 构造DynamoDB的Item对象
  let userInfo = {};
  Object.keys(intent.slots).forEach((item) => {
    console.log(item);
    userInfo[item] = { "S": intent.slots[item] };
  });

  dynamodb.putItem({
    "TableName": tableName,
    "Item": userInfo
  }, function(err, data) {
    if (err) {
      console.log('Failure storing user info');
      console.log(err);
      callback(close(intent.sessionAttributes, { contentType: 'PlainText', content: "I am sorry, but something went wrong. Please try again." }));
    } else {
      console.log("Successfully Stored UserInfo");
      callback(close(intent.sessionAttributes, { contentType: 'PlainText', content: "Thank you for attending the MCQ." }));
    }
  });
}

额外注意事项

  • 确保在Amazon Lex的MCQ_A意图设置中,将QB标记为必填槽位,并配置好对应的提示语,这样Lex会自动处理槽位收集流程,直到所有必填槽位都获取到值后,才会将dialogState设置为ReadyForFulfillment。
  • 检查DynamoDB表MCQ的结构,确保QA、QB、Session这些属性的配置(比如是否为必填项、数据类型)和Lambda写入的内容匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:18:11