Skip to main content
Connect to Google’s generative AI embeddings service using the GoogleGenerativeAIEmbeddings class, found in the langchain-google-genai package. This will help you get started with Google’s Generative AI embedding models (like Gemini) using LangChain. For detailed documentation on GoogleGenerativeAIEmbeddings features and configuration options, please refer to the API reference.

Overview

Integration details

Setup

To access Google Generative AI embedding models you’ll need to create a Google Cloud project, enable the Generative Language API, get an API key, and install the langchain-google-genai integration package.

Credentials

To use Google Generative AI models, you must have an API key. You can create one in Google AI Studio. See the Google documentation for instructions. Once you have a key, set it as an environment variable GOOGLE_API_KEY:
To enable automated tracing of your model calls, set your LangSmith API key:

Installation

Usage

Batch

You can also embed multiple strings at once for a processing speedup:

Indexing and Retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document in the InMemoryVectorStore.

Task type

GoogleGenerativeAIEmbeddings optionally support a task_type, which currently must be one of:
  • SEMANTIC_SIMILARITY: Used to generate embeddings that are optimized to assess text similarity.
  • CLASSIFICATION: Used to generate embeddings that are optimized to classify texts according to preset labels.
  • CLUSTERING: Used to generate embeddings that are optimized to cluster texts based on their similarities.
  • RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, QUESTION_ANSWERING, and FACT_VERIFICATION: Used to generate embeddings that are optimized for document search or information retrieval.
  • CODE_RETRIEVAL_QUERY: Used to retrieve a code block based on a natural language query, such as sort an array or reverse a linked list. Embeddings of the code blocks are computed using RETRIEVAL_DOCUMENT.
By default, we use RETRIEVAL_DOCUMENT in the embed_documents method and RETRIEVAL_QUERY in the embed_query method. If you provide a task type, we will use that for all methods.

API reference

For detailed documentation on GoogleGenerativeAIEmbeddings features and configuration options, please refer to the API reference.

Additional Configuration

You can pass the following parameters to ChatGoogleGenerativeAI in order to customize the SDK’s behavior:
  • client_options: Client Options to pass to the Google API Client, such as a custom client_options["api_endpoint"]
  • transport: The transport method to use, such as rest, grpc, or grpc_asyncio.

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.