LangChain v0.2.x (Jan 2026 release cycle), Python 3.10+
Core Syntax
Instantiate a ChatModel and get a basic completion. Ensure OPENAI_API_KEY is set in your environment.
from langchain_openai import ChatOpenAI # Modern practice: specific integration imports
from langchain_core.messages import HumanMessage # Standard message types
# Initialize a chat model. Default model is typically gpt-3.5-turbo.
llm = ChatOpenAI(temperature=0.7) # Adjust creativity (0.0-1.0)
# Invoke the model with a simple message.
response = llm.invoke([
HumanMessage(content="What is the capital of France?") # Input as a list of messages
])
print(response.content) # Access the generated text content
Essential Patterns
Combine prompts and models using LangChain Expression Language (LCEL) for robust, composable chains.
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser # Parses model output to a string
# Define a chat prompt template with a system and human message.
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI assistant. Respond concisely."), # System instructions
("user", "{question}") # User input placeholder
])
# Create a simple chain: Prompt -> Model -> Output Parser.
# The '|' operator pipes the output of one component as input to the next.
chain = prompt | ChatOpenAI(temperature=0.3) | StrOutputParser()
# Invoke the chain with input variables.
response = chain.invoke({"question": "What is the largest ocean on Earth?"})
print(response)
Retrieval Augmented Generation (RAG)
Integrate external knowledge by retrieving relevant documents and passing them to the LLM.
import os
from langchain_community.document_loaders import TextLoader # Load documents from files
from langchain_text_splitters import RecursiveCharacterTextSplitter # Split text into chunks
from langchain_openai import OpenAIEmbeddings # Generate vector embeddings
from langchain_community.vectorstores import FAISS # In-memory vector store
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
# 1. Load and split documents
loader = TextLoader("data/example.txt") # Assume 'data/example.txt' exists with text
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
# 2. Create embeddings and vector store
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(splits, embeddings) # Create index from splits and embeddings
retriever = vectorstore.as_retriever() # Convert vector store to a retriever
# 3. Define RAG prompt
rag_prompt = ChatPromptTemplate.from_messages([
("system", "Answer the question based ONLY on the following context:\n{context}"),
("user", "{question}")
])
# 4. Construct RAG chain
# RunnablePassthrough allows passing input through, optionally mapping it.
# 'context' key is populated by the retriever.
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()} # Map input to retriever and question
| rag_prompt
| ChatOpenAI(temperature=0)
| StrOutputParser()
)
# Invoke the RAG chain.
# Ensure 'data/example.txt' contains information about "Python" for a meaningful response.
print(rag_chain.invoke("What is Python primarily used for?"))
Tools and Agents
Allow LLMs to interact with external systems or perform actions, extending their capabilities.
from langchain.agents import create_react_agent, AgentExecutor
from langchain_core.tools import tool # Modern decorator for defining tools
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
# Define a custom tool using the @tool decorator.
@tool
def get_current_weather(location: str) -> str:
"""Returns the current weather for a given location."""
if "london" in location.lower():
return "It's 10°C and cloudy in London."
elif "paris" in location.lower():
return "It's 15°C and sunny in Paris."
else:
return "Weather data not available for this location."
# List of tools available to the agent.
tools = [get_current_weather]
# Define the agent's prompt. ReAct agents require specific formatting.
# LangChain provides default prompts for common agent types.
agent_prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant that can use tools."),
("user", "{input}"),
("placeholder", "{agent_scratchpad}") # Essential for ReAct agent internal monologue
])
# Initialize the LLM for the agent's reasoning.
llm = ChatOpenAI(temperature=0)
# Create a ReAct agent.
agent = create_react_agent(llm, tools, agent_prompt)
# Create an AgentExecutor to run the agent.
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) # verbose for tracing agent steps
# Invoke the agent.
agent_executor.invoke({"input": "What is the weather in London?"})
Memory
Maintain conversational context across turns for stateful interactions.
from langchain.chains import ConversationChain # Simple chain for conversational memory
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory # Stores conversation history directly
# Initialize LLM and memory.
llm = ChatOpenAI(temperature=0.5)
memory = ConversationBufferMemory() # Default memory stores all messages
# Create a conversation chain with memory.
# The ConversationChain automatically handles prompt formatting for memory.
conversation = ConversationChain(
llm=llm,
memory=memory,
verbose=True # See the prompt being constructed with memory
)
# First turn
conversation.invoke("My favorite color is blue.")
# Second turn, the LLM remembers the previous statement.
response = conversation.invoke("What is my favorite color?")
print(response['response'])
Gotchas & Best Practices
1. Environment Variables: Always manage API keys securely via environment variables.
import os
# Recommended: Set this before running any LangChain code that uses OpenAI.
# os.environ["OPENAI_API_KEY"] = "sk-YOUR_ACTUAL_KEY"
# For production, use a secrets manager or .env files (with dotenv).
2. LCEL for Composability & Performance: Prefer LCEL (|) over older Chain classes for better streaming, batching, and async support.
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
# BAD: Older, less flexible chain composition
# from langchain.chains import LLMChain
# prompt = ChatPromptTemplate.from_template("Tell me a {adjective} joke.")
# llm_chain = LLMChain(prompt=prompt, llm=ChatOpenAI())
# llm_chain.invoke({"adjective": "funny"})
# GOOD: LCEL for modern, efficient chaining
chain = (
ChatPromptTemplate.from_template("Tell me a {adjective} joke.")
| ChatOpenAI(temperature=0.8)
| StrOutputParser()
)
print(chain.invoke({"adjective": "funny"}))
3. When to use LangChain vs. Plain LLM Calls:
- Use LangChain when:
- You need complex orchestration (multiple steps, conditional logic).
- Integrating with external data sources (RAG).
- Managing conversational memory.
- Using tools/agents for dynamic actions.
- Experimenting with different LLMs/components.
- Requiring robust logging, tracing, and observability (LangSmith).
- Use plain LLM API calls when:
- Simple, single-turn prompts without context or external data.
- Extreme latency requirements where framework overhead is critical.
- Direct control over every API parameter is paramount.
4. Token Management: Be mindful of context window limits and cost. Use RecursiveCharacterTextSplitter with appropriate chunk_size and chunk_overlap for RAG. Monitor token usage with callbacks or LangSmith.
Advanced Techniques
Leverage async, streaming, and batching for performance and responsiveness.
import asyncio
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(temperature=0.7)
prompt = ChatPromptTemplate.from_template("Tell me a short story about {topic}.")
chain = prompt | llm | StrOutputParser()
# Asynchronous invocation for non-blocking operations
async def async_invoke_example():
response = await chain.ainvoke({"topic": "a space cat"})
print(f"Async response: {response}\n")
# Streaming for real-time output (e.g., in a UI)
async def stream_example():
print("Streaming response:")
async for chunk in chain.astream({"topic": "a magical forest"}):
print(chunk, end="", flush=True)
print("\n")
# Batching for parallel processing of multiple inputs
async def batch_example():
inputs = [{"topic": "a robot chef"}, {"topic": "an underwater city"}]
responses = await chain.abatch(inputs)
print(f"Batch responses: {responses}\n")
# Run all examples
asyncio.run(async_invoke_example())
asyncio.run(stream_example())
asyncio.run(batch_example())
Quick Reference
- LCEL Operators:
|(pipe),.invoke(),.ainvoke(),.stream(),.astream(),.batch(),.abatch() - Core Components:
ChatOpenAI,ChatPromptTemplate,StrOutputParser,RunnablePassthrough - Retrieval:
TextLoader,RecursiveCharacterTextSplitter,OpenAIEmbeddings,FAISS,.as_retriever() - Agents:
@tooldecorator,create_react_agent,AgentExecutor - Memory:
ConversationBufferMemory,ConversationChain - Environment:
OPENAI_API_KEY(required for OpenAI models) - Version Note: LangChain
v0.2.xemphasizes LCEL (langchain_core.runnables) and specific integrations (langchain_openai,langchain_community). Olderlangchainpackage components are being refactored or deprecated.
References
This page is AI-assisted. References official documentation.