# 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
- [Overview](#overview)
- [Installation](#installation)
- [Quickstart](#quickstart)
- [Chat Models](#chat-models)
- [Multimodal Inputs](#multimodal-inputs)
- [Multimodal Outputs](#multimodal-outputs)
- [Multimodal Outputs in Chains](#multimodal-outputs-in-chains)
- [Thinking Support](#thinking-support)
- [Embeddings](#embeddings)
- [Semantic Retrieval (RAG)](#semantic-retrieval-rag)
---
## 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/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/d2/03/5ff9f00f942a3ea695a8e250b0ae681c50c8eacee21f716d7668a8bd0a82/langchain_google_genai-2.1.7.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- [Overview](#overview)\n- [Installation](#installation)\n- [Quickstart](#quickstart)\n- [Chat Models](#chat-models)\n - [Multimodal Inputs](#multimodal-inputs)\n - [Multimodal Outputs](#multimodal-outputs)\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\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/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\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\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An integration package connecting Google's genai package and LangChain",
"version": "2.1.7",
"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": "7654159d13ef38dea4a16ecfb51ccc213912b1ac9fdbc2f84f556b7bca5e495e",
"md5": "0d769fb19b7fe21439f358d989e839dd",
"sha256": "df90736536d71d5a2eb3117ed9df9c86e10bcf03b51d883ee5d2a727771f4bc3"
},
"downloads": -1,
"filename": "langchain_google_genai-2.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0d769fb19b7fe21439f358d989e839dd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 47433,
"upload_time": "2025-07-09T15:38:56",
"upload_time_iso_8601": "2025-07-09T15:38:56.108050Z",
"url": "https://files.pythonhosted.org/packages/76/54/159d13ef38dea4a16ecfb51ccc213912b1ac9fdbc2f84f556b7bca5e495e/langchain_google_genai-2.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d2035ff9f00f942a3ea695a8e250b0ae681c50c8eacee21f716d7668a8bd0a82",
"md5": "0818f826a20a09990971cf5678953feb",
"sha256": "a0e77f06175843e527d7bcde195cc0332bf538416038e7de8e5a4bd256801181"
},
"downloads": -1,
"filename": "langchain_google_genai-2.1.7.tar.gz",
"has_sig": false,
"md5_digest": "0818f826a20a09990971cf5678953feb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 43917,
"upload_time": "2025-07-09T15:38:57",
"upload_time_iso_8601": "2025-07-09T15:38:57.164678Z",
"url": "https://files.pythonhosted.org/packages/d2/03/5ff9f00f942a3ea695a8e250b0ae681c50c8eacee21f716d7668a8bd0a82/langchain_google_genai-2.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-09 15:38:57",
"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"
}