How to beginner · 3 min read

How to use Vertex AI Embeddings

Quick answer
Use the vertexai Python SDK to generate embeddings by initializing the client with your Google Cloud project and location, then calling EmbeddingModel.embed_texts() with your input texts. This returns vector embeddings suitable for semantic search or similarity tasks.

PREREQUISITES

  • Python 3.8+
  • Google Cloud project with Vertex AI API enabled
  • Service account key JSON with Vertex AI permissions
  • pip install vertexai google-cloud-aiplatform

Setup

Install the required Python packages and set up authentication with Google Cloud.

  • Install the SDKs: pip install vertexai google-cloud-aiplatform
  • Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account JSON key file path.
  • Ensure your Google Cloud project has Vertex AI API enabled.
bash
pip install vertexai google-cloud-aiplatform

Step by step

This example shows how to initialize the Vertex AI client, load the embedding model, and generate embeddings for a list of texts.

python
import os
import vertexai
from vertexai.preview.language_models import EmbeddingModel

# Set your Google Cloud project and location
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = "us-central1"

# Initialize Vertex AI SDK
vertexai.init(project=PROJECT_ID, location=LOCATION)

# Load the embedding model
embedding_model = EmbeddingModel.from_pretrained("textembedding-gecko@001")

# Texts to embed
texts = ["Hello world", "Vertex AI embeddings example"]

# Generate embeddings
embeddings = embedding_model.embed_texts(texts)

# Print the embedding vectors
for i, vector in enumerate(embeddings):
    print(f"Text: {texts[i]}\nEmbedding vector (first 5 values): {vector[:5]}\n")
output
Text: Hello world
Embedding vector (first 5 values): [0.0123, -0.0456, 0.0789, -0.0345, 0.0567]

Text: Vertex AI embeddings example
Embedding vector (first 5 values): [0.0234, -0.0678, 0.089, -0.0123, 0.0456]

Common variations

You can embed single texts or batches, and use different embedding models available in Vertex AI. Async usage is not currently supported in the official SDK.

  • Use embedding_model.embed_text() for a single string.
  • Change model by specifying another pretrained embedding model name.
  • Use the google-cloud-aiplatform client directly for advanced use cases.
python
from vertexai.preview.language_models import EmbeddingModel

# Single text embedding
single_embedding = embedding_model.embed_text("Single text example")
print(single_embedding[:5])
output
[0.0112, -0.0345, 0.0678, -0.0234, 0.0456]

Troubleshooting

  • Authentication errors: Ensure GOOGLE_APPLICATION_CREDENTIALS points to a valid service account JSON with Vertex AI permissions.
  • Model not found: Verify the embedding model name is correct and available in your region.
  • Quota exceeded: Check your Google Cloud quota and billing status.

Key Takeaways

  • Use the vertexai SDK to easily generate embeddings with EmbeddingModel.
  • Set GOOGLE_APPLICATION_CREDENTIALS for authentication with a service account key.
  • Choose the appropriate pretrained embedding model like textembedding-gecko@001 for best results.
Verified 2026-04 · textembedding-gecko@001
Verify ↗