Implementation:Langchain ai Langchain BaseMemory
| Knowledge Sources | |
|---|---|
| Domains | Memory, Chain State Management |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
BaseMemory is a deprecated abstract base class that defines the interface for memory components in LangChain Chains, enabling state persistence across chain executions.
Description
BaseMemory is an abstract base class located in the langchain_classic package (formerly langchain v0.0.x). It extends both Serializable and Python's ABC, providing the foundational interface for memory management in Chains. Memory allows storing information about past executions and injecting that context into future runs, which is particularly useful for conversational chains that need to maintain conversation history.
This class has been deprecated since v0.3.3 and is scheduled for removal in LangChain v1.0.0. Users are directed to the migration guide for updated memory patterns.
Usage
Import this class when building custom memory implementations for legacy LangChain Chains. New projects should follow the migration guide at the LangChain documentation for current memory patterns.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/langchain/langchain_classic/base_memory.py - Lines: 1-117
Signature
@deprecated(since="0.3.3", removal="1.0.0", message=("..."))
class BaseMemory(Serializable, ABC):
model_config = ConfigDict(arbitrary_types_allowed=True)
@property
@abstractmethod
def memory_variables(self) -> list[str]: ...
@abstractmethod
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]: ...
async def aload_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]: ...
@abstractmethod
def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None: ...
async def asave_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None: ...
@abstractmethod
def clear(self) -> None: ...
async def aclear(self) -> None: ...
Import
from langchain_classic.base_memory import BaseMemory
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| inputs | dict[str, Any] |
Yes | The inputs to the chain, used by load_memory_variables and save_context.
|
| outputs | dict[str, str] |
Yes | The outputs of the chain, used by save_context.
|
Outputs
| Name | Type | Description |
|---|---|---|
| memory_variables | list[str] |
The string keys that this memory class adds to chain inputs. |
| load_memory_variables return | dict[str, Any] |
Key-value pairs of memory data given the text input to the chain. |
Abstract Methods
Subclasses must implement the following:
| Method | Return Type | Description |
|---|---|---|
memory_variables (property) |
list[str] |
Returns the string keys this memory class will add to chain inputs. |
load_memory_variables(inputs) |
dict[str, Any] |
Returns key-value pairs given the text input to the chain. |
save_context(inputs, outputs) |
None |
Saves the context of a chain run to memory. |
clear() |
None |
Clears memory contents. |
Async variants (aload_memory_variables, asave_context, aclear) have default implementations that delegate to their synchronous counterparts via run_in_executor.
Usage Examples
Basic Usage
from langchain_classic.base_memory import BaseMemory
from typing import Any
class SimpleMemory(BaseMemory):
memories: dict[str, Any] = dict()
@property
def memory_variables(self) -> list[str]:
return list(self.memories.keys())
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, str]:
return self.memories
def save_context(
self, inputs: dict[str, Any], outputs: dict[str, str]
) -> None:
pass
def clear(self) -> None:
pass
Related Pages
- Heuristic:Langchain_ai_Langchain_Warning_Deprecated_Langchain_Classic
- Deprecated since v0.3.3; see Memory Migration Guide