Dustin Coates in Alexaskillskit

A Look at Dynamic Entities for the Alexa Skills Kit

For a long time, developers working on Alexa skills have requested the ability to update slot values dynamically. Amazon has announced that today, with dynamic entity values at runtime. The dynamic values let the experience adjust to the user (personalization) or the context, and they come with some constraints as well.

Realize that this is not a way to update the slot values for all users across all invocations. This is instead temporary. Once you see that, you understand the next part, which is that you provide or remove values at runtime through a Dialog.UpdateDynamicEntities directive.

const MinorLeagueHandler = {
  canHandle () {
    // Do your thing
  },
  handle (handlerInput) {
    const dynamicEntities = {
      type: "Dialog.UpdateDynamicEntities",
      updateBehavior: "REPLACE",
      types: [
        {
          name: "Team",
          values: [
            {
              id: "round-rock",
              name: {
                value: "Round Rock Express",
                synonyms: ["Round Rock", "Express"]
              }
            },
            {
              id: "corpus-christi",
              name: {
                value: "Corpus Christi Hooks",
                synonyms: ["Corpus Christi", "Hooks", "Corpus"]
              }
            },
          ]
        }
      ]
    };

    // Speech and response strings

    return handlerInput.responseBuilder
      .speak(speech)
      .reprompt(noResponse)
      .addDirective(dynamicEntities)
      .getResponse();
  }
};

In this code, we’re replacing all of the dynamic entity values with the two for the Round Rock Express and Corpus Christi Hooks. You can only replace or clear the dynamic values. Clearing will happen most often at the end of a session, although the dynamic values will also auto-clean after thirty minutes.

You can see that, because you have to proactively send the directive to replace the dynamic values, this can only happen after the user has interacted with the skill. Another constraint is you can have at most 100 dynamic values. Still, dynamic values are useful because you can use them to bias the slot matching based on user and contextual data.

The reason why this works is due to the order that Amazon will focus on the values:

  • Dynamic entities (custom slots)
  • Dynamic entities (extended built-in slots)
  • Static entities (custom slots)
  • Static entities (extended built-in slots)

That’s the part about sending, but then there’s the inbound values. I’ll get to that but first…

I’m about to say something that may not seem significant to you, but is the biggest surprise to me in all of this. Ready? The resolutionsPerAuthority array is no longer guaranteed to have only a single item in it.

I know, right?

This means you now need to check for all of the values in resolutionsPerAuthority and also check whether they’re dynamic or or static.

The inbound resolutions will now be from either amzn1.er-authority.echo-sdk.amzn1.ask.skill.<SKILL ID>.<SLOT TYPE> or amzn1.er-authority.echo-sdk.dynamic.amzn1.ask.skill.<SKILL ID>.<SLOT TYPE>. A small difference there, but one specifies that it’s dynamic.

Want some code to handle it? I know you do.

function separateResolutions (resolutions) {
  const dynamicMatcher = /echo-sdk\.dynamic\.amzn1/;
  const reducer = function (acc, curr) {
    if (curr.authority.match(dynamicMatcher)) {
      acc.dynamic = curr.values;
    } else {
      acc.static = curr.values;
    }

    return acc;
  };

  return resolutions.reduce(reducer, {dynamic: [], static: []});
}

We’ll end with ideas of when you might use dynamic values. Maybe users have their own values unique to them (say, members of their household). Maybe a quiz is going to expect different answers based on the question. Perhaps even the values change every day, such as which movies are available in theaters. Finally, perhaps your slot values are variable because you’ve got a regularly updating catalogue. Use dynamic values paired with a strong voice search solution (cough, cough) to match the users’ requests.