Skip to content

Gemini Embedding 2 API ​

Model overview ​

Gemini Embedding 2 is Google's multimodal embedding model. It maps text, images, PDFs, audio, and video into a shared semantic space for semantic search, cross-modal retrieval, classification, and RAG knowledge bases. It returns numeric vectors for similarity calculations and indexing.

PropertyDescription
Model namegemini-embedding-2
InputsText, images, PDFs, audio, and video, including combined inputs
Context windowUp to 8,192 tokens across all inputs
Output dimensions3,072 by default; adjust with embedContentConfig.outputDimensionality, for example 768 or 1536

See the official model documentation for media formats, counts, and duration limits.

API usage ​

Endpoint and authentication ​

Use Google's native embedContent interface with the Exchange Token endpoint and your platform API key:

text
POST https://api.exchangetoken.ai/v1/models/gemini-embedding-2:embedContent
http
Content-Type: application/json
x-goog-api-key: YOUR_API_KEY

You can also replace x-goog-api-key with Authorization: Bearer YOUR_API_KEY.

Requests and responses on this page follow the Google Cloud native embedContent REST schema.

Request Body ​

FieldTypeRequiredDescription
contentobjectYesInput to embed
content.partsarrayYesInput parts; each uses one of text, inlineData, or fileData
content.parts[].textstringBy input typeText input
content.parts[].inlineDataobjectBy input typeMedia with mimeType and Base64 data, without a data:...;base64, prefix
content.parts[].fileDataobjectBy input typeFile with mimeType and an upstream-readable fileUri, such as gs://...
embedContentConfigobjectNoEmbedding options
embedContentConfig.outputDimensionalityintegerNoVector dimensions
embedContentConfig.documentOcrbooleanNoEnable document OCR
embedContentConfig.audioTrackExtractionbooleanNoInclude a video's audio track

All parts in one content.parts produce one vector. Text and an image together produce a joint representation; send two requests to index them separately.

For text retrieval, put instructions in the text, such as task: search result | query: How can I track my order?. Format documents as title: Order help | text: Track delivery on the orders page. This model uses text instructions instead of the older taskType / task_type parameter. See the official task instructions.

Response Body ​

A successful call returns a JSON object:

FieldTypeDescription
embedding.valuesnumber[]Generated vector
usageMetadata.promptTokenCountintegerInput tokens
usageMetadata.totalTokenCountintegerTotal tokens, when returned
usageMetadata.promptTokensDetailsarrayInput usage by modality
usageMetadata.promptTokensDetails[].modalitystringFor example, TEXT, IMAGE, AUDIO, or VIDEO
usageMetadata.promptTokensDetails[].tokenCountintegerTokens for that modality
truncatedbooleanWhether input was truncated, when returned

This illustrative response shows only the first 3 vector values and uses sample usage counts. Actual dimensions and usage come from your response.

json
{
  "embedding": {
    "values": [0.012345, -0.023456, 0.034567]
  },
  "usageMetadata": {
    "promptTokenCount": 12,
    "totalTokenCount": 12,
    "promptTokensDetails": [
      { "modality": "TEXT", "tokenCount": 12 }
    ]
  },
  "truncated": false
}

Read the vector from embedding.values. This endpoint returns a single embedding object.

Examples ​

Set your Exchange Token API key. All examples below use this environment variable:

bash
export EXCHANGE_TOKEN_API_KEY="YOUR_API_KEY"

Text embedding (cURL) ​

bash
curl --fail-with-body -sS -X POST \
  "https://api.exchangetoken.ai/v1/models/gemini-embedding-2:embedContent" \
  -H "x-goog-api-key: $EXCHANGE_TOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "content": {
      "parts": [{ "text": "Exchange Token provides unified access to model APIs." }]
    }
  }'

Retrieval query with custom dimensions (cURL) ​

Generate a 768-dimensional query vector. Index documents with the same model and dimensions, replacing the text with the document content and task format.

bash
curl --fail-with-body -sS -X POST \
  "https://api.exchangetoken.ai/v1/models/gemini-embedding-2:embedContent" \
  -H "x-goog-api-key: $EXCHANGE_TOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "content": {
      "parts": [{ "text": "task: search result | query: How can I track my order?" }]
    },
    "embedContentConfig": {
      "outputDimensionality": 768
    }
  }'

Joint text and image embedding (Python) ​

Encode a local image.png as Base64 and embed it together with text. This example uses only the Python standard library.

python
import base64
import json
import os
from pathlib import Path
from urllib.request import Request, urlopen

image_data = base64.b64encode(Path("image.png").read_bytes()).decode("ascii")
payload = {
    "content": {
        "parts": [
            {"text": "A dog running across a grassy field"},
            {"inlineData": {"mimeType": "image/png", "data": image_data}},
        ]
    },
    "embedContentConfig": {"outputDimensionality": 768},
}

request = Request(
    "https://api.exchangetoken.ai/v1/models/gemini-embedding-2:embedContent",
    data=json.dumps(payload).encode("utf-8"),
    headers={
        "x-goog-api-key": os.environ["EXCHANGE_TOKEN_API_KEY"],
        "Content-Type": "application/json",
    },
    method="POST",
)
with urlopen(request, timeout=120) as response:
    result = json.load(response)

values = result["embedding"]["values"]
print("Dimensions:", len(values))
print("First 5 values:", values[:5])
print("Usage:", result.get("usageMetadata"))

You can also send PDFs, audio, and video through inlineData. Replace the file, mimeType, and optional text. Remove the text Part to embed only the file. Common types are listed below; files must meet the model limits.

InputExample MIME types
Imageimage/png, image/jpeg
PDFapplication/pdf
Audioaudio/mp3, audio/wav
Videovideo/mp4, video/mpeg

Reference a PDF file (cURL) ​

Use fileData for an existing Google Cloud Storage file that upstream can read. Replace gs://YOUR_BUCKET/document.pdf with your URI and ensure upstream has read access; a URI does not upload a local file. This example enables document OCR.

bash
curl --fail-with-body -sS -X POST \
  "https://api.exchangetoken.ai/v1/models/gemini-embedding-2:embedContent" \
  -H "x-goog-api-key: $EXCHANGE_TOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "content": {
      "parts": [{
        "fileData": {
          "mimeType": "application/pdf",
          "fileUri": "gs://YOUR_BUCKET/document.pdf"
        }
      }]
    },
    "embedContentConfig": {
      "outputDimensionality": 768,
      "documentOcr": true
    }
  }'