Skip to main content
This library supports access to a variety of Google’s models, including the Gemini family of models and their Nano Banana image generation model. You can access these models through either Google’s Google AI API (sometimes also called the Generative AI API or the AI Studio API) or through the Google Cloud Platform Vertex AI service. This will help you getting started with ChatGoogle chat models. For detailed documentation of all ChatGoogle features and configurations head to the API reference.
@langchain/google is the recommended package for all new Google Gemini integrations. It replaces the older @langchain/google-genai and @langchain/google-vertexai packages. See legacy packages for migration details.

Overview

Integration details

Model features

See the links in the table headers below for guides on how to use specific features. Note that while logprobs are supported, Gemini has fairly restricted usage of them.

Setup

Credentials through AI Studio (API Key)

To use the model through Google AI Studio (sometimes called the Generative AI API), you will need an API key. You can obtain one from the Google AI Studio. Once you have your API key, you can set it as an environment variable:
Or you can pass it directly to the model constructor:

Credentials through Vertex AI Express Mode (API Key)

Vertex AI also supports Express Mode, which allows you to use an API key for authentication. You can obtain a Vertex AI API key from the Google Cloud Console. Once you have your API key, you can set it as an environment variable:
When using Vertex AI Express Mode, you will also need to specify the platform type as gcp when instantiating the model.

Credentials through Vertex AI (OAuth Application Default Credentials / ADC)

For production environments on Google Cloud, it is recommended to use Application Default Credentials (ADC). This is supported in Node.js environments. If you are running on a local machine, you can set up ADC by installing the Google Cloud SDK and running:
Alternatively, you can set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account key file:

Credentials through Vertex AI (OAuth saved credentials)

If you are running in a web environment or want to provide credentials directly, you can use the GOOGLE_CLOUD_CREDENTIALS environment variable. This should contain the content of your service account key file (not the path).
You can also provide these credentials directly in your code using the credentials parameter.

Tracing

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain ChatGoogle integration lives in the @langchain/google package:

Instantiation

The import path differs depending on whether you are running in a Node.js environment or a Web/Edge environment.
The model will automatically determine whether to use the Google AI API or Vertex AI based on your configuration:
  • If you provide an apiKey (or set GOOGLE_API_KEY), it defaults to Google AI.
  • If you provide credentials (or set GOOGLE_APPLICATION_CREDENTIALS / GOOGLE_CLOUD_CREDENTIALS in Node), it defaults to Vertex AI.

Google AI (AI Studio)

Vertex AI

Vertex AI Express Mode

To use Vertex AI with an API key (Express Mode), you must explicitly set the platformType.

Model Configuration Best Practices

While ChatGoogle supports standard model parameters like temperature, topP, and topK, best practice with Gemini models is to leave these at their default values. The models are highly tuned around these defaults. If you want to control the “randomness” or “creativity” of the model, it is recommended to use specific instructions in your prompt or system prompt (e.g., “Be creative”, “Give a concise factual answer”) rather than adjusting the temperature.

Invocation

Response Metadata

The AIMessage response contains metadata about the generation, including token usage and log probabilities.

Token Usage

The usage_metadata property allows you to inspect token counts.

Logprobs

If you enable logprobs in the model configuration, they will be available in the response_metadata.

Safety settings

By default, current versions of Gemini have safety settings turned off. If you want to enable safety settings for various categories, you can use the safetySettings attribute of the model.

Structured output

You can use the withStructuredOutput method to get structured JSON output from the model.

Tool calling

ChatGoogle supports standard LangChain tool calling as well as Gemini-specific “Specialty Tools” (like Code Execution and Grounding).

Standard Tools

You can use standard LangChain tools defined with Zod schemas.

Specialty Tools

Gemini offers several built-in tools for code execution and grounding.
You cannot mix these “Specialty Tools” (Code Execution, Google Search, etc.) with standard LangChain tools (like the weather tool above) in the same request.

Code execution

Gemini models support code execution, which allows the model to generate and run Python code to solve complex problems.
You can use the googleSearch tool to ground responses with Google Search. This is useful for questions about current events or specific facts.
The googleSearchRetrieval tool is maintained for backwards compatibility, but googleSearch is preferred.

Grounding with URL Retrieval

You can also ground responses using a specific URL.

Grounding with a data store

If you are using Vertex AI (platformType: "gcp"), you can ground responses using a Vertex AI Search data store.

Context caching

By default, Gemini models do implicit context caching. If the start of the history that you send to Gemini exactly matches context that Gemini has in its cache, it will reduce the token cost for that request. You can also explicitly pass some content to the model once, cache the input tokens, and then refer to the cached tokens for subsequent requests to reduce cost and latency. Creating this explicit cache is not supported by LangChain, but if you have created the cache, you can reference it in your invocation.

Multimodal Requests

The ChatGoogle model supports multimodal requests, allowing you to send images, audio, and video along with text. You can use the contentBlocks field in your messages to provide these inputs in a structured way.

Images

Audio

Video

Reasoning / Thinking

Google’s Gemini 2.5 and Gemini 3 models support “thinking” or “reasoning” steps. These models may perform reasoning even if you don’t explicitly configure it, but the library will only return the reasoning summaries (thought blocks) if you explicitly set a value for how much to reason/think. This library offers compatibility between models, allowing you to use unified parameters:
  • maxReasoningTokens (or thinkingBudget): Specifies the maximum number of tokens to use for reasoning.
    • 0: Turns off reasoning (if supported).
    • -1: Uses the model’s default.
    • > 0: Sets the specific token budget.
  • reasoningEffort (or thinkingLevel): Sets the relative effort.
    • Values: "minimal", "low", "medium", "high".
Thought blocks also include a reasoningContentBlock field. This contains the ContentBlock based on the underlying part sent by Gemini. While this is typically a text block, for multimodal models like Nano Banana Pro, it could be an image or other media block.

Image Generation with Nano Banana and Nano Banana Pro

To generate images, you need to use a model that supports it (such as gemini-2.5-flash-image) and configure the responseModalities to include “IMAGE”.

Speech Generation (TTS)

Some Gemini models support generating speech (audio output). To enable this, configure the responseModalities to include “AUDIO” and provide a speechConfig. The speechConfig can be a full Gemini speech configuration object, but for most cases you just need to provide a string with a prebuilt voice name. Many models return audio in raw PCM format (audio/L16), which requires a WAV header to be playable by most media players.

Multi-speaker TTS

You can also configure multiple speakers for a single request. This is useful to have Gemini read a script. The simplified speechConfig for this requires you to assign a speaker to each pre-defined name representing the voice, and then use that speaker in the script.

API Reference

For detailed documentation of all ChatGoogle features and configurations head to the API reference.