Add assets to a knowledge store

This guide shows you how to add an uploaded asset to a knowledge store and wait for indexing to complete.

Key concepts

  • Asset: Your uploaded content. Once created, you can reference the same asset across multiple operations without uploading the file again.
  • Knowledge store: A persistent store of your videos and images plus the understanding the platform derives from them — spatiotemporal context, a typed ontology, and embeddings — that together enable corpus-level reasoning.
  • Knowledge store item: An asset added to a knowledge store. The platform processes each item asynchronously. When processing finishes, the item is ready for downstream tasks.

Prerequisites

  • You’ve already uploaded your content, and the asset has reached the ready status. See the Upload content page for details.
  • You’ve already created a knowledge store. See the Create a knowledge store page for details.

Example

Copy and paste the code below, replacing the placeholders surrounded by <> with your values.

1from twelvelabs import TwelveLabs
2import time
3
4client = TwelveLabs(api_key="<YOUR_API_KEY>")
5STORE_ID = "<YOUR_KNOWLEDGE_STORE_ID>"
6
7# Step 1: Add the asset to the knowledge store
8item = client.knowledge_store_items.create(
9 knowledge_store_id=STORE_ID,
10 asset_id="<YOUR_ASSET_ID>",
11 # asset_type="image", # Uncomment to add an image (the default is video)
12)
13print(f"Item added: {item.id}")
14
15# Step 2: Check the status of the knowledge store item
16while True:
17 status = client.knowledge_store_items.retrieve(
18 knowledge_store_id=STORE_ID, item_id=item.id
19 ).status
20 if status == "ready":
21 break
22 elif status == "failed":
23 raise Exception("Indexing failed")
24 print(f"Status: {status}, waiting...")
25 time.sleep(10)
26print("Indexing complete")

Code explanation

1

Add the asset to the knowledge store

Add an uploaded asset to a knowledge store as an item. The platform begins indexing the item asynchronously. This example prints the item identifier to the standard output.
Function call: You call the knowledge_store_items.create method.
Parameters:

  • knowledge_store_id: The unique identifier of the knowledge store.
  • asset_id: The unique identifier of the asset to add.
  • (Optional) asset_type: The type of asset. Defaults to video; set it to image to add an image asset. The file_type field of the asset records the type the platform detected during upload.

Return value: An object of type KnowledgeStoreItem with a field named id representing the unique identifier of the newly created knowledge store item, and a field named status representing its current state.

2

Check the status of the knowledge store item

Poll the item until it reaches the ready status. Indexing usually takes longer than the asset processing that happens after upload. One asset can exist in multiple knowledge stores. For the full list of statuses, see the Knowledge stores page.
Function call: You call the knowledge_store_items.retrieve method.
Parameters:

  • knowledge_store_id: The unique identifier of the knowledge store.
  • item_id: The unique identifier of the knowledge store item, from the previous step.

Return value: An object of type KnowledgeStoreItem. Check the status field for the current state.

Next steps

Jupyter notebook

Open In Colab