Code Beginner easy · 5 min

ChatGoogleGenerativeAI: Gemini via LangChain

What you will learn
Connect to Google's Gemini model through LangChain using the ChatGoogleGenerativeAI class.

Why this matters

Gemini is a free-tier-friendly LLM alternative to OpenAI, and LangChain abstracts away the API differences so your chain code stays identical whether you swap from OpenAI to Gemini: this is the whole point of LangChain's unified interface.

Skip if: Don't use ChatGoogleGenerativeAI if you need real-time information, financial data updates, or models with strict latency SLAs: Gemini's free tier has rate limits. Also avoid it if your organization has security policies that block Google APIs.

Explanation

ChatGoogleGenerativeAI is LangChain's wrapper around Google's Gemini API. It presents Gemini as a standard LangChain LLM that accepts the same invoke() and stream() methods as ChatOpenAI, Claude, or any other chat model: no special syntax required.

Mechanically, when you call invoke() on a ChatGoogleGenerativeAI instance, LangChain translates your messages into Google's API format, sends the request to Gemini's servers, receives the response, and converts it back into a standard AIMessage object. The interface is identical to every other LLM in LangChain.

Use ChatGoogleGenerativeAI when you want to prototype with a free model, need cost-effective inference for non-critical applications, or want to test if swapping LLM providers breaks your chain logic. It's your first proof that LangChain chains are truly model-agnostic.

Analogy

ChatGoogleGenerativeAI is like a universal power adapter. Your chain is the device. Whether you plug it into an OpenAI outlet (ChatOpenAI), Google outlet (ChatGoogleGenerativeAI), or Anthropic outlet (ChatAnthropic), the device works the same way: only the backend changes.

Code

Illustrative only - not runnable without a valid API key
python
import os
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

os.environ["GOOGLE_API_KEY"] = "your-google-api-key-here"

llm = ChatGoogleGenerativeAI(
    model="gemini-2.0-flash",
    temperature=0.7
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful programming assistant."),
    ("human", "{question}")
])

chain = prompt | llm | StrOutputParser()

response = chain.invoke({"question": "What is LCEL in LangChain?"})
print(response)
Output
LCEL stands for LangChain Expression Language. It's a declarative way to compose chains in LangChain using the pipe operator (|). Instead of writing imperative code with intermediate variables, you chain components together: prompt | llm | output_parser. This makes your code more readable, enables automatic batching and streaming, and simplifies debugging. LCEL is now the recommended way to build LangChain applications and replaces the older LLMChain class entirely.

What just happened?

The code created a ChatGoogleGenerativeAI instance pointing to Gemini 2.0 Flash, built a prompt template with system and user messages, chained them together with the LLM and a string parser using the pipe operator, and invoked the chain with a question about LCEL. Google's Gemini API processed the request and returned a text response about what LCEL is.

Common gotcha

Forgetting to set the GOOGLE_API_KEY environment variable or using an invalid/expired API key will cause a silent authentication failure that looks like a network timeout. Also, ChatGoogleGenerativeAI doesn't support streaming in the same way older versions did: you must use .stream() explicitly on the chain, not on the model itself.

Error recovery

google.auth.exceptions.DefaultCredentialsError
GOOGLE_API_KEY environment variable is not set or is empty. Set it with: os.environ["GOOGLE_API_KEY"] = "your-key" before instantiating ChatGoogleGenerativeAI.
google.api_core.exceptions.PermissionDenied
Your API key is invalid, revoked, or doesn't have access to the Gemini API. Verify the key in Google Cloud Console and ensure the Generative AI API is enabled for your project.
google.api_core.exceptions.ResourceExhausted
You've hit the rate limit on the free tier. Either wait before retrying or switch to a paid tier in Google Cloud Console.
ValueError: model not found
The model name (e.g., 'gemini-2.0-flash') doesn't exist or you have a typo. Check the current available models at https://ai.google.dev/models.

Experienced dev note

A common trap: developers think ChatGoogleGenerativeAI is 'just like ChatOpenAI but free' and skip reading the error messages. Gemini's rate limits are strict on the free tier (60 requests per minute, shared across all users using your API key if you hardcode it). In production, always use service account credentials with proper IAM roles rather than hardcoding an API key. Also, Gemini's context window is large (1M tokens in 2.0 models) but it's slower than OpenAI at processing very long inputs: profile before assuming it's a drop-in replacement for production workloads.

Check your understanding

If you replaced ChatGoogleGenerativeAI with ChatOpenAI in the code above (and provided an OpenAI API key instead), would the chain output change? Why or why not?

Show answer hint

The answer involves understanding that LCEL chains are model-agnostic and the underlying logic shouldn't change: but the actual text response will differ because Gemini and GPT-4 are different models with different training data and reasoning patterns. The structure and invocation method remain identical.

VERSION ChatGoogleGenerativeAI moved from langchain_community to its own package (langchain_google_genai) in langchain 0.2.0+. If using langchain < 0.2.0, import from langchain_community.chat_models instead.
NEXT

Next, learn how to chain multiple LLMs together or add memory to your chain so it remembers previous messages: both are natural extensions that keep you within LangChain's LCEL fundamentals.

Community Notes

No notes yetBe the first to share a version-specific fix or tip.