Input and output contexts

Input and output contexts are applied to intents. They work together to control conversation flow:

  • Output contexts control active contexts. When an intent is matched, any configured output contexts for that intent become active.
  • Input contexts control intent matching. While contexts are active, Dialogflow is more likely to match intents that are configured with input contexts that are a subset of currently active contexts.

With contexts, you can:

  • Control the order of intent matching.
  • Create context-specific intents with the same training phrases.

For example, consider the following intents:

Intent name Training phrases Input contexts Output contexts
pet-choose "dogs", "cats" - $pet
dog-show "Show me a picture" dogs -
cat-show "Show me a picture" cats -

The pet-choose intent asks the end-user to select dogs or cats. Once selected, it sets the selection to a $pets parameter and uses the parameter as the output context. The dog-show and cat-show intents show a picture of dogs or cats, depending on the active context.

Output contexts

Multiple output contexts can be applied to an intent. When an intent is matched, any output contexts applied to the intent become active.

Parameter references for active contexts

When an intent with output context is matched, its collected parameter values can serve as temporary storage with parameter references for active contexts. These values can be accessed during future intent matching, for as long as the context remains active.

For example, consider the following variation of the pet example above:

Intent name Training phrases Parameters Input contexts Output contexts Response
pet-choose "dogs", "cats" $pet - pet-chosen "You chose $pet"
pet-show "Show me a picture" - pet-chosen - "Here is a picture of a #pet-chosen.pet"

The pet-choose intent sets the $pet parameter to the value chosen by the end-user, and it activates the pet-chosen context. When the pet-show intent is matched, the intent response can access the $pet parameter via the active context.

Lifespan

Each active context has a lifespan that defines the number of conversational turns for which the context remains active. The default lifespan is five conversational turns for normal intents and two conversational turns for follow-up intents. You can override the default lifespan for any contexts. In addition, all contexts expire after 20 minutes of becoming active. When an intent is matched with an output context that is already active, the lifespan and expiration timer are reset.

For example, consider the following dialog with a pet information agent. The lifespan for the cats context is two turns.

Turn Dialog Explanation
1 Agent: What kind of pet do you want to know about? The pet-init intent is matched.
2 User: I like cats.
Agent: What do you want to know about cats?
The pet-select intent has a cats output context, which becomes active.
3 User: What do they look like?
Agent: Here's a cat picture. What else do you want to know?
The cat-image intent has a cats input context, so it is matched. The cats context has been active for 1 turn.
4 User: What do they sound like?
Agent: Here's a cat sound. What else do you want to know?
The cat-audio intent has a cats input context, so it is matched. The cats context has been active for 2 turns.
5 User: How big are they?
Agent: What kind of pet do you want to know about?
The pet-init intent is matched again, because the cats context is no longer active.

Input contexts

While contexts are active, Dialogflow is more likely to match intents that are configured with input contexts that correspond to the currently active contexts. The detailed intent matching rules are as follows:

  1. An intent's input context list must be a subset of the currently active context list in order to be matched. This also applies to intents with no input contexts.
  2. For intents that satisfy rule #1, those with an input context list that most closely match the currently active context list are more likely to be matched.

For example:

  • Intents with no input contexts can be matched at any time.
  • When no contexts are active, only intents with no input contexts can be matched.
  • If contexts "A" and "B" are active, an intent with "A" and "C" input contexts cannot be matched.
  • If contexts "A", "B", and "C" are active, an intent with "A" and "B" input contexts can be matched.
  • If contexts "A" and "B" are active, an intent with "A" and "B" input contexts is more likely to be matched than an intent with no input context or an intent with just "A" for an input context.