Skip to content

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):

  1. Set the GOOGLE_API_KEY environment variable (recommended), or
  2. Pass your API key using the google_api_key kwarg

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:

embeddings = GoogleGenerativeAIEmbeddings(
    model="gemini-embedding-001",
    client_args={"proxy": "socks5://user:pass@host:port"},
)
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.

client class-attribute instance-attribute

client: Any = None

The Google GenAI client instance.

model class-attribute instance-attribute

model: str = Field(...)

The name of the embedding model to use.

Example: 'gemini-embedding-001'

task_type class-attribute instance-attribute

task_type: str | None = Field(default=None)

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

credentials: Any = Field(default=None, exclude=True)

Custom credentials for Vertex AI authentication.

When provided, forces Vertex AI backend.

Accepts a google.auth.credentials.Credentials object.

vertexai class-attribute instance-attribute

vertexai: bool | None = Field(default=None)

Whether to use Vertex AI backend.

If None (default), backend is automatically determined:

  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

project class-attribute instance-attribute

project: str | None = Field(default=None)

Google Cloud project ID (Vertex AI only).

Falls back to GOOGLE_CLOUD_PROJECT env var if not provided.

location class-attribute instance-attribute

location: str | None = Field(
    default_factory=from_env("GOOGLE_CLOUD_LOCATION", default=None)
)

Google Cloud region (Vertex AI only).

Defaults to GOOGLE_CLOUD_LOCATION env var, then 'us-central1'.

base_url class-attribute instance-attribute

base_url: str | None = Field(default=None)

The base URL to use for the API client.

additional_headers class-attribute instance-attribute

additional_headers: dict[str, str] | None = Field(default=None)

Additional HTTP headers to include in API requests.

client_args class-attribute instance-attribute

client_args: dict[str, Any] | None = Field(default=None)

Additional arguments to pass to the underlying HTTP client.

Applied to both sync and async clients.

request_options class-attribute instance-attribute

request_options: dict | None = Field(default=None)

A dictionary of request options to pass to the Google API client.

Example: {'timeout': 10}

output_dimensionality class-attribute instance-attribute

output_dimensionality: int | None = Field(default=None)

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.

TYPE: list[str]

batch_size

Batch size of embeddings to send to the model

TYPE: int DEFAULT: _DEFAULT_BATCH_SIZE

task_type

TYPE: str | None DEFAULT: None

titles

Optional list of titles for texts provided.

Only applicable when TaskType is 'RETRIEVAL_DOCUMENT'.

TYPE: list[str] | None DEFAULT: None

output_dimensionality

Optional reduced dimension for the output embedding.

TYPE: int | None DEFAULT: None

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: str

task_type

TYPE: str | None DEFAULT: None

title

Optional title for the text.

Only applicable when TaskType is 'RETRIEVAL_DOCUMENT'.

TYPE: str | None DEFAULT: None

output_dimensionality

Optional reduced dimension for the output embedding.

TYPE: int | None DEFAULT: None

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.

TYPE: list[str]

batch_size

The batch size of embeddings to send to the model

TYPE: int DEFAULT: _DEFAULT_BATCH_SIZE

task_type

TYPE: str | None DEFAULT: None

titles

Optional list of titles for texts provided.

Only applicable when TaskType is 'RETRIEVAL_DOCUMENT'.

TYPE: list[str] | None DEFAULT: None

output_dimensionality

Optional reduced dimension for the output embedding.

TYPE: int | None DEFAULT: None

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: str

task_type

TYPE: str | None DEFAULT: None

title

Optional title for the text.

Only applicable when TaskType is 'RETRIEVAL_DOCUMENT'.

TYPE: str | None DEFAULT: None

output_dimensionality

Optional reduced dimension for the output embedding.

TYPE: int | None DEFAULT: None

RETURNS DESCRIPTION
list[float]

Embedding for the text.