'Dialogflow API - TypeError: Invalid constructor input for BatchUpdateIntentsRequest: intents
I am trying to train Dialogflow using its API to pass some of the limitations of training with the UI when you have a preexisting training dataset. The upload dataset feature seems to be quite limited.
With the minimum setup, trying to make create intents from the API, I am getting the following error:
Traceback (most recent call last):
File "main.py", line 102, in <module>
update_agent()
File "main.py", line 87, in update_agent
response = client.batch_update_intents(
File "env/lib/python3.8/site-packages/google/cloud/dialogflow_v2/services/intents/client.py", line 1114, in batch_update_intents
request = intent.BatchUpdateIntentsRequest(request)
File "env/lib/python3.8/site-packages/proto/message.py", line 520, in __init__
raise TypeError(
TypeError: Invalid constructor input for BatchUpdateIntentsRequest: intents {
name: "projects/project-name/agent/intents/welcome"
display_name: "Welcome"
training_phrases {
parts {
text: "Training phrase 1"
}
}
messages {
text {
text: "B"
text: "o"
text: "t"
text: " "
text: "R"
text: "e"
text: "s"
text: "p"
text: "o"
text: "n"
text: "s"
text: "e"
}
}
}
This is the python code I used:emphasized text
def update_agent():
intentBatch = dialogflow.types.intent.IntentBatch()
training_phrases = [
dialogflow.types.intent.Intent.TrainingPhrase(
parts=[
dialogflow.types.intent.Intent.TrainingPhrase.Part(
text="Training phrase 1"
)
]
)
]
intent = dialogflow.types.intent.Intent(
name="projects/{}/agent/intents/{}".format(PROJECT_ID, "welcome"),
display_name="Welcome",
webhook_state="WEBHOOK_STATE_ENABLED",
training_phrases=training_phrases,
messages=[
dialogflow.types.intent.Intent.Message(
text=dialogflow.types.intent.Intent.Message.Text(text="Bot Response"))
]
)
intentBatch.intents.append(intent)
client = dialogflow.IntentsClient()
response = client.batch_update_intents(
request=intentBatch
)
print("Intent updated")
print(response)
I followed this documentation to check the expected schema, I left out some of the optional fields and included all the required fields, but it doesn't seem to solve the problem.
Also, is the "messages" from the API response being split into a dictionary of its characters expected?
Solution 1:[1]
You should nest intentBatch in BatchUpdateIntentsRequest object. Then the new BatchUpdateIntentsRequest object could now be passed to batch_update_intents(). Also, intent is sequence type so it should be inside [] , this is the same case with messages that is why it was split into a dictionary of characters.
See modified code below:
from google.cloud import dialogflow_v2 as dialogflow
def update_agent():
intentBatch = dialogflow.types.IntentBatch()
training_phrases = [
dialogflow.types.intent.Intent.TrainingPhrase(
parts=[
dialogflow.types.intent.Intent.TrainingPhrase.Part(
text="Training phrase 1"
)
]
)
]
intent = [dialogflow.types.intent.Intent(
name="projects/<your-project>/agent/intents/<>",
display_name="welcome",
webhook_state="WEBHOOK_STATE_ENABLED",
training_phrases=training_phrases,
messages=[
dialogflow.types.intent.Intent.Message(
text=dialogflow.types.intent.Intent.Message.Text(text=["Bot Response"]))
]
)]
#intentBatch.intents.append(intent)
intentBatch = dialogflow.types.intent.IntentBatch(intents=intent)
client = dialogflow.IntentsClient()
response = client.batch_update_intents(
request=dialogflow.types.BatchUpdateIntentsRequest(intent_batch_inline=intentBatch, parent="projects/<your-project>/agent")
)
print("Intent updated")
print(response)
update_agent()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anjela B |


