LangChain with Google Gemini in Python: Your First Program Explained Line by Line
# Building Your First LangChain Application with Google Gemini
## What is LangChain?
LangChain is an open-source Python framework that helps developers build applications powered by Large Language Models (LLMs) such as Google Gemini, OpenAI GPT, Anthropic Claude, and others.
Instead of writing low-level API calls, LangChain provides reusable components for prompts, message handling, chains, document retrieval, memory, tools, and agents, making it easier to build intelligent AI applications.
In this article, we will start with a simple LangChain program that communicates with Google's Gemini model. The purpose is to understand the basic building blocks of a LangChain application before moving on to more advanced concepts.
---
# Installation
## Install Required Packages
Create a Python virtual environment (recommended), then install the required packages:
pip install langchain langchain-google-genai python-dotenv
The packages are used for the following purposes:
| Package | Purpose |
|---|---|
| `langchain` | Provides the LangChain framework and core abstractions. |
| `langchain-google-genai` | Provides integration between LangChain and Google's Gemini models. |
| `python-dotenv` | Loads environment variables from a `.env` file. |
# Configure the Gemini API Key
The application needs a Google Gemini API key to communicate with the Gemini model.
API keys should not be hardcoded directly in your Python source code. Instead, store the key in a `.env` file.
Create a file named with env extension
//env file
GEMINI_API_KEY=AQ.A**********************************************************Q
.env
Replace `your-gemini-api-key` with your actual Gemini API key.
It is also recommended to add `.env` to your `.gitignore` file so that the API key is not accidentally committed to a Git repository.
---
# Simple LangChain Program
The following example demonstrates the simplest way to communicate with
Google's Gemini model using LangChain. python code
//env file
GEMINI_API_KEY=AQ.A**********************************************************Q
Google's Gemini model using LangChain.
from dotenv import load_dotenvfrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_core.prompts import PromptTemplate# Load environment variablesload_dotenv()# Create the LLMllm = ChatGoogleGenerativeAI(model="gemini-2.5-flash")prompt = PromptTemplate.from_template("What is {topic}? Explain in {lines} lines.")# Create a chainchain = prompt | llm# Ask a questionresponse = chain.invoke({"topic": "LangChain","lines": "2"})print(response.content)
{"topic": "LangChain", "lines": "2"}
│▼PromptTemplate│▼"What is LangChain? Explain in 2 lines."│▼Gemini 2.5 Flash│▼AIMessage│▼response.content
What makes this a LangChain example?
These parts are specifically from LangChain:
from langchain_google_genai import ChatGoogleGenerativeAI
This gives you the Gemini chat model through LangChain.
from langchain_core.prompts import PromptTemplate
This gives you LangChain's prompt template.
Then:
prompt = PromptTemplate.from_template(
"What is {topic}? Explain in {lines} lines."
)
creates a reusable prompt.
And this is the most important LangChain part:
chain = prompt | llm
It creates a RunnableSequence:
PromptTemplate → Gemini
Finally:
response = chain.invoke({"topic": "LangChain","lines": "2"})
executes the chain.
Comments
Post a Comment