Storage¶
langchain-classic documentation
These docs cover the langchain-classic package. This package will be maintained for security vulnerabilities until December 2026. Users are encouraged to migrate to the langchain package for the latest features and improvements. See docs for langchain
langchain_classic.storage
¶
Implementations of key-value stores and storage helpers.
Module provides implementations of various key-value stores that conform to a simple key-value interface.
The primary goal of these storages is to support implementation of caching.
EncoderBackedStore
¶
Bases: BaseStore[K, V]
Wraps a store with key and value encoders/decoders.
Examples that uses JSON for encoding/decoding:
import json
def key_encoder(key: int) -> str:
return json.dumps(key)
def value_serializer(value: float) -> str:
return json.dumps(value)
def value_deserializer(serialized_value: str) -> float:
return json.loads(serialized_value)
# Create an instance of the abstract store
abstract_store = MyCustomStore()
# Create an instance of the encoder-backed store
store = EncoderBackedStore(
store=abstract_store,
key_encoder=key_encoder,
value_serializer=value_serializer,
value_deserializer=value_deserializer,
)
# Use the encoder-backed store methods
store.mset([(1, 3.14), (2, 2.718)])
values = store.mget([1, 2]) # Retrieves [3.14, 2.718]
store.mdelete([1, 2]) # Deletes the keys 1 and 2
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initialize an |
mget |
Get the values associated with the given keys. |
amget |
Async get the values associated with the given keys. |
mset |
Set the values for the given keys. |
amset |
Async set the values for the given keys. |
mdelete |
Delete the given keys and their associated values. |
amdelete |
Async delete the given keys and their associated values. |
yield_keys |
Get an iterator over keys that match the given prefix. |
ayield_keys |
Async get an iterator over keys that match the given prefix. |
__init__
¶
__init__(
store: BaseStore[str, Any],
key_encoder: Callable[[K], str],
value_serializer: Callable[[V], bytes],
value_deserializer: Callable[[Any], V],
) -> None
Initialize an EncodedStore.
| PARAMETER | DESCRIPTION |
|---|---|
store
|
The underlying byte store to wrap. |
key_encoder
|
Function to encode keys from type |
value_serializer
|
Function to serialize values from type |
value_deserializer
|
Function to deserialize bytes back to type V. |
mget
¶
amget
async
¶
mset
¶
amset
async
¶
mdelete
¶
mdelete(keys: Sequence[K]) -> None
Delete the given keys and their associated values.
| PARAMETER | DESCRIPTION |
|---|---|
keys
|
A sequence of keys to delete.
TYPE:
|
amdelete
async
¶
amdelete(keys: Sequence[K]) -> None
Async delete the given keys and their associated values.
| PARAMETER | DESCRIPTION |
|---|---|
keys
|
A sequence of keys to delete.
TYPE:
|
yield_keys
¶
ayield_keys
async
¶
ayield_keys(*, prefix: str | None = None) -> AsyncIterator[K] | AsyncIterator[str]
Async get an iterator over keys that match the given prefix.
| PARAMETER | DESCRIPTION |
|---|---|
prefix
|
The prefix to match.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
AsyncIterator[K] | AsyncIterator[str]
|
Keys that match the given prefix. |
LocalFileStore
¶
Bases: ByteStore
BaseStore interface that works on the local file system.
Examples:
Create a LocalFileStore instance and perform operations on it:
from langchain_classic.storage import LocalFileStore
# Instantiate the LocalFileStore with the root path
file_store = LocalFileStore("/path/to/root")
# Set values for keys
file_store.mset([("key1", b"value1"), ("key2", b"value2")])
# Get values for keys
values = file_store.mget(["key1", "key2"]) # Returns [b"value1", b"value2"]
# Delete keys
file_store.mdelete(["key1"])
# Iterate over keys
for key in file_store.yield_keys():
print(key) # noqa: T201
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Implement the |
mget |
Get the values associated with the given keys. |
mset |
Set the values for the given keys. |
mdelete |
Delete the given keys and their associated values. |
yield_keys |
Get an iterator over keys that match the given prefix. |
__init__
¶
__init__(
root_path: str | Path,
*,
chmod_file: int | None = None,
chmod_dir: int | None = None,
update_atime: bool = False,
) -> None
Implement the BaseStore interface for the local file system.
| PARAMETER | DESCRIPTION |
|---|---|
root_path
|
The root path of the file store. All keys are interpreted as paths relative to this root. |
chmod_file
|
Sets permissions for newly created files, overriding the
current
TYPE:
|
chmod_dir
|
Sets permissions for newly created dirs, overriding the
current
TYPE:
|
update_atime
|
Updates the filesystem access time (but not the modified time) when a file is read. This allows MRU/LRU cache policies to be implemented for filesystems where access time updates are disabled.
TYPE:
|