GoogleGenerativeAIEmbeddings()The task type.
Valid options include:
'TASK_TYPE_UNSPECIFIED''RETRIEVAL_QUERY''RETRIEVAL_DOCUMENT''SEMANTIC_SIMILARITY''CLASSIFICATION''CLUSTERING''QUESTION_ANSWERING''FACT_VERIFICATION''CODE_RETRIEVAL_QUERY'See TaskType for details.
Google Generative AI Embeddings.
Setup:
Added in langchain-google-genai 4.0.0.
GoogleGenerativeAIEmbeddings now supports both the Gemini Developer
API and Vertex AI Platform as backend options.
For Gemini Developer API (simplest):
GOOGLE_API_KEY environment variable (recommended), orgoogle_api_key kwargFor Vertex AI:
Set vertexai=True and provide project (and optionally location).
Example:
from langchain_google_genai import GoogleGenerativeAIEmbeddings
# Gemini Developer API
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
embeddings.embed_query("What's our Q1 revenue?")
# Vertex AI
embeddings = GoogleGenerativeAIEmbeddings(
model="gemini-embedding-001",
project="my-project",
vertexai=True,
)
**Automatic backend detection** (when `vertexai=None` / unspecified):
1. If `GOOGLE_GENAI_USE_VERTEXAI` env var is set, uses that value
2. If `credentials` parameter is provided, uses Vertex AI
3. If `project` parameter is provided, uses Vertex AI
4. Otherwise, uses Gemini Developer API
Environment variables:
| Variable | Purpose | Backend |
|---|---|---|
GOOGLE_API_KEY |
API key (primary) | Both |
GEMINI_API_KEY |
API key (fallback) | Both |
GOOGLE_GENAI_USE_VERTEXAI |
Force Vertex AI (true/false) |
Vertex AI |
GOOGLE_CLOUD_PROJECT |
GCP project ID | Vertex AI |
GOOGLE_CLOUD_LOCATION |
GCP region (default: us-central1) |
Vertex AI |
HTTPS_PROXY |
HTTP/HTTPS proxy URL | Both |
SSL_CERT_FILE |
Custom SSL certificate file | Both |
GOOGLE_API_KEY is checked first for backwards compatibility. (GEMINI_API_KEY
was introduced later to better reflect the API's branding.)
Proxy configuration:
Set these before initializing:
export HTTPS_PROXY='http://username:password@proxy_uri:port'
export SSL_CERT_FILE='path/to/cert.pem' # Optional: custom SSL certificate
For SOCKS5 proxies or advanced proxy configuration, use the client_args
parameter:
embeddings = GoogleGenerativeAIEmbeddings(
model="gemini-embedding-001",
client_args={"proxy": "socks5://user:pass@host:port"},
)