Handling Action’s No Match errors in Dialogflow: Three strikes and you’re out

Neto Marin
Google Developers
Published in
5 min readOct 30, 2018

Posted by Kyo Lee, Technical Writer

People are predictably unpredictable and unpredictably predictable. Even when we ask a question and expect to hear “yes” or “no” as an answer, there will always be those whose answers translate to some form of “maybe.”

Unlike graphical interfaces, which present users with limited choices and well-defined actions (think of a web page with “Buy” or “Cancel” buttons), conversational interfaces are always confronted with near-infinite possibilities of what the next user input may be. You’ll quickly learn that, when building a conversational interface, a big chunk of work goes into making your interface capable of handling unexpected remarks from users.

Source: Pixabay

In that regard, one of the common questions that developers often ask is,

“How should my Action respond when users say unexpected things?”

Think baseball. Follow the “three-strikes rule — in baseball, a batter is given three chances to hit a pitched ball that passes through the strike zone. Similarly, we’ll allow our conversational interface to give users three chances to formulate the utterance in a way that the interface can understand. Let’s take a look:

User: I’d like to get tickets for the event next weekend.
Agent: Sure, how many people are going?
User: Me and my buddies.
Agent: Sorry, how many was that? (Strike 1)
User: I guess 3. No. 4, including me. Then, Mike might want to join…
Agent: Sorry, I didn’t get that. How many tickets would you like to purchase? (Strike 2)
User: Hmm… It depends on what Mike’s doing this Friday.
Agent: This seems to be beyond my expertise. Let’s stop here. There are currently 17 seats available. Bye for now. (Strike 3)

Notice that, instead of repeating the same prompt at each turn, the interface adjusts the response according to the following guidelines:

  • Strike 1: Rephrase the question in a condensed form and ask again.
  • Strike 2: Clarify the original question with added details to help the user formulate the answer. Consider including examples of acceptable utterances.
  • Strike 3: Terminate the conversation. Repeating yet another question will likely increase the user’s frustration, so it’s best to end the conversation after three attempts.

Next, we’ll go over how to create a conversational interface that follows these guidelines.

How to build it in Dialogflow

To implement the behavior above, we need a conversational interface to do the following:

  • Count the number of error occurrences (strikes) in conversation.
  • Adjust responses based on how many strikes the user currently has.

To perform these tasks, we’re going to need the help of a Dialogflow concept called contexts. In Dialogflow, a context, which includes a JSON object, enables a conversational interface to do the following:

  • Track the state of the conversation.
  • Carry information from one intent to another.

Using contexts, we can set up our default fallback intent to track the number of error occurrences and adjust responses based on the error count. However, to handle the complexity of the given tasks, we’ll need to deploy fulfillment, which uses Actions on Google Node.js client library.

Fulfillment gives your conversational interface the power and flexibility of having a backend server. Implemented as a web service endpoint, fulfillment can process complex logic, talk to databases and other web services, compose dynamic responses, and more.

Now, let’s implement our fallback behavior:

  1. In the Dialogflow console, create a new agent.
  2. Go to the Intents page.
  3. Click on Default Fallback Intent.
  4. Scroll down to the Fulfillment section at the bottom of the intent page.
  5. Toggle on the Enable webhook call for this intent button.

6. Click SAVE.
7. Click the Fulfillment tab on the navigation bar to go to the fulfillment page.
8. Toggle the inline editor’s button to ENABLED.

9. Copy and paste the code below to the index.js tab of the inline editor:

10. Click DEPLOY.
11. Test the fallback behavior using the simulator in the Dialogflow console.

Note: The simulator in the Dialogflow console does not simulate the termination of conversation. However, in the Actions on Google console, you should see the effect of conv.close(), which ends the conversation.

Notice the following in the code:

  • Actions on Google client library automatically creates a context, which stores conv.data as a parameter object.
  • When the first No Match error is detected, the variable fallbackCount is created and is added to conv.data.
  • With fallbackCount’s initial value set to 0 (it is then incremented to 1 immediately), this variable tracks the error count in the conversation.
  • If the function sees that fallbackCount is 3, the function delivers the final response to the user and terminates the conversation.
  • The dummy variable ticketCount has a fixed value of 17. However, in a real application, fulfillment needs to obtain the actual ticket count from the ticketing service via API and assign the value to ticketCount.
  • While it’s not shown in the code above, if the user says something that the interface understands within the three turns, other intents should reset the value of conv.data.fallbackCount to 0.

With this fulfillment in place, your conversational interface has now gained the ability to gracefully navigate challenging situations in conversation. Be sure to tweak, adjust, and refine the fallback behavior in a way that fits your application.

Note: When you create an Action using templates, your Action obtains this fallback behavior automatically.

What we have learned

Ideally, we want a conversational interface to always succeed in helping users complete their tasks. However, what we need to understand from the “three-strikes rule is that a conversational interface must approach challenges in a pragmatic way. Realistically, your conversational interface cannot resolve all the issues that may arise during conversations with users. If so, then the best approach is to allow the conversational interface to determine when to terminate the conversation, which, in turn, creates a better experience for users.

To learn more about No Match errors and Dialogflow’s features, contexts and fulfillment, visit these pages:

Google conversation design guide: Errors

Dialogflow documentation: Contexts

Dialogflow documentation: Fulfillment

If you have questions or comments, talk to us in our G+ community or reach out to us on Twitter @ActionsOnGoogle — don’t forget to use the hashtag #AoGDevs!

--

--

Neto Marin
Google Developers

Actions on Google DevRel, aiming to help developers to create awesome Actions for Google Assistant. But first of all a passionate and dedicated dad and husband!