Home / Class/ BaseStore Class — langchain Architecture

BaseStore Class — langchain Architecture

Architecture documentation for the BaseStore class in stores.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0ebcc119_721f_59ac_fdad_f204128f9add["BaseStore"]
  f0969880_8c80_c0b9_0855_568e385cf51f["stores.py"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|defined in| f0969880_8c80_c0b9_0855_568e385cf51f
  b0304fd2_aa9d_cb86_5d0a_071c8af02f46["mget()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| b0304fd2_aa9d_cb86_5d0a_071c8af02f46
  2b123c53_c00f_8d62_f5b3_bc4284abdcea["amget()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| 2b123c53_c00f_8d62_f5b3_bc4284abdcea
  f2c282df_5ff0_596b_4902_34202dc91f40["mset()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| f2c282df_5ff0_596b_4902_34202dc91f40
  42492e44_4055_96e6_d459_77c90ee232e8["amset()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| 42492e44_4055_96e6_d459_77c90ee232e8
  814e50ab_809f_10c4_4f56_0cac298d59c6["mdelete()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| 814e50ab_809f_10c4_4f56_0cac298d59c6
  ed32f1a4_af11_2188_8f64_f8d37bd13ec6["amdelete()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| ed32f1a4_af11_2188_8f64_f8d37bd13ec6
  71f3c37a_5c31_eb54_b796_d14b5b7fa05c["yield_keys()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| 71f3c37a_5c31_eb54_b796_d14b5b7fa05c
  209b297b_10f4_b869_0579_6cd920be0ca0["ayield_keys()"]
  0ebcc119_721f_59ac_fdad_f204128f9add -->|method| 209b297b_10f4_b869_0579_6cd920be0ca0

Relationship Graph

Source Code

libs/core/langchain_core/stores.py lines 26–170

class BaseStore(ABC, Generic[K, V]):
    """Abstract interface for a key-value store.

    This is an interface that's meant to abstract away the details of different
    key-value stores. It provides a simple interface for getting, setting, and deleting
    key-value pairs.

    The basic methods are `mget`, `mset`, and `mdelete` for getting, setting, and
    deleting multiple key-value pairs at once. The `yield_keys` method is used to
    iterate over keys that match a given prefix.

    The async versions of these methods are also provided, which are meant to be used in
    async contexts. The async methods are named with an `a` prefix, e.g., `amget`,
    `amset`, `amdelete`, and `ayield_keys`.

    By default, the `amget`, `amset`, `amdelete`, and `ayield_keys` methods are
    implemented using the synchronous methods. If the store can natively support async
    operations, it should override these methods.

    By design the methods only accept batches of keys and values, and not single keys or
    values. This is done to force user code to work with batches which will usually be
    more efficient by saving on round trips to the store.

    Examples:
        ```python
        from langchain.storage import BaseStore


        class MyInMemoryStore(BaseStore[str, int]):
            def __init__(self) -> None:
                self.store: dict[str, int] = {}

            def mget(self, keys: Sequence[str]) -> list[int | None]:
                return [self.store.get(key) for key in keys]

            def mset(self, key_value_pairs: Sequence[tuple[str, int]]) -> None:
                for key, value in key_value_pairs:
                    self.store[key] = value

            def mdelete(self, keys: Sequence[str]) -> None:
                for key in keys:
                    if key in self.store:
                        del self.store[key]

            def yield_keys(self, prefix: str | None = None) -> Iterator[str]:
                if prefix is None:
                    yield from self.store.keys()
                else:
                    for key in self.store.keys():
                        if key.startswith(prefix):
                            yield key
        ```
    """

    @abstractmethod
    def mget(self, keys: Sequence[K]) -> list[V | None]:
        """Get the values associated with the given keys.

        Args:
            keys: A sequence of keys.

        Returns:
            A sequence of optional values associated with the keys.
                If a key is not found, the corresponding value will be `None`.
        """

    async def amget(self, keys: Sequence[K]) -> list[V | None]:
        """Async get the values associated with the given keys.

        Args:
            keys: A sequence of keys.

        Returns:
            A sequence of optional values associated with the keys.
                If a key is not found, the corresponding value will be `None`.
        """
        return await run_in_executor(None, self.mget, keys)

    @abstractmethod
    def mset(self, key_value_pairs: Sequence[tuple[K, V]]) -> None:
        """Set the values for the given keys.

Frequently Asked Questions

What is the BaseStore class?
BaseStore is a class in the langchain codebase, defined in libs/core/langchain_core/stores.py.
Where is BaseStore defined?
BaseStore is defined in libs/core/langchain_core/stores.py at line 26.

Analyze Your Own Codebase

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

Try Supermodel Free