# langchain-google-genai
**LangChain integration for Google Gemini models using the `generative-ai` SDK**
This package enables seamless access to Google Gemini's chat, vision, embeddings, and retrieval-augmented generation (RAG) features within the LangChain ecosystem.
---
## Table of Contents
- [langchain-google-genai](#langchain-google-genai)
- [Table of Contents](#table-of-contents)
- [Overview](#overview)
- [Installation](#installation)
- [Quickstart](#quickstart)
- [Chat Models](#chat-models)
- [Multimodal Inputs](#multimodal-inputs)
- [Multimodal Outputs](#multimodal-outputs)
- [Audio Output](#audio-output)
- [Multimodal Outputs in Chains](#multimodal-outputs-in-chains)
- [Thinking Support](#thinking-support)
- [Embeddings](#embeddings)
- [Semantic Retrieval (RAG)](#semantic-retrieval-rag)
- [Resources](#resources)
---
## Overview
This package provides LangChain support for Google Gemini models (via the official [Google Generative AI SDK](https://googleapis.github.io/python-genai/)). It supports:
- Text and vision-based chat models
- Embeddings for semantic search
- Multimodal inputs and outputs
- Retrieval-Augmented Generation (RAG)
- Thought tracing with reasoning tokens
---
## Installation
```bash
pip install -U langchain-google-genai
````
---
## Quickstart
Set up your environment variable with your Gemini API key:
```bash
export GOOGLE_API_KEY=your-api-key
```
Then use the `ChatGoogleGenerativeAI` interface:
```python
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-pro")
response = llm.invoke("Sing a ballad of LangChain.")
print(response.content)
```
---
## Chat Models
The main interface for Gemini chat models is `ChatGoogleGenerativeAI`.
### Multimodal Inputs
Gemini vision models support image inputs in single messages.
```python
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-pro-vision")
message = HumanMessage(
content=[
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": "https://picsum.photos/seed/picsum/200/300"},
]
)
response = llm.invoke([message])
print(response.content)
```
✅ `image_url` can be:
- A public image URL
- A Google Cloud Storage path (`gcs://...`)
- A base64-encoded image (e.g., `data:image/png;base64,...`)
---
### Multimodal Outputs
The Gemini 2.0 Flash Experimental model supports both text and inline image outputs.
```python
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="models/gemini-2.0-flash-exp-image-generation")
response = llm.invoke(
"Generate an image of a cat and say meow",
generation_config=dict(response_modalities=["TEXT", "IMAGE"]),
)
image_base64 = response.content[0].get("image_url").get("url").split(",")[-1]
meow_text = response.content[1]
print(meow_text)
```
---
### Audio Output
```
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="models/gemini-2.5-flash-preview-tts")
# example
response = llm.invoke(
"Please say The quick brown fox jumps over the lazy dog",
generation_config=dict(response_modalities=["AUDIO"]),
)
# Base64 encoded binary data of the image
wav_data = response.additional_kwargs.get("audio")
with open("output.wav", "wb") as f:
f.write(wav_data)
```
---
### Multimodal Outputs in Chains
You can use Gemini models in a LangChain chain:
```python
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import ChatPromptTemplate
from langchain_google_genai import ChatGoogleGenerativeAI, Modality
llm = ChatGoogleGenerativeAI(
model="models/gemini-2.0-flash-exp-image-generation",
response_modalities=[Modality.TEXT, Modality.IMAGE],
)
prompt = ChatPromptTemplate.from_messages([
("human", "Generate an image of {animal} and tell me the sound it makes.")
])
chain = {"animal": RunnablePassthrough()} | prompt | llm
response = chain.invoke("cat")
```
---
### Thinking Support
Gemini 2.5 Flash Preview supports internal reasoning ("thoughts").
```python
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="models/gemini-2.5-flash-preview-04-17",
thinking_budget=1024
)
response = llm.invoke("How many O's are in Google? How did you verify your answer?")
reasoning_score = response.usage_metadata["output_token_details"]["reasoning"]
print("Response:", response.content)
print("Reasoning tokens used:", reasoning_score)
```
---
## Embeddings
You can use Gemini embeddings in LangChain:
```python
from langchain_google_genai import GoogleGenerativeAIEmbeddings
embeddings = GoogleGenerativeAIEmbeddings(model="models/gemini-embedding-001")
vector = embeddings.embed_query("hello, world!")
print(vector)
```
---
## Semantic Retrieval (RAG)
Use Gemini with RAG to retrieve relevant documents from your knowledge base.
```python
from langchain_google_genai.vectorstores import GoogleVectorStore
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.document_loaders import DirectoryLoader
# Create a corpus (collection of documents)
corpus_store = GoogleVectorStore.create_corpus(display_name="My Corpus")
# Create a document under that corpus
document_store = GoogleVectorStore.create_document(
corpus_id=corpus_store.corpus_id, display_name="My Document"
)
# Load and upload documents
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
for file in DirectoryLoader(path="data/").load():
chunks = text_splitter.split_documents([file])
document_store.add_documents(chunks)
# Query the document corpus
aqa = corpus_store.as_aqa()
response = aqa.invoke("What is the meaning of life?")
print("Answer:", response.answer)
print("Passages:", response.attributed_passages)
print("Answerable probability:", response.answerable_probability)
```
---
## Resources
- [LangChain Documentation](https://docs.langchain.com/)
- [Google Generative AI SDK](https://googleapis.github.io/python-genai/)
- [Gemini Model Documentation](https://ai.google.dev/)
Raw data
{
"_id": null,
"home_page": "https://github.com/langchain-ai/langchain-google",
"name": "langchain-google-genai",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ba/24/4ad44e9a8ad25682c22b56f0b665eb6d87090f2360355b48095e285a7810/langchain_google_genai-2.1.9.tar.gz",
"platform": null,
"description": "# langchain-google-genai\n\n**LangChain integration for Google Gemini models using the `generative-ai` SDK**\n\nThis package enables seamless access to Google Gemini's chat, vision, embeddings, and retrieval-augmented generation (RAG) features within the LangChain ecosystem.\n\n---\n\n## Table of Contents\n\n- [langchain-google-genai](#langchain-google-genai)\n - [Table of Contents](#table-of-contents)\n - [Overview](#overview)\n - [Installation](#installation)\n - [Quickstart](#quickstart)\n - [Chat Models](#chat-models)\n - [Multimodal Inputs](#multimodal-inputs)\n - [Multimodal Outputs](#multimodal-outputs)\n - [Audio Output](#audio-output)\n - [Multimodal Outputs in Chains](#multimodal-outputs-in-chains)\n - [Thinking Support](#thinking-support)\n - [Embeddings](#embeddings)\n - [Semantic Retrieval (RAG)](#semantic-retrieval-rag)\n - [Resources](#resources)\n\n---\n\n## Overview\n\nThis package provides LangChain support for Google Gemini models (via the official [Google Generative AI SDK](https://googleapis.github.io/python-genai/)). It supports:\n\n- Text and vision-based chat models\n- Embeddings for semantic search\n- Multimodal inputs and outputs\n- Retrieval-Augmented Generation (RAG)\n- Thought tracing with reasoning tokens\n\n---\n\n## Installation\n\n```bash\npip install -U langchain-google-genai\n````\n\n---\n\n## Quickstart\n\nSet up your environment variable with your Gemini API key:\n\n```bash\nexport GOOGLE_API_KEY=your-api-key\n```\n\nThen use the `ChatGoogleGenerativeAI` interface:\n\n```python\nfrom langchain_google_genai import ChatGoogleGenerativeAI\n\nllm = ChatGoogleGenerativeAI(model=\"gemini-pro\")\nresponse = llm.invoke(\"Sing a ballad of LangChain.\")\nprint(response.content)\n```\n\n---\n\n## Chat Models\n\nThe main interface for Gemini chat models is `ChatGoogleGenerativeAI`.\n\n### Multimodal Inputs\n\nGemini vision models support image inputs in single messages.\n\n```python\nfrom langchain_core.messages import HumanMessage\nfrom langchain_google_genai import ChatGoogleGenerativeAI\n\nllm = ChatGoogleGenerativeAI(model=\"gemini-pro-vision\")\n\nmessage = HumanMessage(\n content=[\n {\"type\": \"text\", \"text\": \"What's in this image?\"},\n {\"type\": \"image_url\", \"image_url\": \"https://picsum.photos/seed/picsum/200/300\"},\n ]\n)\n\nresponse = llm.invoke([message])\nprint(response.content)\n```\n\n\u2705 `image_url` can be:\n\n- A public image URL\n- A Google Cloud Storage path (`gcs://...`)\n- A base64-encoded image (e.g., `data:image/png;base64,...`)\n\n---\n\n### Multimodal Outputs\n\nThe Gemini 2.0 Flash Experimental model supports both text and inline image outputs.\n\n```python\nfrom langchain_google_genai import ChatGoogleGenerativeAI\n\nllm = ChatGoogleGenerativeAI(model=\"models/gemini-2.0-flash-exp-image-generation\")\n\nresponse = llm.invoke(\n \"Generate an image of a cat and say meow\",\n generation_config=dict(response_modalities=[\"TEXT\", \"IMAGE\"]),\n)\n\nimage_base64 = response.content[0].get(\"image_url\").get(\"url\").split(\",\")[-1]\nmeow_text = response.content[1]\nprint(meow_text)\n```\n\n---\n\n### Audio Output\n\n```\nfrom langchain_google_genai import ChatGoogleGenerativeAI\n\nllm = ChatGoogleGenerativeAI(model=\"models/gemini-2.5-flash-preview-tts\")\n# example\nresponse = llm.invoke(\n \"Please say The quick brown fox jumps over the lazy dog\",\n generation_config=dict(response_modalities=[\"AUDIO\"]),\n)\n\n# Base64 encoded binary data of the image\nwav_data = response.additional_kwargs.get(\"audio\")\nwith open(\"output.wav\", \"wb\") as f:\n f.write(wav_data)\n```\n\n---\n\n### Multimodal Outputs in Chains\n\nYou can use Gemini models in a LangChain chain:\n\n```python\nfrom langchain_core.runnables import RunnablePassthrough\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_google_genai import ChatGoogleGenerativeAI, Modality\n\nllm = ChatGoogleGenerativeAI(\n model=\"models/gemini-2.0-flash-exp-image-generation\",\n response_modalities=[Modality.TEXT, Modality.IMAGE],\n)\n\nprompt = ChatPromptTemplate.from_messages([\n (\"human\", \"Generate an image of {animal} and tell me the sound it makes.\")\n])\n\nchain = {\"animal\": RunnablePassthrough()} | prompt | llm\nresponse = chain.invoke(\"cat\")\n```\n\n---\n\n### Thinking Support\n\nGemini 2.5 Flash Preview supports internal reasoning (\"thoughts\").\n\n```python\nfrom langchain_google_genai import ChatGoogleGenerativeAI\n\nllm = ChatGoogleGenerativeAI(\n model=\"models/gemini-2.5-flash-preview-04-17\",\n thinking_budget=1024\n)\n\nresponse = llm.invoke(\"How many O's are in Google? How did you verify your answer?\")\nreasoning_score = response.usage_metadata[\"output_token_details\"][\"reasoning\"]\n\nprint(\"Response:\", response.content)\nprint(\"Reasoning tokens used:\", reasoning_score)\n```\n\n---\n\n## Embeddings\n\nYou can use Gemini embeddings in LangChain:\n\n```python\nfrom langchain_google_genai import GoogleGenerativeAIEmbeddings\n\nembeddings = GoogleGenerativeAIEmbeddings(model=\"models/gemini-embedding-001\")\nvector = embeddings.embed_query(\"hello, world!\")\nprint(vector)\n```\n\n---\n\n## Semantic Retrieval (RAG)\n\nUse Gemini with RAG to retrieve relevant documents from your knowledge base.\n\n```python\nfrom langchain_google_genai.vectorstores import GoogleVectorStore\nfrom langchain_text_splitters import CharacterTextSplitter\nfrom langchain_community.document_loaders import DirectoryLoader\n\n# Create a corpus (collection of documents)\ncorpus_store = GoogleVectorStore.create_corpus(display_name=\"My Corpus\")\n\n# Create a document under that corpus\ndocument_store = GoogleVectorStore.create_document(\n corpus_id=corpus_store.corpus_id, display_name=\"My Document\"\n)\n\n# Load and upload documents\ntext_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)\nfor file in DirectoryLoader(path=\"data/\").load():\n chunks = text_splitter.split_documents([file])\n document_store.add_documents(chunks)\n\n# Query the document corpus\naqa = corpus_store.as_aqa()\nresponse = aqa.invoke(\"What is the meaning of life?\")\n\nprint(\"Answer:\", response.answer)\nprint(\"Passages:\", response.attributed_passages)\nprint(\"Answerable probability:\", response.answerable_probability)\n```\n\n---\n\n## Resources\n\n- [LangChain Documentation](https://docs.langchain.com/)\n- [Google Generative AI SDK](https://googleapis.github.io/python-genai/)\n- [Gemini Model Documentation](https://ai.google.dev/)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An integration package connecting Google's genai package and LangChain",
"version": "2.1.9",
"project_urls": {
"Homepage": "https://github.com/langchain-ai/langchain-google",
"Repository": "https://github.com/langchain-ai/langchain-google",
"Source Code": "https://github.com/langchain-ai/langchain-google/tree/main/libs/genai"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "84d8e1162835d5d6eefaae341c2d1cf750ab53222a421252346905187e53b8a2",
"md5": "2d8e828ba9b54c65be92974995e0bbba",
"sha256": "8d3aab59706b8f8920a22bcfd63c5000ce430fe61db6ecdec262977d1a0be5b8"
},
"downloads": -1,
"filename": "langchain_google_genai-2.1.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2d8e828ba9b54c65be92974995e0bbba",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 49381,
"upload_time": "2025-08-04T18:51:50",
"upload_time_iso_8601": "2025-08-04T18:51:50.510062Z",
"url": "https://files.pythonhosted.org/packages/84/d8/e1162835d5d6eefaae341c2d1cf750ab53222a421252346905187e53b8a2/langchain_google_genai-2.1.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "ba244ad44e9a8ad25682c22b56f0b665eb6d87090f2360355b48095e285a7810",
"md5": "ffad932891e1a9a71c79c1c0f31df0f3",
"sha256": "cd5d6f644b8dac3e312e30101bb97541aab240e82678e87a4df039ee1dc77531"
},
"downloads": -1,
"filename": "langchain_google_genai-2.1.9.tar.gz",
"has_sig": false,
"md5_digest": "ffad932891e1a9a71c79c1c0f31df0f3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 45866,
"upload_time": "2025-08-04T18:51:51",
"upload_time_iso_8601": "2025-08-04T18:51:51.420921Z",
"url": "https://files.pythonhosted.org/packages/ba/24/4ad44e9a8ad25682c22b56f0b665eb6d87090f2360355b48095e285a7810/langchain_google_genai-2.1.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-04 18:51:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "langchain-ai",
"github_project": "langchain-google",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "langchain-google-genai"
}