Skip to main content
Access Google’s Generative AI models, including the Gemini family, via the Gemini Developer API or Vertex AI. The Gemini Developer API offers quick setup with API keys, ideal for individual developers. Vertex AI provides enterprise features and integrates with Google Cloud Platform. For information on the latest models, model IDs, their features, context windows, etc. head to the Google AI docs.
Vertex AI consolidation & compatibilityAs of langchain-google-genai 4.0.0, this package uses the consolidated google-genai SDK instead of the legacy google-ai-generativelanguage SDK.This migration brings support for Gemini models both via the Gemini Developer API and Gemini API in Vertex AI, superseding certain classes in langchain-google-vertexai, such as ChatVertexAI.Read the full announcement and migration guide.
API ReferenceFor detailed documentation of all features and configuration options, head to the ChatGoogleGenerativeAI API reference.

Overview

Integration details

Model features

Setup

To access Google AI models you’ll need to create a Google Account, get a Google AI API key, and install the langchain-google-genai integration package.

Installation

Credentials

This integration supports two backends: Gemini Developer API and Vertex AI. The backend is selected automatically based on your configuration.

Backend selection

The backend is determined as follows:
  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
You can also explicitly set vertexai=True or vertexai=False to override auto-detection.
Quick setup with API keyRecommended for individual developers / new users.Head to Google AI Studio to generate an API key:
The integration checks for GOOGLE_API_KEY first, then GEMINI_API_KEY as a fallback.

Environment variables

To enable automated tracing of your model calls, set your LangSmith API key:

Instantiation

Now we can instantiate our model object and generate responses:
Temperature for Gemini 3.0+ modelsIf temperature is not explicitly set and the model is Gemini 3.0 or later, it will be automatically set to 1.0 instead of the default 0.7 per Google GenAI API best practices. Using 0.7 with Gemini 3.0+ can cause infinite loops, degraded reasoning performance, and failure on complex tasks.
See the ChatGoogleGenerativeAI API Reference for the full set of available model parameters.

Proxy configuration

If you need to use a proxy, set these environment variables before initializing:
For SOCKS5 proxies or advanced proxy configuration, use the client_args parameter:

Custom endpoints and headers

Use base_url and additional_headers for model-level HTTP options, such as routing requests through an internal gateway:
To pass headers or other HTTP options for a single request, provide http_options when invoking the model:
The same call-time options work with async invocation:
The per-request http_options may be a dictionary or a google.genai.types.HttpOptions object. Header dictionaries are merged with model-level additional_headers, and per-request header values take precedence. Model-level timeout and max_retries settings are preserved unless you explicitly override timeout or retry_options in http_options.

Invocation

Message content shapeGemini 3 series models return a list of content blocks to capture thought signatures. Use .text to get string content:
Gemini 2.5 and earlier return a plain string for .content.

Multimodal usage

Gemini models accept multimodal inputs (text, images, audio, video, PDFs) and some models can generate multimodal outputs.

Supported input methods

*YouTube URLs are supported for video input in preview.

File upload

You can upload files to Google’s servers and reference them by URI. This works for PDFs, images, videos, and audio files.
Once uploaded, you can reference the file in any of the media-specific sections below using the file_id pattern.

Image input

