Home / Class/ CombinedMemory Class — langchain Architecture

CombinedMemory Class — langchain Architecture

Architecture documentation for the CombinedMemory class in combined.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e7b5b56d_2836_1021_bdd4_75c127a0aa35["CombinedMemory"]
  1f107c9a_ba73_698c_b028_96ca5ade32ec["BaseMemory"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|extends| 1f107c9a_ba73_698c_b028_96ca5ade32ec
  48fb025a_9570_1ddd_d527_ec72c1ae6a6f["BaseChatMemory"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|extends| 48fb025a_9570_1ddd_d527_ec72c1ae6a6f
  eaaf9644_873e_b0bc_9b69_7024f87dcf64["combined.py"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|defined in| eaaf9644_873e_b0bc_9b69_7024f87dcf64
  3d7caaf7_1cc6_4d24_b1a0_6ce3018a5a74["_check_repeated_memory_variable()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| 3d7caaf7_1cc6_4d24_b1a0_6ce3018a5a74
  b56e91a8_d5f7_4641_02fb_dee7ba998eee["check_input_key()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| b56e91a8_d5f7_4641_02fb_dee7ba998eee
  fb860651_3ef7_82ee_c5df_cb34243bcb8b["memory_variables()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| fb860651_3ef7_82ee_c5df_cb34243bcb8b
  4a2e6218_9c03_0bcc_8ec7_3895a20b4fc9["load_memory_variables()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| 4a2e6218_9c03_0bcc_8ec7_3895a20b4fc9
  e32735e8_d6fd_1b1d_8205_d8cae45fb475["save_context()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| e32735e8_d6fd_1b1d_8205_d8cae45fb475
  cd22e3c5_c3b9_a274_4d1c_51dbfb68ba9b["clear()"]
  e7b5b56d_2836_1021_bdd4_75c127a0aa35 -->|method| cd22e3c5_c3b9_a274_4d1c_51dbfb68ba9b

Relationship Graph

Source Code

libs/langchain/langchain_classic/memory/combined.py lines 10–85

class CombinedMemory(BaseMemory):
    """Combining multiple memories' data together."""

    memories: list[BaseMemory]
    """For tracking all the memories that should be accessed."""

    @field_validator("memories")
    @classmethod
    def _check_repeated_memory_variable(
        cls,
        value: list[BaseMemory],
    ) -> list[BaseMemory]:
        all_variables: set[str] = set()
        for val in value:
            overlap = all_variables.intersection(val.memory_variables)
            if overlap:
                msg = (
                    f"The same variables {overlap} are found in multiple"
                    "memory object, which is not allowed by CombinedMemory."
                )
                raise ValueError(msg)
            all_variables |= set(val.memory_variables)

        return value

    @field_validator("memories")
    @classmethod
    def check_input_key(cls, value: list[BaseMemory]) -> list[BaseMemory]:
        """Check that if memories are of type BaseChatMemory that input keys exist."""
        for val in value:
            if isinstance(val, BaseChatMemory) and val.input_key is None:
                warnings.warn(
                    "When using CombinedMemory, "
                    "input keys should be so the input is known. "
                    f" Was not set on {val}",
                    stacklevel=5,
                )
        return value

    @property
    def memory_variables(self) -> list[str]:
        """All the memory variables that this instance provides."""
        """Collected from the all the linked memories."""

        memory_variables = []

        for memory in self.memories:
            memory_variables.extend(memory.memory_variables)

        return memory_variables

    def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, str]:
        """Load all vars from sub-memories."""
        memory_data: dict[str, Any] = {}

        # Collect vars from all sub-memories
        for memory in self.memories:
            data = memory.load_memory_variables(inputs)
            for key, value in data.items():
                if key in memory_data:
                    msg = f"The variable {key} is repeated in the CombinedMemory."
                    raise ValueError(msg)
                memory_data[key] = value

        return memory_data

    def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
        """Save context from this session for every memory."""
        # Save context for all sub-memories
        for memory in self.memories:
            memory.save_context(inputs, outputs)

    def clear(self) -> None:
        """Clear context from this session for every memory."""
        for memory in self.memories:
            memory.clear()

Frequently Asked Questions

What is the CombinedMemory class?
CombinedMemory is a class in the langchain codebase, defined in libs/langchain/langchain_classic/memory/combined.py.
Where is CombinedMemory defined?
CombinedMemory is defined in libs/langchain/langchain_classic/memory/combined.py at line 10.
What does CombinedMemory extend?
CombinedMemory extends BaseMemory, BaseChatMemory.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free