Code beginner · 3 min read

How to install LangChain in python

Direct answer
Install LangChain in Python using pip install langchain and import modules with from langchain_openai import ChatOpenAI for current SDK usage.

Setup

Install
bash
pip install langchain
Imports
python
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_core.prompts import ChatPromptTemplate

Examples

inpip install langchain
outSuccessfully installed langchain-0.2.0
infrom langchain_openai import ChatOpenAI client = ChatOpenAI(model_name='gpt-4o')
outChatOpenAI client initialized with model gpt-4o
infrom langchain_community.vectorstores import FAISS faiss_index = FAISS.load_local('index_path')
outFAISS vector store loaded from local path

Integration steps

  1. Run the command pip install langchain in your terminal to install the package.
  2. Import LangChain modules from langchain_openai and langchain_community as needed.
  3. Initialize LangChain clients or loaders in your Python code.
  4. Use LangChain classes and methods to build your AI application.
  5. Run your Python script to verify LangChain is installed and working.

Full code

python
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_core.prompts import ChatPromptTemplate

# Example: Initialize a ChatOpenAI client
client = ChatOpenAI(model_name='gpt-4o')
print(f"LangChain client initialized with model: {client.model_name}")
output
LangChain client initialized with model: gpt-4o

API trace

Request
json
N/A - installation and import do not send API requests
Response
json
N/A - installation and import do not receive API responses
ExtractN/A

Variants

Install with extras for OpenAI and FAISS

Use this to install LangChain with optional dependencies for OpenAI API integration and FAISS vector store support.

python
pip install langchain[openai,faiss]
Install LangChain with GPU support

Use this variant if you plan to run LangChain with GPU-accelerated models or vector stores.

python
pip install langchain[gpu]
Install LangChain in a virtual environment

Use this to isolate LangChain installation in a virtual environment to avoid dependency conflicts.

python
python -m venv env
source env/bin/activate
pip install langchain

Performance

LatencyN/A for installation; typical API calls with LangChain depend on underlying model latency (~800ms for gpt-4o).
CostN/A for installation; API usage costs depend on the model and provider used via LangChain.
Rate limitsN/A for installation; rate limits depend on the AI API provider (OpenAI, Anthropic, etc.).
  • Use concise prompts to reduce token usage.
  • Cache embeddings or vector store results to avoid repeated calls.
  • Batch requests when possible to optimize throughput.
ApproachLatencyCost/callBest for
Standard pip installN/AFreeBasic setup and development
Install with extrasN/AFreeFull feature set with OpenAI and FAISS support
Virtual environment installN/AFreeIsolated development environments

Quick tip

Always install LangChain using pip and import from langchain_openai or langchain_community to ensure compatibility with the latest SDK.

Common mistake

Beginners often import LangChain modules from deprecated paths like langchain.chat_models instead of langchain_openai, causing import errors.

Verified 2026-04 · gpt-4o
Verify ↗