Disclosure : Some of the links in this post are affiliate links so if you click on the link and purchase the item, I will receive a commission.

How to Add Entities to an EntityType in DialogFlow Using Python

Managing Entities within an EntityType is pretty easy, you can only delete, create, or update - all in batch. To start, you always need to get the parent EntityType or its name. You can grab this with:

entity_type_UUID = "ab3dasd2-4d23-4da4-b124-75337b48e35d"
entity_type_client = dialogflow.EntityTypesClient()
full_entity_type_path = entity_type_client.entity_type_path(dialogflow_project_id(), entity_type_UUID)
entity_type = entity_type_client.get_entity_type(full_entity_type_path)

Like most things with DialogFlow, in order to grab the EntityType, you need to get what the documentation refers to as the "name", which is really a UUID and is not clear how to get.

How to Get the DialogFlow EntityType Name in Python

As normal, the "name" is really a UUID and not the display name (which is shown as the "entity name" in the visual admin), it is the unique identifier. You can go about grabbing this several different ways.

When you are just testing out functionality, you can manually pull up the "Edit EntityType" page in the DialogFlow admin. The UUID is then the last part of the url:

Manually getting entity type names in dialogflow

This works just fine with a limited amount of intents, but it doesn't work if you need to get something dynamically, or a large number. It also doesn't work if you ever re-create the agent or the entity type, or want to reuse the code on another agent, since it would then change.

Getting the EntityType or Name Via the Display Name

It's much better long term (at least for my purposes) to get the entity type via the display name, which will also be the same. To do this you grab all the entity types in your agent and loop through them comparing the display names to the one you are looking for.

entity_type_client = dialogflow.EntityTypesClient()
parent = entity_type_client.project_agent_path(dialogflow_project_id())
entity_type_to_find_display_name = "[Display Name]" #I.E. "food_cut" in the example above
entity_types = entity_type_client.list_entity_types(parent)
entity_type_to_find = None
for entity_type in entity_types:
  if entity_type_to_find_display_name == entity_type.display_name:
    entity_type_to_find = entity_type

if entity_type_to_find:
    #Do Stuff

Once you have the entity type, you can grab the uuid name with:


Finally we can create and delete Entities!

Creating DialogFlow Entities in Python

To add an Entity to an EntityType, you need to use the Entity object, which is just the "value" (or "name") and an array of the synonyms.

  value = "Entity Name",
  synonyms = ["Synonm 1", "Synonm 2", "Synonm 3"]

You then pass that into the "batch_create_entities()" call.

entity_type_client = dialogflow.EntityTypesClient()
entity_to_add = dialogflow.types.EntityType.Entity(
    value = "Entity Name",
    synonyms = ["Entity Syn 1", "Entity Syn 2", "Entity Syn 3"]
response = entity_type_client.batch_create_entities(entity_type_UUID, [entity_to_add])
Example of entity added via python

This also works with multiple entities.

  entity_type_client = dialogflow.EntityTypesClient()
  response = entity_type_client.batch_create_entities(entity_type_UUID, [entity_to_add1, entity_to_add2, entity_to_add3])

Updating Entities

Updating entites works the exact same way, just pass it into the "batch_update_entities" funtction instead.

entity_type_client = dialogflow.EntityTypesClient()
entity_to_update = dialogflow.types.EntityType.Entity(
    value = "Entity Name",
    synonyms = ["Entity Syn 1", "Entity Syn 2", "Entity Syn 3"]
response = entity_type_client.batch_update_entities(entity_type_UUID, [entity_to_update])

What Happens if the Entity Exists

Strangly, if you call "batch_create_entities()" and the entity already exists it will just update it. And if you call "batch_update_entities()" and the entity doesn't exist, it just creates it. I honestly have no idea what the difference between the two is.

Entity Updates Are Not Destructive

As of April 2nd, 2020, updating the entities does not do a full update, it just adds to them. So you can't remove entities using that method. This really comes into play for the synonyms.

I.E. if dialogflow has:

EntityToUpdate: ["Syn1"]

and I pass in:

EntityToUpdate: ["Syn2", "Syn3", "Syn4"]

It currently results in:

EntityToUpdate: ["Syn1", "Syn2", "Syn3", "Syn4"]

So if you only want it to have the new synonyms, you need to delete and re-create it.

Deleting Entities in DiagflowFlow with Python

One we have the entity name, it is relatively easy to delete the entities. You just need to know the name of the entity (and for once it is just the actual name, not a UUID) and pass it into the delete_entites() function. All entities work is done in bulk, so it's always an array.

entity_type_client = dialogflow.EntityTypesClient()
entity_to_delete = "Entity Name" #In the Above Screenshot, "Zucchini" or "Pea Pods" would be the name
response = entity_type_client.batch_delete_entities(entity_type_UUID, [entity_to_delete])

And for multiples ones just pass them all in.

entity_type_client = dialogflow.EntityTypesClient()
entity_to_delete1 = "Entity Name 1"
entity_to_delete2 = "Entity Name 2"
entity_to_delete3 = "Entity Name 3"
response = entity_type_client.batch_delete_entities(entity_type_UUID, [entity_to_delete1, entity_to_delete2, entity_to_delete3])

And if you want to just delete all entities in an EntityType you can do the following:

entity_type_client = dialogflow.EntityTypesClient()
full_entity_type_path = entity_type_client.entity_type_path(dialogflow_project_id(), entity_type_UUID)
entity_type = entity_type_client.get_entity_type(full_entity_type_path)
entities = entity_type.entities
entity_values = [entity.value for entity in entities]
response = entity_type_client.batch_delete_entities(entity_type_UUID, entity_values)

Other How to Build a Chatbot Articles

placeholder image

Cookie Consent

This website uses cookies or similar technologies, to enhance your browsing experience and provide personalized recommendations. By continuing to use our website, you agree to our Privacy Policy