Embed a query

This guide shows how you can create an embedding for a query 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.
  • Media source: One image, video, or audio input that the platform combines into your embedding.

Workflow

This guide shows how to combine text and media sources into a single embedding for retrieving matching content. This example combines text with an image and 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. To upload other types of content, see Upload and processing methods.

The platform processes your request synchronously and returns the embedding in the response. Provide text, media sources, or both, and combine up to 10 media sources in one request.

Customize your embeddings

You can name a media source and reference it from your text, truncate text that exceeds the 2,000-token limit, and request a per-dimension uncertainty vector.

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

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 media files must meet the following requirements:

    • Size and duration: Each media source can be up to 32 MB, and video and audio up to 30 seconds. This limit applies whether you provide a URL, base64-encoded data, or an asset identifier. For longer or larger files, see the Embed content at scale page.
    • 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, MultiInputRequest, MultiInputMediaSource
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 for your query
27response = client.embed.v_2.create(
28 input_type="multi_input",
29 model_name="marengo3.5",
30 multi_input=MultiInputRequest(
31 input_text="<YOUR_TEXT>",
32 # To reference a media source from your text, name the source below and use <@name>:
33 # input_text="A person wearing <@outfit>",
34 # Omit media_sources to create an embedding from your text alone:
35 media_sources=[
36 MultiInputMediaSource(
37 media_type="image", # Or "video" or "audio"
38 asset_id=asset.id,
39 # url="<YOUR_MEDIA_URL>", # Use direct links to raw media files. Video hosting platforms and cloud storage sharing links are not supported
40 # base_64_string="<BASE_64_ENCODED_DATA>",
41 # name="outfit", # Required when input_text references this media source
42 ),
43 # Add up to 10 media sources. The platform processes them in the order they appear:
44 # MultiInputMediaSource(media_type="image", asset_id="<YOUR_ASSET_ID>", name="accessory"),
45 ],
46 ),
47 # auto_truncate=True, # Truncate text above 2,000 tokens instead of returning an error
48 # embedding_uncertainty=True, # Valid only when the request embeds text alone or media alone
49)
50
51# 5. Process the results
52print(f"Number of embeddings: {len(response.data)}")
53for embedding_data in response.data:
54 print(f"Embedding dimensions: {len(embedding_data.embedding)}")
55 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 for your query

Function call: You call the embed.v_2.create function.
Parameters:

  • input_type: The type of content. Set this parameter to multi_input.

  • model_name: The embedding model to use. This example uses marengo3.5.

  • (Optional) auto_truncate: Set this parameter to true to truncate your text when it exceeds 2,000 tokens. Media sources do not count toward this limit. The default is false, which returns a 400 error instead.

  • (Optional) embedding_uncertainty: Set this parameter to true to receive a data[].embedding_uncertainty field in the response, representing a per-dimension uncertainty vector with the same length as the embedding array. A higher value shows lower confidence in that dimension. Set this parameter to true only when your request embeds text only, or media only. A request that combines text with media sources returns a 400 error.

  • multi_input: A MultiInputRequest object containing the following properties:

    • (Optional) input_text: The text to include in the embedding. To reference a specific media source, use the <@name> format, where name matches the name field of that media source.
    • (Optional) media_sources: An array of up to 10 MultiInputMediaSource objects. The platform processes them in the order they appear. Each object contains the following properties:
      • media_type: The type of media. Valid values are image, video, and audio.
      • The source of the media 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 media file.

        • base_64_string: The base64-encoded media data.

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

      • (Optional) name: A unique name for this media source. This property is required when input_text references this media source.

    Provide input_text, media_sources, or both.

Return value: An object of type EmbeddingSuccessResponse containing a field named data, which is a list of embedding objects. A request that combines text with media sources returns one embedding in this list. Each embedding object includes the following fields:

  • embedding: An array of floats representing the embedding vector.
  • embedding_option: The type of embedding generated.

The response also contains a field named usage with the token counts for your request.

5

Process the results

This example prints the number of embeddings, their dimensions, and the first 10 values of each embedding.