Home / File/ memory_assert.py — langchain Source File

memory_assert.py — langchain Source File

Architecture documentation for memory_assert.py, a python file in the langchain codebase. 10 imports, 0 dependents.

File python LangChainCore MessageInterface 10 imports 2 classes

Entity Profile

Dependency Diagram

graph LR
  3e70e0e1_ddc3_69e9_583b_0336ed48d360["memory_assert.py"]
  0029f612_c503_ebcf_a452_a0fae8c9f2c3["os"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 0029f612_c503_ebcf_a452_a0fae8c9f2c3
  fae55357_8e00_f092_b2c8_957d1841a180["tempfile"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> fae55357_8e00_f092_b2c8_957d1841a180
  996b2db9_46dd_901f_f7eb_068bafab4b12["time"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 996b2db9_46dd_901f_f7eb_068bafab4b12
  9cec496e_5583_62d2_b4a1_469bbdaf601f["collections"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 9cec496e_5583_62d2_b4a1_469bbdaf601f
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> feec1ec4_6917_867b_d228_b134d0ff8099
  31eab4ab_7281_1e6c_b17d_12e6ad9de07a["langchain_core.runnables"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 31eab4ab_7281_1e6c_b17d_12e6ad9de07a
  bc299d65_c03c_8892_3cc8_690b9de57e0f["langgraph.checkpoint.base"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> bc299d65_c03c_8892_3cc8_690b9de57e0f
  2ff5ef5d_5050_1ab2_e4f2_f72e391945a2["langgraph.checkpoint.memory"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 2ff5ef5d_5050_1ab2_e4f2_f72e391945a2
  e48e506c_5154_500e_aec3_6c908c408c7a["langgraph.checkpoint.serde.base"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> e48e506c_5154_500e_aec3_6c908c408c7a
  8974d549_3692_a8e9_9d95_4593c0e01f7d["langgraph.pregel._checkpoint"]
  3e70e0e1_ddc3_69e9_583b_0336ed48d360 --> 8974d549_3692_a8e9_9d95_4593c0e01f7d
  style 3e70e0e1_ddc3_69e9_583b_0336ed48d360 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import os
import tempfile
import time
from collections import defaultdict
from typing import Any

from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.base import (
    ChannelVersions,
    Checkpoint,
    CheckpointMetadata,
)
from langgraph.checkpoint.memory import InMemorySaver, PersistentDict
from langgraph.checkpoint.serde.base import (
    SerializerProtocol,
)
from langgraph.pregel._checkpoint import copy_checkpoint


class MemorySaverAssertImmutable(InMemorySaver):
    storage_for_copies: defaultdict[str, dict[str, dict[str, tuple[str, bytes]]]]

    def __init__(
        self,
        *,
        serde: SerializerProtocol | None = None,
        put_sleep: float | None = None,
    ) -> None:
        _, filename = tempfile.mkstemp()

        class TempfilePersistentDict(PersistentDict):
            def __init__(self, *args: Any, **kwargs: Any) -> None:
                super().__init__(*args, filename=filename, **kwargs)

        super().__init__(serde=serde, factory=TempfilePersistentDict)
        self.storage_for_copies = defaultdict(lambda: defaultdict(dict))
        self.put_sleep = put_sleep
        self.stack.callback(os.remove, filename)

    def put(
        self,
        config: RunnableConfig,
        checkpoint: Checkpoint,
        metadata: CheckpointMetadata,
        new_versions: ChannelVersions,
    ) -> RunnableConfig:
        if self.put_sleep:
            time.sleep(self.put_sleep)
        # assert checkpoint hasn't been modified since last written
        thread_id = config["configurable"]["thread_id"]
        checkpoint_ns = config["configurable"]["checkpoint_ns"]
        if saved := super().get(config):
            assert (
                self.serde.loads_typed(
                    self.storage_for_copies[thread_id][checkpoint_ns][saved["id"]]
                )
                == saved
            )
        self.storage_for_copies[thread_id][checkpoint_ns][checkpoint["id"]] = (
            self.serde.dumps_typed(copy_checkpoint(checkpoint))
        )
        # call super to write checkpoint
        return super().put(config, checkpoint, metadata, new_versions)

Domain

Subdomains

Dependencies

  • collections
  • langchain_core.runnables
  • langgraph.checkpoint.base
  • langgraph.checkpoint.memory
  • langgraph.checkpoint.serde.base
  • langgraph.pregel._checkpoint
  • os
  • tempfile
  • time
  • typing

Frequently Asked Questions

What does memory_assert.py do?
memory_assert.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, MessageInterface subdomain.
What does memory_assert.py depend on?
memory_assert.py imports 10 module(s): collections, langchain_core.runnables, langgraph.checkpoint.base, langgraph.checkpoint.memory, langgraph.checkpoint.serde.base, langgraph.pregel._checkpoint, os, tempfile, and 2 more.
Where is memory_assert.py in the architecture?
memory_assert.py is located at libs/langchain_v1/tests/unit_tests/agents/memory_assert.py (domain: LangChainCore, subdomain: MessageInterface, directory: libs/langchain_v1/tests/unit_tests/agents).

Analyze Your Own Codebase

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

Try Supermodel Free