Provide image inputs along with text using a HumanMessage with a list content format.
Other supported image formats:
  • A Google Cloud Storage URI (gs://...). Ensure the service account has access.

PDF input

Provide PDF file inputs along with text.

Audio input

Provide audio file inputs along with text.

Video input

Provide video file inputs along with text.
YouTube video input (preview)
  • Only public videos are supported (not private or unlisted)
  • Free tier: max 8 hours of YouTube video per day

Image generation

Certain models can generate text and images inline. See Gemini API docs for details.
Use image_config to control image dimensions and quality (see genai.types.ImageConfig). It can be set at instantiation (applies to all calls) or at invocation (per-call override):
By default, image generation models may return both text and images (e.g. “Ok! Here’s an image of a…”). You can request that the model only return images by setting the response_modalities parameter:

Audio generation

Certain models can generate audio files. See Gemini API docs for details.
Vertex AI LimitationAudio generation models are currently in limited preview on Vertex AI and may require allowlist access. If you encounter an INVALID_ARGUMENT error when using TTS models with vertexai=True, your GCP project may need to be allowlisted.For more details, see this Google AI forum discussion.

Tool calling

You can equip the model with tools to call.

Structured output

Force the model to respond with a specific structure. See the Gemini API docs for more info.
For streaming structured output, merge dictionaries instead of using +=:

Structured output methods

Two methods are supported for structured output:
  • method="json_schema" (default): Uses Gemini’s native structured output. Recommended for better reliability, as it constrains the model’s generation process directly rather than relying on post-processing tool calls.
  • method="function_calling": Uses tool calling to extract structured data.
When using with_structured_output(method="function_calling"), do not pass additional tools (like Google Search) in the same call. To get structured output and search grounding in a single call, use .bind() with response_mime_type and response_schema instead of with_structured_output:
This uses Gemini’s native JSON schema mode for structuring the output while allowing tools like Google Search for grounding—all in a single LLM call.

Token usage tracking

Access token usage information from the response metadata.

Thinking support

Certain Gemini models support configurable thinking depth. The parameter depends on the model version:

Gemini 2.5 models: thinking_budget

For Gemini 2.5 models, use thinking_budget (an integer token count) instead:
  • Set to 0 to disable thinking (where supported)
  • Set to -1 for dynamic thinking (model decides)
  • Set to a positive integer to constrain token usage
Not all models allow disabling thinking. See the Gemini models documentation for details.

Viewing model thoughts

To see a thinking model’s reasoning, set include_thoughts=True:
See the Gemini API docs for more information on thinking.

Thought signatures

Thought signatures are encrypted representations of the model’s reasoning. They enable Gemini to maintain thought context across multi-turn conversations, since the API is stateless.
Gemini 3 may raise 4xx errors if thought signatures are not passed back with tool call responses. Upgrade to langchain-google-genai >= 3.1.0 to ensure this is handled correctly.
Signatures appear in AIMessage responses:
  • Text blocks: extras.signature within the content block
  • Tool calls: additional_kwargs["__gemini_function_call_thought_signatures__"]
For multi-turn conversations, pass the full AIMessage back to the model so signatures are preserved. This happens automatically when you append the AIMessage to your messages list (as shown in the tool calling example above).
Don’t reconstruct messages manually. If you create a new AIMessage instead of passing the original object, the signatures will be lost and the API may reject the request.

Built-in tools

Google Gemini supports a variety of built-in tools, which can be bound to the model in the usual way. See Gemini docs for detail.

Google maps

Certain models support grounding using Google Maps. Maps grounding connects Gemini’s generative capabilities with Google Maps’ current, factual location data. This enables location-aware applications that provide accurate, geographically specific responses. See Gemini docs for detail.
The response will include grounding metadata with location information from Google Maps. You can optionally provide a specific location context using tool_config with lat_lng. This is useful when you want to ground queries relative to a specific geographic point.

URL context

The URL context tool enables the model to access and analyze content from URLs you provide in your prompt. This is useful for tasks like summarizing web pages, extracting data from multiple sources, or answering questions about online content. See Gemini docs for detail and limitations.

Code execution

See Gemini docs for detail.

Computer use

The Gemini 2.5 Computer Use model (gemini-2.5-computer-use-preview-10-2025) can interact with browser environments to automate web tasks like clicking, typing, and scrolling.
Preview model limitationsThe Computer Use model is in preview and may produce unexpected behavior. Always supervise automated tasks and avoid use with sensitive data or critical operations. See the Gemini API docs for safety best practices.
You can configure the environment and exclude specific UI actions:
Advanced configuration
The model returns function calls for UI actions (like click_at, type_text_at, scroll) with normalized coordinates. You’ll need to implement the actual execution of these actions in your browser automation framework.

Safety settings

Gemini models have default safety settings that can be overridden. If you are receiving lots of 'Safety Warnings' from your models, you can try tweaking the safety_settings attribute of the model. For example, to turn off safety blocking for dangerous content, you can construct your LLM as follows:
For an enumeration of the categories and thresholds available, see Google’s safety settings guide.

Context caching

Context caching allows you to store and reuse content (e.g., PDFs, images) for faster processing. The cached_content parameter accepts a cache name created via the Google Generative AI API.
This caches a single file and queries it.
This caches two files using Part and queries them together.
See the Gemini API docs on context caching for more information.

Response metadata

Access response metadata from the model response.

API reference

For detailed documentation of all features and configuration options, head to the ChatGoogleGenerativeAI API reference.