ConversationChain Class — langchain Architecture
Architecture documentation for the ConversationChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 853b7df9_e3a3_92a2_3356_2d2935763bb7["ConversationChain"] 8d3a235d_a08f_2979_f52a_1772067dd1d3["LLMChain"] 853b7df9_e3a3_92a2_3356_2d2935763bb7 -->|extends| 8d3a235d_a08f_2979_f52a_1772067dd1d3 df646892_6a63_bc00_9455_be9f2246d36d["base.py"] 853b7df9_e3a3_92a2_3356_2d2935763bb7 -->|defined in| df646892_6a63_bc00_9455_be9f2246d36d 80f31d7d_b306_0e56_0ef2_2bef34e17254["is_lc_serializable()"] 853b7df9_e3a3_92a2_3356_2d2935763bb7 -->|method| 80f31d7d_b306_0e56_0ef2_2bef34e17254 9bad9e5d_6c49_720c_dcea_170da57f71ac["input_keys()"] 853b7df9_e3a3_92a2_3356_2d2935763bb7 -->|method| 9bad9e5d_6c49_720c_dcea_170da57f71ac 7b02e98d_b3d9_e0dd_2c16_67db7fa2c663["validate_prompt_input_variables()"] 853b7df9_e3a3_92a2_3356_2d2935763bb7 -->|method| 7b02e98d_b3d9_e0dd_2c16_67db7fa2c663
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/conversation/base.py lines 19–149
class ConversationChain(LLMChain):
"""Chain to have a conversation and load context from memory.
This class is deprecated in favor of `RunnableWithMessageHistory`. Please refer
to this tutorial for more detail: https://python.langchain.com/docs/tutorials/chatbot/
`RunnableWithMessageHistory` offers several benefits, including:
- Stream, batch, and async support;
- More flexible memory handling, including the ability to manage memory
outside the chain;
- Support for multiple threads.
Below is a minimal implementation, analogous to using `ConversationChain` with
the default `ConversationBufferMemory`:
```python
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
store = {} # memory is maintained outside the chain
def get_session_history(session_id: str) -> InMemoryChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
model = ChatOpenAI(model="gpt-3.5-turbo-0125")
chain = RunnableWithMessageHistory(model, get_session_history)
chain.invoke(
"Hi I'm Bob.",
config={"configurable": {"session_id": "1"}},
) # session_id determines thread
```
Memory objects can also be incorporated into the `get_session_history` callable:
```python
from langchain_classic.memory import ConversationBufferWindowMemory
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
store = {} # memory is maintained outside the chain
def get_session_history(session_id: str) -> InMemoryChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
memory = ConversationBufferWindowMemory(
chat_memory=store[session_id],
k=3,
return_messages=True,
)
assert len(memory.memory_variables) == 1
key = memory.memory_variables[0]
messages = memory.load_memory_variables({})[key]
store[session_id] = InMemoryChatMessageHistory(messages=messages)
return store[session_id]
model = ChatOpenAI(model="gpt-3.5-turbo-0125")
chain = RunnableWithMessageHistory(model, get_session_history)
chain.invoke(
"Hi I'm Bob.",
config={"configurable": {"session_id": "1"}},
) # session_id determines thread
```
Example:
```python
from langchain_classic.chains import ConversationChain
Extends
Source
Frequently Asked Questions
What is the ConversationChain class?
ConversationChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/conversation/base.py.
Where is ConversationChain defined?
ConversationChain is defined in libs/langchain/langchain_classic/chains/conversation/base.py at line 19.
What does ConversationChain extend?
ConversationChain extends LLMChain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free