Manage contexts

In most cases, you configure contexts at design-time (when you are building an agent). For example, when you have a training phrase like: "I would like to add pizza to my shopping cart."

In some advanced scenarios, you may also want to write code that gets and sets some contexts at runtime (during a live conversation). An example could be, you detected a device location and you have added it to the context, so you can refer to the location at a later moment.

This document describes how to get and set contexts at design-time and runtime by using the Dialogflow Console, the API, or fulfillment.

Context naming

The following rules apply to naming contexts:

Rule Examples
Use alphanumeric names. mycontext1
Use - or _ instead of spaces. my-context-1
Names are not case sensitive. Abc123 and abc123 are considered equivalent.
All context names are lowercase when using the API. abc123

Set contexts at design-time

Console

  1. Go to the Dialogflow ES console.
  2. Select an agent.
  3. Select Intents in the left sidebar menu.
  4. Expand the Contexts section of the intent data.
  5. Click the Add output context or Add input context field in intent data.
  6. Enter a name for the context and press Return.
  7. For output contexts, optionally click the lifespan circle and change the lifespan. Lifespan UI snapshot
  8. Add more contexts as needed.
  9. Click Save.

API

See the Intents reference.

Set contexts at runtime

Fulfillment

When your webhook service sends a WebhookResponse, set the outputContexts field to desired active contexts.

API

The request for a Sessions type detectIntent call contains a queryParameters.contexts field, which is used to set active contexts.

The following is a REST JSON example of a detectIntent request:

POST https://dialogflow.googleapis.com/v2/{session=projects/*/agent/sessions/*}:detectIntent

{
  "queryInput": {
    "text": {
      "languageCode": "en-US",
      "text": "I would like to add pizza to my shopping cart."
    }
  },
  "queryParams": {
    "contexts": [{
        "name": "projects/project-id/agent/sessions/session-id/contexts/product-chosen",
        "lifespanCount": 5,
        "parameters": {
          "product": "Pizza",
          "device-location" "@52.3377871,4.8698096,17z"
        }
      }]
  }
}

At any time, you can also call the create, delete, and update methods on the Contexts type.

Get contexts at runtime

Fulfillment

When your webhook service receives a WebhookRequest, the queryResult.outputContexts field contains the active contexts.

API

The response to a Sessions type detectIntent call contains a queryResult.outputContexts field, which provides the list of active contexts.

The following is a REST JSON example of a detectIntent response:

{
  "responseId": "response-id",
  "queryResult": {
    "queryText": "I would like to add pizza to my shopping cart.",
    "parameters": {
      "product": "pizza"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "The product has been added.",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "The product has been added."
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/project-id/agent/sessions/session-id/contexts/product-chosen",
        "lifespanCount": 5,
        "parameters": {
          "product": "Pizza",
          "device-location" "@52.3377871,4.8698096,17z"
        }
      }
    ],
    "intent": {
      "name": "projects/project-id/agent/intents/intent-id",
      "displayName": "buy-product"
    },
    "intentDetectionConfidence": 0.8057143,
    "languageCode": "en",
  }
}

At any time, you can also call the get and list methods on the Contexts type.