Image embeddings

This guide shows how you can create image embeddings using the Marengo 3.5 video understanding model. For complete specifications and input requirements, see the Marengo 3.5 page.

The Marengo video understanding model generates embeddings for all modalities in the same latent space. This shared space enables any-to-any searches across different types of content.

For details on how your usage is measured and billed, see the Pricing page.

Key concepts

This section explains the key concepts and terminology used in this guide:

  • Asset: Your uploaded content. Once created, you can reference the same asset across multiple operations without uploading the file again.
  • Embedding: Vector representation of your content.
  • Embedding task: An asynchronous operation for processing your content and creating embeddings. Contains a status and the resulting embeddings when complete.

Workflow

This guide shows how to create one embedding for an image file, so your queries can match its content. The example uploads the image as an asset. You can also pass a URL or base64-encoded data inline instead of creating an asset; both are shown as commented-out lines in the code examples.

The platform processes your files asynchronously, one file per request. This example embeds one image; repeat the request for each file in your collection.

For an image, the type of embedding, the output format, and the scope fields each accept a single value. You can request a per-dimension uncertainty vector.

To combine an image with text or with other media in a single embedding, see the Embed a query guide.

Use these embeddings for similarity search, content classification, clustering, recommendations, or Retrieval-Augmented Generation (RAG).

Retention policy

Embeddings created with the asynchronous method are stored for seven days. After this, you must recreate them to obtain the results again.

Prerequisites

  • To use the platform, you need an API key:

    1

    If you don’t have an account, sign up for a free account.

    2

    Go to the API Keys page.

    3

    If you need to create a new key, select the Create API Key button. Enter a name and set the expiration period. The default is 12 months.

    4

    Select the Copy icon next to your key to copy it to your clipboard.

  • Depending on the programming language you are using, install the TwelveLabs SDK by entering one of the following commands:

    $pip install --upgrade twelvelabs
  • Your image files must meet the following requirements:

    • Upload limits: Image files up to 32 MB.

    • Model capabilities: See the complete input requirements for Marengo 3.5.

Complete example

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

1import time
2from twelvelabs import TwelveLabs, AsyncImageInputRequest, MediaSource
3
4# 1. Initialize the client
5client = TwelveLabs(api_key="<YOUR_API_KEY>")
6
7# 2. Upload an image
8asset = client.assets.create(
9 method="url",
10 url="<YOUR_IMAGE_URL>" # Use direct links to raw media files. Video hosting platforms and cloud storage sharing links are not supported
11 # Or use method="direct" and file=open("<PATH_TO_IMAGE_FILE>", "rb") to upload a local file up to 32 MB
12)
13print(f"Created asset: id={asset.id}")
14
15# 3. Check the status of the asset
16print("Waiting for asset to be ready...")
17while True:
18 asset = client.assets.retrieve(asset.id)
19 if asset.status == "ready":
20 print("Asset is ready")
21 break
22 if asset.status == "failed":
23 raise RuntimeError(f"Asset processing failed: id={asset.id}")
24 time.sleep(5)
25
26# 4. Create an embedding task
27task = client.embed.v_2.tasks.create(
28 input_type="image",
29 model_name="marengo3.5",
30 image=AsyncImageInputRequest(
31 media_source=MediaSource(
32 asset_id=asset.id,
33 # url="<YOUR_IMAGE_URL>", # Use direct links to raw media files. Video hosting platforms and cloud storage sharing links are not supported
34 # base_64_string="<BASE_64_ENCODED_DATA>",
35 ),
36 ),
37 # embedding_uncertainty=True,
38)
39print(f"Task ID: {task.id}")
40
41# 5. Monitor the status
42while True:
43 task = client.embed.v_2.tasks.retrieve(task_id=task.id)
44 if task.status == "ready":
45 print("Task completed")
46 break
47 elif task.status == "failed":
48 print("Task failed")
49 break
50 else:
51 print("Task still processing...")
52 time.sleep(5)
53
54# 6. Process the results
55print(f"Number of embeddings: {len(task.data)}")
56for embedding_data in task.data:
57 print(f"Embedding dimensions: {len(embedding_data.embedding)}")
58 print(f"First 10 values: {embedding_data.embedding[:10]}")

Code explanation

1

Import the SDK and initialize the client

Create a client instance to interact with the TwelveLabs Video Understanding Platform.
Function call: You call the constructor of the TwelveLabs class.
Parameters:

  • api_key: The API key to authenticate your requests to the platform.

Return value: An object of type TwelveLabs configured for making API calls.

2

Upload an image

Upload an image file to create an asset.
Function call: You call the assets.create function.
Parameters:

  • method: The upload method for your asset. Use url for a publicly accessible or direct to upload a local file. This example uses url.
  • url or file: The publicly accessible URL of your image file or an opened file object in binary read mode. This example uses url.

Return value: An object of type Asset. This object contains, among other information, a field named id representing the unique identifier of your asset.

3

Check the status of the asset

Asset processing is asynchronous. Poll the status of the asset until it is ready before you use it.
Function call: You call the assets.retrieve function.
Parameters:

  • asset_id: The unique identifier of your asset.

Return value: An object of type Asset containing, among other information, a field named status representing the current status of the asset. Check this field until its value is ready.

4

Create an embedding task

Create an embedding task to start processing your image.
Function call: You call the embed.v_2.tasks.create function.
Parameters:

  • input_type: The type of content. Set this parameter to image.
  • model_name: The embedding model to use. This example uses marengo3.5.
  • (Optional) embedding_uncertainty: Set this parameter to true to receive a data[].embedding_uncertainty field in the response. This field is a per-dimension uncertainty vector with the same length as the embedding array. A higher value shows lower confidence in that dimension.
  • image: An object containing the following properties:
    • media_source: An object specifying the source of the image file. Specify one of the following:
      • asset_id: The unique identifier of an asset from a previous upload.

      • url: The publicly accessible URL of the image file.

      • base_64_string: The base64-encoded image data.

        This example uses the identifier of the asset created in the previous step.

Return value: An object of type TasksCreateResponse containing, among other information, a field named id, which represents the unique identifier of your embedding task. You can use this identifier to track the status of your embedding task.

5

Monitor the status

The platform requires some time to process images. Poll the status of the embedding task until it is ready. This example uses a loop to check the status every 5 seconds.
Function call: You repeatedly call the embed.v_2.tasks.retrieve function until the task completes.

Parameters:

  • task_id: The unique identifier of your embedding task.

Return value: An object of type EmbeddingTaskResponse containing, among other information, the following fields:

  • status: The current status of the task. The possible values are:
    • processing: The platform is creating the embeddings.
    • ready: Processing is complete. Embeddings are available in the data field.
    • failed: The task failed.
  • data: When the status is ready, this field contains a list with one embedding object. The embedding object includes:
    • embedding: The embedding vector (a list of floats).
    • embedding_option: The type of embedding. For an image, this field is visual.
    • embedding_scope: The scope of the embedding. For an image, this field is asset.
6

Process the results

This example prints the dimensions and the first 10 vector values of the embedding.