Registry / llm-agents / langchain-google-genai

langchain-google-genai

JSON →
library4.3.6pypypi✓ verified 25d ago

LangChain Google Generative AI provides integrations with Google's GenAI models (like Gemini) for use within the LangChain framework. It offers classes for chat models, traditional LLMs, and embeddings. The current version is 4.2.1, and it's actively maintained with frequent updates within the larger LangChain Google monorepo.

pip install langchain-google-genai
INSTALL
IMPORT
SIG · LANGCHAIN-GOOGLE-G
L
langchain-google-genai
llm-agentspythonv4.3.6
Install
10.2s avg
Import
3906ms
Disk
112MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.3.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 4.078s · 101.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 10.2s · import 3.734s · 110MB
112MB installed
● package 112MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ChatGoogleGenerativeAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.chat_models import ChatGooglePalm
The `ChatGooglePalm` class from the main `langchain` package is deprecated. Use `ChatGoogleGenerativeAI` from `langchain-google-genai` for current Gemini models.
GoogleGenerativeAI
from langchain_google_genai import GoogleGenerativeAI
from langchain.llms import GooglePalm
The `GooglePalm` class from the main `langchain` package is deprecated. Use `GoogleGenerativeAI` from `langchain-google-genai` for current Gemini models.
GoogleGenerativeAIEmbeddings
from langchain_google_genai import GoogleGenerativeAIEmbeddings
Provides embedding functionality for Google's generative models.

This quickstart demonstrates how to initialize `ChatGoogleGenerativeAI` with a Gemini model and invoke it with a simple prompt. Ensure your `GOOGLE_API_KEY` is set as an environment variable or passed directly. It also shows the basic structure for chat interactions and mentions streaming.

import os from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.messages import HumanMessage, SystemMessage # Ensure GOOGLE_API_KEY is set in your environment variables. # You can also pass it directly as google_api_key='YOUR_API_KEY'. api_key = os.environ.get("GOOGLE_API_KEY", "") if not api_key: print("Warning: GOOGLE_API_KEY environment variable not set.") print("Please set it or pass it directly to the model constructor.") else: try: # Initialize the ChatGoogleGenerativeAI model chat_model = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=api_key) # Prepare messages for the model messages = [ SystemMessage(content="You are a helpful assistant."), HumanMessage(content="What is the capital of France?") ] # Invoke the model and print the response response = chat_model.invoke(messages) print("\nModel Response:", response.content) # Example of streaming (uncomment to try) # print("\nStreaming Response:") # for chunk in chat_model.stream(messages): # print(chunk.content, end="") # print() except Exception as e: print(f"An error occurred: {e}")
Debug
Known issues
gotchaAuthentication requires setting the `GOOGLE_API_KEY` environment variable or passing it directly to the model constructor. Alternatively, `GOOGLE_APPLICATION_CREDENTIALS` can be used for service account authentication in Google Cloud environments.
fix
Set `os.environ['GOOGLE_API_KEY'] = 'YOUR_KEY'` before initialization, or pass `google_api_key='YOUR_KEY'` to `ChatGoogleGenerativeAI` or `GoogleGenerativeAI`.
affects: All versions
deprecatedOlder `GooglePalm` and `ChatGooglePalm` classes found directly in `langchain.llms` or `langchain.chat_models` are deprecated. The functionality has been moved to the `langchain-google-genai` package for better modularity and to support newer models like Gemini.
fix
Migrate your imports from `langchain.llms.GooglePalm` to `from langchain_google_genai import GoogleGenerativeAI` and `langchain.chat_models.ChatGooglePalm` to `from langchain_google_genai import ChatGoogleGenerativeAI`.
affects: LangChain versions < 0.1.0 (for integrated classes) and all `langchain-google-genai` versions.
gotchaModel naming conventions can vary. Models like `gemini-pro`, `gemini-1.5-flash`, or specific versions like `gemini-pro-1.0` should be specified correctly. The default model may change or have different capabilities.
fix
Always specify the exact model name you intend to use (e.g., `model="gemini-pro"`). Refer to Google's official documentation for the latest available models and their capabilities.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'langchain_google_genai'
The `langchain-google-genai` package is not installed or not available in the current Python environment.
fix
Install the package using pip: `pip install -U langchain-google-genai`
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found.
This error occurs when the `langchain-google-genai` library cannot find valid authentication credentials (like a Google Cloud service account or an API key). It often defaults to looking for Application Default Credentials (ADC) if an API key is not correctly provided.
fix
Ensure your Google API key is set as an environment variable named `GOOGLE_API_KEY` or `GEMINI_API_KEY`, or pass it directly to the `ChatGoogleGenerativeAI` constructor: `ChatGoogleGenerativeAI(model="gemini-pro", google_api_key="YOUR_API_KEY")`.
AttributeError: 'GenerativeModel' object has no attribute '_system_instruction'
This typically indicates an incompatibility between the version of `langchain-google-genai` and the underlying `google-generativeai` SDK, or an internal API change where the `_system_instruction` attribute is no longer directly accessible or has been removed/renamed.
fix
Ensure both `langchain-google-genai` and `google-generativeai` are updated to their latest compatible versions. Sometimes restarting the kernel or environment after updating helps. If using within other frameworks (like CrewAI), check for known compatibility issues or version recommendations for those integrations.
ImportError: cannot import name 'GoogleGenerativeAI' from 'langchain_google_genai'
The class `GoogleGenerativeAI` is not directly exposed at the top level of the `langchain_google_genai` package anymore, or its name has changed. For chat models, the correct class is `ChatGoogleGenerativeAI`, and for legacy text completion, it is `GoogleGenerativeAI` but imported from `langchain_google_genai.llms`.
fix
For chat models (recommended), use `from langchain_google_genai import ChatGoogleGenerativeAI`. For legacy text completion LLMs, use `from langchain_google_genai.llms import GoogleGenerativeAI`.
Upgrade
Version history
4.3.6latest on PyPI · released Aug 26, 2026
Audit
Dependencies
google-generativeairequiredCore client library for interacting with Google's Generative AI services.
langchain-corerequiredProvides the foundational interfaces and classes for LangChain integrations.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources
langchain-google-genai — pip install langchain-google-genai · libregistry