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.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 thelangchain-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:- If
GOOGLE_GENAI_USE_VERTEXAIenv var is set, uses that value - If
credentialsparameter is provided, uses Vertex AI - If
projectparameter is provided, uses Vertex AI - Otherwise, uses Gemini Developer API
vertexai=True or vertexai=False to override auto-detection.
- Gemini Developer API
- Vertex AI with API key
- Vertex AI with credentials
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:- Gemini Developer API
- Vertex AI
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.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:client_args parameter:
Custom endpoints and headers
Usebase_url and additional_headers for model-level HTTP options, such as routing requests through an internal gateway:
http_options when invoking the model:
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 Gemini 2.5 and earlier return a plain string for
.text to get string content:.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.file_id pattern.
Image input
Provide image inputs along with text using aHumanMessage with a list content format.
- 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.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):
response_modalities parameter:
Audio generation
Certain models can generate audio files. See Gemini API docs for details.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.+=:
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.
Combining structured output with Google Search
When usingwith_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:
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
0to disable thinking (where supported) - Set to
-1for dynamic thinking (model decides) - Set to a positive integer to constrain token usage
Viewing model thoughts
To see a thinking model’s reasoning, setinclude_thoughts=True:
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.AIMessage responses:
- Text blocks:
extras.signaturewithin the content block - Tool calls:
additional_kwargs["__gemini_function_call_thought_signatures__"]
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).
Built-in tools
Google Gemini supports a variety of built-in tools, which can be bound to the model in the usual way.Google search
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.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.
Advanced configuration
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:
Context caching
Context caching allows you to store and reuse content (e.g., PDFs, images) for faster processing. Thecached_content parameter accepts a cache name created via the Google Generative AI API.
Single file example
Single file example
This caches a single file and queries it.
Multiple files example
Multiple files example
This caches two files using
Part and queries them together.Response metadata
Access response metadata from the model response.API reference
For detailed documentation of all features and configuration options, head to theChatGoogleGenerativeAI API reference.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

