How to load chat history in LangChain
Quick answer
In
LangChain, load chat history by using memory classes like ConversationBufferMemory or by persisting messages externally and reloading them into the memory. This enables maintaining context across chat sessions for models like gpt-4o.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain_openai langchain_community
Setup
Install the necessary LangChain packages and set your OpenAI API key as an environment variable.
- Run
pip install langchain_openai langchain_community - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install langchain_openai langchain_community Step by step
This example demonstrates loading chat history into ConversationBufferMemory and using it with ChatOpenAI to maintain context across messages.
import os
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory
# Initialize memory to store chat history
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Initialize chat model
chat = ChatOpenAI(model_name="gpt-4o", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
# Simulate loading previous chat history
previous_messages = [
{"role": "user", "content": "Hello, who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}
]
# Load previous messages into memory
memory.chat_memory.messages.extend(previous_messages)
# New user input
new_input = "Can you tell me who was the MVP?"
# Create messages for the model including history
messages = memory.chat_memory.messages + [{"role": "user", "content": new_input}]
# Get response
response = chat(messages=messages)
# Append new interaction to memory
memory.chat_memory.messages.append({"role": "assistant", "content": response.content})
print("Assistant:", response.content) output
Assistant: The MVP of the 2020 World Series was Corey Seager of the Los Angeles Dodgers.
Common variations
You can also load chat history from external sources like files or databases and then feed them into ConversationBufferMemory. For async usage, use ChatOpenAI with async methods. Different models like gpt-4o-mini or Anthropic's claude-3-5-sonnet-20241022 can be used similarly by adjusting imports and client initialization.
import os
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory
async def async_chat_with_history():
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
chat = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
# Load previous messages
memory.chat_memory.messages.extend([
{"role": "user", "content": "Hi!"},
{"role": "assistant", "content": "Hello! How can I help you today?"}
])
new_input = "What's the weather like?"
messages = memory.chat_memory.messages + [{"role": "user", "content": new_input}]
response = await chat.agenerate(messages=[messages])
print("Assistant:", response.generations[0][0].text)
# To run async function:
# import asyncio
# asyncio.run(async_chat_with_history()) output
Assistant: I'm sorry, I don't have real-time weather data, but you can check a weather website or app for the latest updates.
Troubleshooting
- If chat history does not appear to persist, ensure you are appending messages to the memory's
chat_memory.messageslist correctly. - For large histories, consider summarizing or truncating to avoid token limits.
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly.
Key Takeaways
- Use
ConversationBufferMemoryto load and maintain chat history in LangChain. - Append previous messages to memory before sending new inputs to preserve context.
- You can load chat history from external sources and feed it into LangChain memory.
- Async and different model variants are supported with similar patterns.
- Always manage token limits by truncating or summarizing long chat histories.