Embed a Single Chunk

Call the Gemini embedding API and return the vector for one piece of text

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

The embedding API call

Use client.models.embed_content() to convert text into a vector. Pass the model name, the text, and a config object that sets the task type.

ArgumentValue
model"gemini-embedding-001"
contentsThe text string to embed
configtypes.EmbedContentConfig(task_type="RETRIEVAL_DOCUMENT")

The function returns a response object. Access the vector with result.embeddings[0].values.

from google.genai import types

result = client.models.embed_content(
    model="gemini-embedding-001",
    contents=text,
    config=types.EmbedContentConfig(task_type="RETRIEVAL_DOCUMENT")
)
return result.embeddings[0].values

Instructions

Complete the embed_text function. The starter code provides the signature.

  1. Create a variable named result. Assign it client.models.embed_content(model="gemini-embedding-001", contents=text, config=types.EmbedContentConfig(task_type="RETRIEVAL_DOCUMENT")).
  2. Return result.embeddings[0].values.