2

I am having trouble with sending a JSON response from my python3.8 lambda function (default lambda_handler function). I am pretty sure I understand what I am doing after reading most of the docs and the Lambda Function Input Event and Response Format. from that resource, it says the only required section is the 'dialogAction' section.

Right now, my lex-bot has 1 intent and one slot. I know that this works because when I add a logger to the code, I can see that my lambda function is recieving confirmed JSON format.

My code tries to send a final response from the lambda function, but when I run the lex-bot in the console I get the following error:

Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of IntentResponse, problem: The validated object is null at [Source: {"dialogAction": {"type": "Close", "fulfillmentState": "Fulfilled", "message": {"contentType": "PlainText", "content": "milk"}}}; line: 1, column: 128]

Here is my python code:

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def lambda_handler(event, context):
    # print
    
    item = event["sessionState"]["intent"]["slots"]["MilkProduct"]["value"]["resolvedValues"][0]
    logger.debug(item)
    
    return{
            "dialogAction": {
                "type": "Close",
                "fulfillmentState": "Fulfilled",
                "message": {
                    "contentType": "PlainText",
                    "content": item
                }
            }
        }

I do not think this is necessary for you to see, but here is what the lex-bot is sending me after it confirms the slot for the intent has been confirmed:

{
    "sessionId": "120304235774457",
    "inputTranscript": "I want to buy milk",
    "interpretations": [
        {
            "intent": {
                "slots": {
                    "MilkProduct": {
                        "shape": "Scalar",
                        "value": {
                            "originalValue": "milk",
                            "resolvedValues": [
                                "milk"
                            ],
                            "interpretedValue": "milk"
                        }
                    }
                },
                "confirmationState": "None",
                "name": "BuyCream",
                "state": "ReadyForFulfillment"
            },
            "nluConfidence": 1
        },
        {
            "intent": {
                "slots": {},
                "confirmationState": "None",
                "name": "FallbackIntent",
                "state": "ReadyForFulfillment"
            }
        }
    ],
    "responseContentType": "text/plain; charset=utf-8",
    "invocationSource": "FulfillmentCodeHook",
    "messageVersion": "1.0",
    "sessionState": {
        "intent": {
            "slots": {
                "MilkProduct": {
                    "shape": "Scalar",
                    "value": {
                        "originalValue": "milk",
                        "resolvedValues": [
                            "milk"
                        ],
                        "interpretedValue": "milk"
                    }
                }
            },
            "confirmationState": "None",
            "name": "BuyCream",
            "state": "ReadyForFulfillment"
        },
        "originatingRequestId": "417dff57-5260-45cc-81a7-06df13fbee9a"
    },
    "inputMode": "Text",
    "bot": {
        "aliasId": "TSTALIASID",
        "aliasName": "TestBotAlias",
        "name": "Shopping",
        "version": "DRAFT",
        "localeId": "en_US",
        "id": "JTGNDOEVQG"
    }
}

Can someone please tell me what I am doing wrong? I have been at this for hours and I seriously do not know what I am doing wrong.

Thanks

jellycsc
  • 10,904
  • 2
  • 15
  • 32
Carter ash
  • 67
  • 8
  • Seemingly python code is fine. Is `item` getting value properly? Input event format should be like this. https://docs.aws.amazon.com/lex/latest/dg/lambda-input-response-format.html#using-lambda-input-event-format – shimo Aug 05 '21 at 23:21
  • Agree with @shimo that the python code works fine. Perhaps print out the final response before returning it from the method. It's possible that there's no value for `item` hence the error. – Reegz Aug 06 '21 at 07:54
  • @jellycsc I am getting the value of item perfectly fine. I took the data passed to the lambda function and used it as a template to index the JSON from Amazon Lex-bot. I have tested the response in the lambda function as well and the return object looks fine (from what the docs requested). I am just not sure if I am doing something wrong in the config of the lex-bot in the console UI. – Carter ash Aug 07 '21 at 22:48
  • I notice this is a few months old, but if you are using Amazon Lex V2 the input event format and response formats have changed, see: https://docs.aws.amazon.com/lexv2/latest/dg/lambda.html – Martin Vegas Dec 30 '21 at 15:01

0 Answers0