LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicbase_memoryBaseMemory
    Classā—Since v1.0Deprecated

    BaseMemory

    Copy
    BaseMemory()

    Bases

    SerializableABC

    Attributes

    Methods

    Inherited fromSerializable(langchain_core)

    Attributes

    Alc_secretsAlc_attributes

    Methods

    Mis_lc_serializableMget_lc_namespace
    View source on GitHub
    M
    lc_id
    Mto_json
    Mto_json_not_implemented
    attribute
    model_config
    attribute
    memory_variables: list[str]
    method
    load_memory_variables
    method
    aload_memory_variables
    method
    save_context
    method
    asave_context
    method
    clear
    method
    aclear

    Abstract base class for memory in Chains.

    Memory refers to state in Chains. Memory can be used to store information about past executions of a Chain and inject that information into the inputs of future executions of the Chain. For example, for conversational Chains Memory can be used to store conversations and automatically add them to future model prompts so that the model has the necessary context to respond coherently to the latest input.

    Example:

    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

    The string keys this memory class will add to chain inputs.

    Return key-value pairs given the text input to the chain.

    Async return key-value pairs given the text input to the chain.

    Save the context of this chain run to memory.

    Async save the context of this chain run to memory.

    Clear memory contents.

    Async clear memory contents.