Overview
Provides a unified Google embeddings implementation supporting both Vertex AI and Google AI (Gemini) through the new google-genai SDK and the legacy google-generativeai SDK.
Description
The GoogleEmbeddings class is a concrete implementation of BaseRagasEmbedding that provides text embedding capabilities via Google's embedding services. It supports three distinct backends:
- Google AI (Gemini) via new SDK (google-genai) -- The recommended approach. Uses a genai.Client() instance with the client.models.embed_content() interface. Supports batch embedding natively.
- Google AI (Gemini) via old SDK (google-generativeai) -- The deprecated approach (support ends August 2025). Uses the genai.embed_content() module-level function. Does not support native batch processing; texts are embedded individually.
- Vertex AI -- Enterprise Google Cloud embedding via google-cloud-aiplatform. Uses TextEmbeddingModel.from_pretrained() and supports batch embedding.
The class performs intelligent client resolution in its constructor. When no client is provided, it attempts auto-import of the new SDK first, then falls back to the old SDK. When a client is provided, it inspects the client's module path and class structure to determine which SDK interface to use. The _use_new_sdk flag tracks the resolved SDK path at runtime.
Async operations are implemented by delegating synchronous calls to a thread pool via run_sync_in_async, since Google's SDKs do not provide native async embedding support.
Usage
Import this class when you need to embed text using Google's embedding models, either through Google AI (Gemini) or Vertex AI. It is also used internally by the embedding_factory when the provider is set to "google".
Code Reference
Source Location
Signature
class GoogleEmbeddings(BaseRagasEmbedding):
PROVIDER_NAME = "google"
REQUIRES_CLIENT = False
DEFAULT_MODEL = "gemini-embedding-001"
def __init__(
self,
client: t.Optional[t.Any] = None,
model: str = "gemini-embedding-001",
use_vertex: bool = False,
project_id: t.Optional[str] = None,
location: t.Optional[str] = "us-central1",
cache: t.Optional[CacheInterface] = None,
**kwargs: t.Any,
): ...
def embed_text(self, text: str, **kwargs: t.Any) -> t.List[float]: ...
async def aembed_text(self, text: str, **kwargs: t.Any) -> t.List[float]: ...
def embed_texts(self, texts: t.List[str], **kwargs: t.Any) -> t.List[t.List[float]]: ...
async def aembed_texts(self, texts: t.List[str], **kwargs: t.Any) -> t.List[t.List[float]]: ...
Import
from ragas.embeddings.google_provider import GoogleEmbeddings
I/O Contract
Constructor Inputs
| Name |
Type |
Required |
Description
|
| client |
Any or None |
No |
Pre-initialized client: genai.Client() for new SDK, genai module for old SDK, or Vertex AI client. If None, auto-imports.
|
| model |
str |
No (default "gemini-embedding-001") |
The embedding model name (e.g., "gemini-embedding-001", "text-embedding-004")
|
| use_vertex |
bool |
No (default False) |
Whether to use Vertex AI instead of Google AI (Gemini)
|
| project_id |
str or None |
No |
Google Cloud project ID (used with Vertex AI)
|
| location |
str or None |
No (default "us-central1") |
Google Cloud region (used with Vertex AI)
|
| cache |
CacheInterface or None |
No |
Optional cache backend for caching embedding results
|
| **kwargs |
Any |
No |
Additional provider-specific arguments passed to the embedding API calls
|
embed_text Inputs
| Name |
Type |
Required |
Description
|
| text |
str |
Yes |
The text string to embed
|
| **kwargs |
Any |
No |
Additional arguments merged with constructor kwargs and passed to the embedding API
|
embed_text Outputs
| Name |
Type |
Description
|
| return |
List[float] |
A list of floats representing the embedding vector
|
embed_texts Inputs
| Name |
Type |
Required |
Description
|
| texts |
List[str] |
Yes |
List of text strings to embed
|
| **kwargs |
Any |
No |
Additional arguments for the embedding API calls
|
embed_texts Outputs
| Name |
Type |
Description
|
| return |
List[List[float]] |
A list of embedding vectors, one per input text
|
Usage Examples
New SDK (Recommended)
from google import genai
from ragas.embeddings.google_provider import GoogleEmbeddings
client = genai.Client(api_key="your-api-key")
embeddings = GoogleEmbeddings(client=client, model="gemini-embedding-001")
# Single text
vector = embeddings.embed_text("What is machine learning?")
# Batch texts
vectors = embeddings.embed_texts(["Text one", "Text two", "Text three"])
Old SDK (Deprecated)
import google.generativeai as genai
from ragas.embeddings.google_provider import GoogleEmbeddings
genai.configure(api_key="your-api-key")
embeddings = GoogleEmbeddings(client=genai, model="text-embedding-004")
vector = embeddings.embed_text("Hello world")
Auto-Import (No Client)
from ragas.embeddings.google_provider import GoogleEmbeddings
# Tries new SDK first, falls back to old SDK
embeddings = GoogleEmbeddings(model="text-embedding-004")
vector = embeddings.embed_text("Hello world")
Vertex AI
from ragas.embeddings.google_provider import GoogleEmbeddings
embeddings = GoogleEmbeddings(
client=vertex_client,
model="textembedding-gecko@003",
use_vertex=True,
project_id="my-gcp-project",
location="us-central1",
)
vector = embeddings.embed_text("Embed this text via Vertex AI")
Via Factory
from ragas.embeddings.base import embedding_factory
from google import genai
client = genai.Client(api_key="your-api-key")
embedder = embedding_factory("google", model="gemini-embedding-001", client=client)
vector = embedder.embed_text("Factory-created embedding")
Related Pages