Adding session attributes in Python for Alexa skills Adding session attributes in Python for Alexa skills json json

Adding session attributes in Python for Alexa skills


The easiest way to add sessionAttributes with Python in my opinion seems to be by using a dictionary. For example, if you want to store some of the slots for future in the session attributes:

session['attributes']['slotKey'] = intent['slots']['slotKey']['value']

Next, you can just pass it on to the build response method:

buildResponse(session['attributes'], buildSpeechletResponse(title, output, reprompt, should_end_session))

The implementation in this case:

def buildSpeechletResponse(title, output, reprompt_text, should_end_session):return {    'outputSpeech': {        'type': 'PlainText',        'text': output    },    'card': {        'type': 'Simple',        'title': "SessionSpeechlet - " + title,        'content': "SessionSpeechlet - " + output    },    'reprompt': {        'outputSpeech': {            'type': 'PlainText',            'text': reprompt_text        }    },    'shouldEndSession': should_end_session    }def buildResponse(session_attributes, speechlet_response):    return {        'version': '1.0',        'sessionAttributes': session_attributes,        'response': speechlet_response    }

This creates the sessionAttributes in the recommended way in the Lambda response JSON.

Also just adding a new sessionAttribute doesn't overwrite the last one if it doesn't exist. It will just create a new key-value pair.

Do note, that this may work well in the service simulator but may return a key attribute error when testing on an actual Amazon Echo. According to this post,

On Service Simulator, sessions starts with Session:{ ... Attributes:{}, ... }When sessions start on the Echo, Session does not have an Attributes key at all.

The way I worked around this was to just manually create it in the lambda handler whenever a new session is created:

 if event['session']['new']:    event['session']['attributes'] = {}    onSessionStarted( {'requestId': event['request']['requestId'] }, event['session'])if event['request']['type'] == 'IntentRequest':    return onIntent(event['request'], event['session'])


First, you have to define the session_attributes.

session_attributes = {}

Then instead of using

session_attributes = create_recipient_first_attribute(recipient_first)

You should use

session_attributes.update(create_recipient_first_attribute(recipient_first)).

The problem you are facing is because you are reassigning the session_attributes. Instead of this, you should just update the session_attributes.

So your final code will become:

session_attributes = {}if session.get('attributes', {}) and "recipient_first" not in session.get('attributes', {}):    recipient_first = intent['slots']['recipient_first']['value']    session_attributes.update(create_recipient_first_attribute(recipient_first))if session.get('attributes', {}) and "dollar_value" not in session.get('attributes', {}):    dollar_value = intent['slots']['dollar_value']['value']    session_attributes.update(create_dollar_value_attribute(dollar_value))


The ASK SDK for Python provides an attribute manager, to manage request/session/persistence level attributes in the skill. You can look at the color picker sample, to see how to use these attributes in skill development.