AWS Lex与Lambda新手求教:修复无效槽值响应格式错误
Hey there, let's break down why you're hitting this error and how to fix it. The core issue here is that AWS Lex expects a full IntentResponse structure from your Lambda function, but right now your build_validation_result function is only returning a partial validation object—Lex doesn't know what to do with that alone.
Why the Error Happens
The error message says it can't construct an IntentResponse because your Lambda is sending back just the validation details:
{"isValid": false, "violatedSlot": "BikeType", "message": {"contentType": "PlainText", "content": "We do not have such type of bikes..."}}
Lex needs a wrapped response that includes sessionAttributes and a dialogAction (telling Lex what to do next—like re-prompting for the invalid slot).
How to Fix the Response Format
First, keep your existing build_validation_result function—it's still useful for creating the validation details. Then, add a helper function to build the full Lex-compliant response, and update your main Lambda handler to use it when validation fails.
Here's the corrected code:
def build_validation_result(is_valid, violated_slot, message_content): if message_content is None: return { "isValid": is_valid, "violatedSlot": violated_slot, } return { 'isValid': is_valid, 'violatedSlot': violated_slot, 'message': {'contentType': 'PlainText', 'content': message_content} } # New helper to build full Lex response def build_lex_response(session_attributes, dialog_action): return { 'sessionAttributes': session_attributes, 'dialogAction': dialog_action } def lambda_handler(event, context): # Extract intent and slot data from the Lex event intent_name = event['currentIntent']['name'] session_attributes = event.get('sessionAttributes', {}) slots = event['currentIntent']['slots'] bike_type = slots['BikeType'] # Validate the BikeType slot valid_bike_types = ['Men', 'Women', 'Unisex', 'Kids'] validation_result = build_validation_result( bike_type in valid_bike_types, 'BikeType', "We do not have such type of bikes, the popular types are Men/Women/Unisex/Kids." if bike_type else None ) if not validation_result['isValid']: # Clear the invalid slot value to let the user re-enter it slots[validation_result['violatedSlot']] = None # Return the full ElicitSlot response to Lex return build_lex_response( session_attributes, { 'type': 'ElicitSlot', 'intentName': intent_name, 'slots': slots, 'slotToElicit': validation_result['violatedSlot'], 'message': validation_result['message'] } ) # If validation passes, proceed with your intent logic here... # For example, close the dialog with a confirmation: return build_lex_response( session_attributes, { 'type': 'Close', 'fulfillmentState': 'Fulfilled', 'message': { 'contentType': 'PlainText', 'content': f"Got it, we have {bike_type} bikes available!" } } )
Key Details to Note
dialogActionType: When a slot is invalid, useElicitSlot—this tells Lex to ask the user for the correct value again.- Required Fields: The
ElicitSlotaction needsintentName,slots(with the invalid slot cleared),slotToElicit, and your errormessage. - Session Attributes: Always include
sessionAttributes(even if it's an empty dict) to comply with Lex's response schema.
This structure will match what Lex expects, and your error should disappear.
内容的提问来源于stack exchange,提问作者odyssey

