Best Practices

This topic includes recommendations on how to work with the Dialogflow V2 API. These guidelines are designed for greater efficiency and accuracy as well as reasonable response times from the Dialogflow V2 API.

Session client scope

You can improve the performance of your Dialogflow V2 API Client Library code by creating a session client that scopes to multiple requests.

Requests to the Dialogflow V2 API must include a token that grants access to the Dialogflow V2 API and identifies your Google Cloud Platform project and account. When you create a session client object using the Client Libraries, the Client Libraries obtain an access token based on credentials that you provide, or your application default credentials. The additional processing to obtain the access token can slow down your request to the Dialogflow V2 API by one or two seconds.

To improve performance, you can use a single instance of a session client object for multiple requests. The session client reuses the same access token for as long as it is valid (typically one hour). Once it expires, the session client refreshes the access token automatically, so you don't need to recreate the session client to refresh your access token. Your request that also refreshes the access token can take an extra second or two.

While this approach will perform better, your code will be less portable in that you cannot include the code to create the session client in a single method call, that could be easily copied to another project.

For example, the following Node.js Client Library code will perform better with the Dialogflow V2 API as the same session client is used for multiple calls to the detectTextIntent method:

// Import the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiate a session client
const sessionClient = new dialogflow.SessionsClient();

function detectTextIntent(projectId, sessionId, queries, languageCode) {
  if (!queries || !queries.length) {
    return;
  }

  // The path to identify the agent that owns the created intent.
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  ...

Alternatively, the following Node.js Client Library code would be considered more portable code as it includes the creation of the session client in the detectTextIntent method:

function detectTextIntent(projectId, sessionId, queries, languageCode) {
  // Import the Dialogflow library
  const dialogflow = require('dialogflow');

  // Instantiate a session client
  const sessionClient = new dialogflow.SessionsClient();

  if (!queries || !queries.length) {
    return;
  }

  // The path to identify the agent that owns the created intent.
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  ...

For more information on accessing the Dialogflow V2 API using the Client Libraries, see Client Libraries.

Was this page helpful? Let us know how we did:

Send feedback about...

Dialogflow Enterprise Edition Documentation