GoogleGenerativeAIEmbeddings¶
Reference docs
This page contains reference documentation for GoogleGenerativeAIEmbeddings. See the docs for conceptual guides, tutorials, and examples on using GoogleGenerativeAIEmbeddings.
GoogleGenerativeAIEmbeddings
¶
Bases: BaseModel, Embeddings
Google Generative AI Embeddings.
Setup
Vertex AI Platform Support
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):
- Set the
GOOGLE_API_KEYenvironment variable (recommended), or - Pass your API key using the
google_api_keykwarg
For 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:
| METHOD | DESCRIPTION |
|---|---|
embed_documents |
Embed a list of strings. |
embed_query |
Embed a single text. |
aembed_documents |
Embed a list of strings asynchronously. |
aembed_query |
Embed a single text asynchronously. |
model
class-attribute
instance-attribute
¶
The name of the embedding model to use.
Example: 'gemini-embedding-001'
task_type
class-attribute
instance-attribute
¶
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_api_key
class-attribute
instance-attribute
¶
google_api_key: SecretStr | None = Field(
alias="api_key",
default_factory=secret_from_env(["GOOGLE_API_KEY", "GEMINI_API_KEY"], default=None),
)
The Google API key to use.
If not provided, will check the env vars GOOGLE_API_KEY and GEMINI_API_KEY.
credentials
class-attribute
instance-attribute
¶
Custom credentials for Vertex AI authentication.
When provided, forces Vertex AI backend.
Accepts a google.auth.credentials.Credentials object.
vertexai
class-attribute
instance-attribute
¶
Whether to use Vertex AI backend.
If None (default), backend is automatically determined:
- If
GOOGLE_GENAI_USE_VERTEXAIenv var is set, uses that value - If
credentialsparameter is provided, uses Vertex AI - If
projectparameter is provided, uses Vertex AI - Otherwise, uses Gemini Developer API
project
class-attribute
instance-attribute
¶
Google Cloud project ID (Vertex AI only).
Falls back to GOOGLE_CLOUD_PROJECT env var if not provided.
location
class-attribute
instance-attribute
¶
Google Cloud region (Vertex AI only).
Defaults to GOOGLE_CLOUD_LOCATION env var, then 'us-central1'.
base_url
class-attribute
instance-attribute
¶
The base URL to use for the API client.
additional_headers
class-attribute
instance-attribute
¶
Additional HTTP headers to include in API requests.
client_args
class-attribute
instance-attribute
¶
Additional arguments to pass to the underlying HTTP client.
Applied to both sync and async clients.
request_options
class-attribute
instance-attribute
¶
A dictionary of request options to pass to the Google API client.
Example: {'timeout': 10}
output_dimensionality
class-attribute
instance-attribute
¶
Default output dimensionality for embeddings.
If set, all embed calls use this dimension unless explicitly overridden.
embed_documents
¶
embed_documents(
texts: list[str],
*,
batch_size: int = _DEFAULT_BATCH_SIZE,
task_type: str | None = None,
titles: list[str] | None = None,
output_dimensionality: int | None = None,
) -> list[list[float]]
Embed a list of strings.
Google Generative AI currently sets a max batch size of 100 strings.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
The list of strings to embed. |
batch_size
|
Batch size of embeddings to send to the model
TYPE:
|
task_type
|
TYPE:
|
titles
|
Optional list of titles for texts provided. Only applicable when |
output_dimensionality
|
Optional reduced dimension for the output embedding.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[list[float]]
|
List of embeddings, one for each text. |
embed_query
¶
embed_query(
text: str,
*,
task_type: str | None = None,
title: str | None = None,
output_dimensionality: int | None = None,
) -> list[float]
Embed a single text.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The text to embed.
TYPE:
|
task_type
|
TYPE:
|
title
|
Optional title for the text. Only applicable when
TYPE:
|
output_dimensionality
|
Optional reduced dimension for the output embedding.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[float]
|
Embedding for the text. |
aembed_documents
async
¶
aembed_documents(
texts: list[str],
*,
batch_size: int = _DEFAULT_BATCH_SIZE,
task_type: str | None = None,
titles: list[str] | None = None,
output_dimensionality: int | None = None,
) -> list[list[float]]
Embed a list of strings asynchronously.
Google Generative AI currently sets a max batch size of 100 strings.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
The list of strings to embed. |
batch_size
|
The batch size of embeddings to send to the model
TYPE:
|
task_type
|
TYPE:
|
titles
|
Optional list of titles for texts provided. Only applicable when |
output_dimensionality
|
Optional reduced dimension for the output embedding.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[list[float]]
|
List of embeddings, one for each text. |
aembed_query
async
¶
aembed_query(
text: str,
*,
task_type: str | None = None,
title: str | None = None,
output_dimensionality: int | None = None,
) -> list[float]
Embed a single text asynchronously.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The text to embed.
TYPE:
|
task_type
|
TYPE:
|
title
|
Optional title for the text. Only applicable when
TYPE:
|
output_dimensionality
|
Optional reduced dimension for the output embedding.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[float]
|
Embedding for the text. |