ConversationBufferMemory Class — langchain Architecture
Architecture documentation for the ConversationBufferMemory class in buffer.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD ca682b22_4749_ba1d_e8e0_7ad990524d2d["ConversationBufferMemory"] b010d3e5_8f8c_da0a_9a18_268dce3d2b0b["BaseChatMemory"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|extends| b010d3e5_8f8c_da0a_9a18_268dce3d2b0b c4f84787_1cbb_89d9_aa54_1423548ba11b["buffer.py"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|defined in| c4f84787_1cbb_89d9_aa54_1423548ba11b 6512a409_98bf_28ea_1ad1_e4b8653642f2["buffer()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 6512a409_98bf_28ea_1ad1_e4b8653642f2 06e5492b_ebcf_5cfc_d43d_223c5a8737d9["abuffer()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 06e5492b_ebcf_5cfc_d43d_223c5a8737d9 1be340ff_56f0_70f2_2300_ab1a64dcfdbc["_buffer_as_str()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 1be340ff_56f0_70f2_2300_ab1a64dcfdbc 0f091ef8_6c84_e83a_c0c7_9ab8380cc147["buffer_as_str()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 0f091ef8_6c84_e83a_c0c7_9ab8380cc147 1347512a_c369_8ed8_813e_475f45118f8a["abuffer_as_str()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 1347512a_c369_8ed8_813e_475f45118f8a c726410a_ca36_0e3a_16a0_04582396f656["buffer_as_messages()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| c726410a_ca36_0e3a_16a0_04582396f656 5285873b_7ec1_7f38_0070_06f46872839c["abuffer_as_messages()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| 5285873b_7ec1_7f38_0070_06f46872839c cc94e8d0_c7e1_6329_4ebf_8b6d25fce7d3["memory_variables()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| cc94e8d0_c7e1_6329_4ebf_8b6d25fce7d3 f613e0d9_66c4_3952_5069_78a0a345ac91["load_memory_variables()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| f613e0d9_66c4_3952_5069_78a0a345ac91 b63065cf_955a_98a6_6f1f_ad366312342e["aload_memory_variables()"] ca682b22_4749_ba1d_e8e0_7ad990524d2d -->|method| b63065cf_955a_98a6_6f1f_ad366312342e
Relationship Graph
Source Code
libs/langchain/langchain_classic/memory/buffer.py lines 21–88
class ConversationBufferMemory(BaseChatMemory):
"""A basic memory implementation that simply stores the conversation history.
This stores the entire conversation history in memory without any
additional processing.
Note that additional processing may be required in some situations when the
conversation history is too large to fit in the context window of the model.
"""
human_prefix: str = "Human"
ai_prefix: str = "AI"
memory_key: str = "history"
@property
def buffer(self) -> Any:
"""String buffer of memory."""
return self.buffer_as_messages if self.return_messages else self.buffer_as_str
async def abuffer(self) -> Any:
"""String buffer of memory."""
return (
await self.abuffer_as_messages()
if self.return_messages
else await self.abuffer_as_str()
)
def _buffer_as_str(self, messages: list[BaseMessage]) -> str:
return get_buffer_string(
messages,
human_prefix=self.human_prefix,
ai_prefix=self.ai_prefix,
)
@property
def buffer_as_str(self) -> str:
"""Exposes the buffer as a string in case return_messages is True."""
return self._buffer_as_str(self.chat_memory.messages)
async def abuffer_as_str(self) -> str:
"""Exposes the buffer as a string in case return_messages is True."""
messages = await self.chat_memory.aget_messages()
return self._buffer_as_str(messages)
@property
def buffer_as_messages(self) -> list[BaseMessage]:
"""Exposes the buffer as a list of messages in case return_messages is False."""
return self.chat_memory.messages
async def abuffer_as_messages(self) -> list[BaseMessage]:
"""Exposes the buffer as a list of messages in case return_messages is False."""
return await self.chat_memory.aget_messages()
@property
def memory_variables(self) -> list[str]:
"""Will always return list of memory variables."""
return [self.memory_key]
@override
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Return history buffer."""
return {self.memory_key: self.buffer}
@override
async def aload_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Return key-value pairs given the text input to the chain."""
buffer = await self.abuffer()
return {self.memory_key: buffer}
Extends
Source
Frequently Asked Questions
What is the ConversationBufferMemory class?
ConversationBufferMemory is a class in the langchain codebase, defined in libs/langchain/langchain_classic/memory/buffer.py.
Where is ConversationBufferMemory defined?
ConversationBufferMemory is defined in libs/langchain/langchain_classic/memory/buffer.py at line 21.
What does ConversationBufferMemory extend?
ConversationBufferMemory extends BaseChatMemory.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free