langchain.storage.encoder_backed.EncoderBackedStore¶

class langchain.storage.encoder_backed.EncoderBackedStore(store: BaseStore[str, Any], key_encoder: Callable[[K], str], value_serializer: Callable[[V], bytes], value_deserializer: Callable[[Any], V])[source]¶

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

Initialize an EncodedStore.

Methods

__init__(store, key_encoder, ...)

Initialize an EncodedStore.

amdelete(keys)

Delete the given keys and their associated values.

amget(keys)

Get the values associated with the given keys.

amset(key_value_pairs)

Set the values for the given keys.

ayield_keys(*[, prefix])

Get an iterator over keys that match the given prefix.

mdelete(keys)

Delete the given keys and their associated values.

mget(keys)

Get the values associated with the given keys.

mset(key_value_pairs)

Set the values for the given keys.

yield_keys(*[, prefix])

Get an iterator over keys that match the given prefix.

Parameters
  • store (BaseStore[str, Any]) –

  • key_encoder (Callable[[K], str]) –

  • value_serializer (Callable[[V], bytes]) –

  • value_deserializer (Callable[[Any], V]) –

__init__(store: BaseStore[str, Any], key_encoder: Callable[[K], str], value_serializer: Callable[[V], bytes], value_deserializer: Callable[[Any], V]) None[source]¶

Initialize an EncodedStore.

Parameters
  • store (BaseStore[str, Any]) –

  • key_encoder (Callable[[K], str]) –

  • value_serializer (Callable[[V], bytes]) –

  • value_deserializer (Callable[[Any], V]) –

Return type

None

async amdelete(keys: Sequence[K]) None¶

Delete the given keys and their associated values.

Parameters

keys (Sequence[K]) – A sequence of keys to delete.

Return type

None

async amget(keys: Sequence[K]) List[Optional[V]]¶

Get the values associated with the given keys.

Parameters

keys (Sequence[K]) – 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 type

List[Optional[V]]

async amset(key_value_pairs: Sequence[Tuple[K, V]]) None¶

Set the values for the given keys.

Parameters

key_value_pairs (Sequence[Tuple[K, V]]) – A sequence of key-value pairs.

Return type

None

async ayield_keys(*, prefix: Optional[str] = None) Union[AsyncIterator[K], AsyncIterator[str]]¶

Get an iterator over keys that match the given prefix.

Parameters

prefix (str) – The prefix to match.

Returns

An iterator over keys that match the given prefix.

This method is allowed to return an iterator over either K or str depending on what makes more sense for the given store.

Return type

Iterator[K | str]

mdelete(keys: Sequence[K]) None[source]¶

Delete the given keys and their associated values.

Parameters

keys (Sequence[K]) –

Return type

None

mget(keys: Sequence[K]) List[Optional[V]][source]¶

Get the values associated with the given keys.

Parameters

keys (Sequence[K]) –

Return type

List[Optional[V]]

mset(key_value_pairs: Sequence[Tuple[K, V]]) None[source]¶

Set the values for the given keys.

Parameters

key_value_pairs (Sequence[Tuple[K, V]]) –

Return type

None

yield_keys(*, prefix: Optional[str] = None) Union[Iterator[K], Iterator[str]][source]¶

Get an iterator over keys that match the given prefix.

Parameters

prefix (Optional[str]) –

Return type

Union[Iterator[K], Iterator[str]]