Search code examples
dialogflow-cx

Dialogflow CX - Location settings have to be initialized - FAILED_PRECONDITION


I am automating Dialogflow CX using Python client libraries. That includes agent/intent/entity etc. creation/updation/deletion. But for the first time run, I am encountering the below error from python.

If I login to console and set the location from there and rerun the code, it is working fine. I am able to create agent.

Followed this URL of GCP -

https://cloud.google.com/dialogflow/cx/docs/concept/region

I am looking for code to automate the region & location setting before running the python code. Kindly provide me with the code.

Below is the code I am using to create agent.

Error -

google.api_core.exceptions.FailedPrecondition: 400 com.google.apps.framework.request.FailedPreconditionException: Location settings have to be initialized before creating the agent in location: us-east1. Code: FAILED_PRECONDITION

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.FAILED_PRECONDITION
        details = "com.google.apps.framework.request.FailedPreconditionException: Location settings have to be initialized before creating the agent in location: us-east1. Code: FAILED_PRECONDITION"
        debug_error_string = "{"created":"@1622183899.891000000","description":"Error received from peer ipv4:142.250.195.170:443","file":"src/core/lib/surface/call.cc","file_line":1068,"grpc_message":"com.google.apps.framework.request.FailedPreconditionException: Location settings have to be initialized before creating the agent in location: us-east1. Code: FAILED_PRECONDITION","grpc_status":9}"


main.py -

# Import Libraries
import google.auth
import google.auth.transport.requests
from google.cloud import dialogflowcx as df
from google.protobuf.field_mask_pb2 import FieldMask
import os, time
import pandas as pd

# Function - Authentication
def gcp_auth():
cred, project = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
auth_req = google.auth.transport.requests.Request()
cred.refresh(auth_req)

# Function - Create Agent
def create_agent(agent_name, agent_description, language_code, location_id, location_path):
    if location_id == "global":
        agentsClient = df.AgentsClient()
    else:
        agentsClient = df.AgentsClient(client_options={"api_endpoint": f"{location_id}-dialogflow.googleapis.com:443"})
    agent = df.Agent(display_name=agent_name, description=agent_description, default_language_code=language_code, time_zone=time_zone, enable_stackdriver_logging=True)
    createAgentRequest = df.CreateAgentRequest(agent=agent, parent=location_path)
    agent = agentsClient.create_agent(request=createAgentRequest)
    return agent```


Solution

  • Currently, Dialogflow does not support configuring the location settings through the API, thus you can not initialise location settings through it. You can only set the location through the Console.

    As an alternative, since the location setting has to be initialised only once for each region per project you could set the location and automate the agent creation process, some useful links: 1 and 2.

    On the other hand, if you would find this feature useful, you can file a Feature Request, here. It will be evaluated by the Google's product team.