Text Embeddings Fresh 🌱
Generate high-quality text embeddings with configurable dimensions, task types, and multi-language support.
Overview
Specifications
| Parameter | Value |
|---|---|
| Model | gemini-embedding-2-preview |
| Max input tokens | 8,192 |
| Default dimensions | 3,072 |
| Dimension range | 128 - 3,072 |
| Languages | 100+ |
| Pricing (Standard) | $0.20 / 1M tokens |
| Pricing (Batch) | $0.10 / 1M tokens |
Step 1: Basic Text Embedding
python
from google import genai
client = genai.Client()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents='What is the meaning of life?'
)
embedding = result.embeddings[0].values
print(f"Dimensions: {len(embedding)}") # 3072javascript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: 'What is the meaning of life?',
});
console.log(response.embeddings[0].values.length); // 3072bash
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{"content": {"parts": [{"text": "What is the meaning of life?"}]}}'Step 2: Specify Task Type
Task types optimize the embedding for your specific use case.
python
from google import genai
from google.genai import types
client = genai.Client()
# For search queries
query_embedding = client.models.embed_content(
model='gemini-embedding-2-preview',
contents='How to train a neural network',
config=types.EmbedContentConfig(
task_type='RETRIEVAL_QUERY'
)
)
# For documents being indexed
doc_embedding = client.models.embed_content(
model='gemini-embedding-2-preview',
contents='Neural networks are trained using backpropagation...',
config=types.EmbedContentConfig(
task_type='RETRIEVAL_DOCUMENT'
)
)Step 3: Custom Dimensions
Reduce dimensions to save storage while maintaining quality.
python
from google import genai
from google.genai import types
client = genai.Client()
# 768 dimensions — good balance of quality and size
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents='Your text here',
config=types.EmbedContentConfig(
output_dimensionality=768
)
)
print(f"Dimensions: {len(result.embeddings[0].values)}") # 768Normalization Required
When using dimensions below 3072, normalize your vectors before computing cosine similarity for accurate results.
Step 4: Batch Multiple Texts
Generate separate embeddings for multiple texts in one request.
python
from google import genai
client = genai.Client()
texts = [
'First document about machine learning',
'Second document about natural language processing',
'Third document about computer vision',
]
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=texts
)
for i, emb in enumerate(result.embeddings):
print(f"Text {i+1}: {len(emb.values)} dimensions")Single vs Multiple
- Single content with multiple parts = one aggregated embedding
- Multiple entries in contents array = separate embeddings per entry
Verification Checklist
- [ ] Text embedding generated with default 3072 dimensions
- [ ] Task type set appropriately for use case
- [ ] Custom dimensions tested (768, 1536)
- [ ] Normalization applied when using reduced dimensions
- [ ] Batch embedding tested with multiple texts
- [ ] Multi-language text tested if applicable
Troubleshooting
| Issue | Solution |
|---|---|
| Token limit exceeded | Split text into chunks under 8192 tokens |
| Low similarity scores | Use matching task types (QUERY + DOCUMENT) |
| Dimension mismatch | Ensure consistent output_dimensionality across all embeddings |
| Slow responses | Use batch API for 50% cost savings on large volumes |
See Also
- Getting Started — Initial setup
- Multimodal Embeddings — Combine text with other modalities
- Benchmarks — MTEB scores by dimension
- RAG Pipeline — End-to-end retrieval workflow