Input and output contexts

Contexts let you control conversation flows by letting you define specific states that a conversation must be in for an intent to match. Normally, Dialogflow matches an intent if its training phrases closely resemble the user utterance. However, when you apply contexts to an intent, Dialogflow will only consider that intent for matching if the context is active.

There are two types of context that let you activate and deactivate contexts and can control the flow of your conversation:

  • Input contexts: When applied to an intent, an input context tells Dialogflow to match the intent only if the user utterance is a close match and if the context is active.
  • Output contexts: When applied to an intent, an output context tells Dialogflow to activate a context if it's not already active or to maintain the context after the intent is matched.

Some use cases where contexts are helpful include:

  • Controlling order of intent matching.
  • Creating different outcomes for intents with the same training phrases.

For example, say you have two intents that have the training phrase "Show me a picture", but one shows pictures of dogs and the other shows pictures of cats. You want to control which intent is matched depending on the user's preference, so another intent asks the user whether they prefer dogs or cats and activates a "likes dogs" or "likes cats" output context. Each "Show me a picture" intent has the corresponding input context to ensure Dialogflow matches the expected intent based on whether the user likes dogs or cats.

Output contexts

When you apply an output context to an intent, Dialogflow activates that context if it's not already active, or renews that context if it's already active.

  • Multiple output contexts can be applied to an intent, allowing for finer intent matching control.
  • You can adjust the lifespan of an output context to set the number of conversation turns the context is active for.

Contexts can also be used when you want to pass information captured from the user to future responses. For example, if a user is asked whether they like cats or dogs more, the parameter value for that captured entity can be passed along in the output context.

Lifespan

By default, output contexts expire after either five requests or 20 minutes after its corresponding intent is matched. If the same output context is included in another intent, the context resets the counter and clock to five requests and 20 minutes. Contexts in follow-up intents have a default lifespan of two requests.

If the "likes cats" context has a lifespan of two and the context's lifespan isn't reset by its related output, the agent could only observe "likes cats" for two conversational turns, as shown in the following example:

  • "Do you like cats?" - Context is attached to this intent.
  • "Yes, I do." - Context is set and activated.
  • "Do you want to see a cat picture?" - Turn 1.
  • "Sure!" - Turn 1.
  • "Here's a cat picture." - Turn 2 and context is removed.

Add one or more output contexts

To add an output context to an intent, follow these steps:

  1. Click on Intents in the left menu.
  2. Click on an intent.
  3. Click in the Add output context text field under the Contexts section.
  4. Enter a name for the context and press Enter/Return.

  5. Optionally, click in the lifespan circle and change the lifespan of the context.

    Multiple output contexts can added to the intent prior to saving.

  6. Click SAVE.

Input contexts

When added to intents, input contexts increase the likelihood of that intent being matched when that context is active.

While intents are matched when a user replies with something similar to defined training phrases, contexts attached to a session can force an intent to be matched.

Intent matching and contexts

Intent matching follows three rules when it comes to contexts:

  1. Queries without any contexts can match any intent.
  2. Queries with any number of contexts can match any intents that don't have input contexts.
  3. When one or more contexts are active, an intent's input contexts must be a subset (one or more) of those active contexts to be matched.

As an example, if contexts "A", "B", and "C" are active, an intent with "A", "B", and "D" input contexts will not be matched because context "D" is not active.

Add an input context

To add an input context to an intent, follow these steps:

  1. Click on Intents in the left menu.
  2. Click on an intent.
  3. Click in the Add input context text field under the Contexts section.
  4. When you begin typing, a list of existing output contexts will be listed. Click on the context you want to match.

  5. Click SAVE.