1

I'm creating an app for searching flight connections with Amazon Alexa. I'm trying to load a json file containing airport informations into a variable to search data from it at a later point. I tried to load it into a global variable but that didn't work. Then I tried to load it into the session_attributes, but this also fails. The json file contains 12668 entries. What am I doing wrong? What would be a good way to do this? Is it taking too much time to load the json into a variable and Alexa quits because of this?

The json file is located in the lambda folder in the alexa developer console, where also the file with the code is.

Alexa responds with "There was a problem with the requests skill's response"

"error": {
            "type": "INVALID_RESPONSE",
            "message": "An exception occurred while dispatching the request to the skill."
        }

here is my basic code:

airportdata = []


def init():
    airportdata = load_airport_data()
    

def load_airport_data():
    with open('airportdata.json', encoding="utf8") as json_file: 
        return json.load(json_file)


class LaunchRequestHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        return ask_utils.is_request_type("LaunchRequest")(handler_input)

    def handle(self, handler_input):
        # init()
        
        session_attr = handler_input.attributes_manager.session_attributes

        session_attr['airportdata'] = load_airport_data()

        speak_output = "Welcome to the App. From where will you departure?"
        
        return (
            handler_input.response_builder
                .speak(speak_output)
                .ask(speak_output)
                .response
        )

EDIT: It turned out the path used was not correct. It should be "/var/task/airportdata.json" instead of just the name of the json file. The files of the lambda gets copied there. Then it works as intended.

TheQuestioner
  • 724
  • 2
  • 8
  • 13

2 Answers2

1

A couple of points that might be worth considering:

  • Can you increase the memory allocation and/or increase the lambda timeout? Increasing the memory allocation also provides more CPU power to the lambda function, which could speed up reading in the JSON file.
  • Only read in the JSON file when it's needed to fulfill the request, e.g. unless you're using the information to provide some context in the LaunchRequest response, save time by not loading it, and only load it for specific IntentRequests.
  • Use session attributes, but only for data that's specific for that session (e.g. user's chosen departure and destination airports). This will again help you avoid loading the large JSON file unless strictly required. That said, avoid storing the whole JSON data structure in the session attributes.
  • Given that it's a relatively large object, could you decrease the load time by using a different decoder or library?
Oscar Schafer
  • 1,215
  • 1
  • 12
  • 25
  • Thanks for the answer. The file is a half megabyte in size. I will try out to read from the json on every call and hope it will not timeout. I could also try to decrease the size of the json by removing some entries that are probably not needed. – TheQuestioner Mar 30 '21 at 20:25
1

Don't load it into the session attributes. Even if it doesn't exceed the maximum attribute size, it's going to get sent back and forth on every subsequent request. Very expensive way of doing things.

You don't mention how large the file is. Parsing a really large file from text to an object is computationally expensive. You don't want to do it every call or any call really. You should be storing it properly in a database and running an API call out to the database.

Last, if you're running this in AWS Lambda, especially if you're using Alexa Hosted, look in your CloudWatch logs to see what the actual error is.

LetMyPeopleCode
  • 1,895
  • 15
  • 20
  • Thanks for the answer. The file is around a half megabyte in size. I sadly don't have access to cloudwatch and a database, since I only have a alexa developer account. I would need a regular aws account? – TheQuestioner Mar 30 '21 at 20:20
  • If you only have a regular Alexa developer account, then your Cloudwatch access is through an icon at the top of the code editor for your skill in the developer console. The developer console provides access to an S3 bucket, Cloudwatch logs, and a DynamoDb table that were provisioned for your Alexa-Hosted skill via the links at the top of that tab. – LetMyPeopleCode Mar 31 '21 at 20:21
  • Thanks! It turned out that the default cloudwatch wasn't correct, I had to change it to US-East, there I could see the logs. Then I found the problem, the path wasn't correct. Should be /var/task/airportdata.json – TheQuestioner Apr 07 '21 at 18:31