Adapters API Reference¶
Complete API reference for all available adapters.
PydanticAdapter¶
PydanticAdapter
¶
Bases: BasePydanticAdapter[T]
Adapter for persisting any pydantic-serializable type.
This is the "less safe" adapter that accepts any Python type that Pydantic can serialize. Unlike BaseModelAdapter (which is constrained to BaseModel types), this adapter can handle: - Pydantic BaseModel instances - Dataclasses (standard and Pydantic) - TypedDict - Primitive types (int, str, float, bool, etc.) - Collection types (list, dict, set, tuple, etc.) - Datetime and other common types
Types that serialize to dicts (BaseModel, dataclass, TypedDict, dict) are stored directly. Other types are wrapped in {"items": value} to ensure consistent dict-based storage.
_raise_on_validation_error
instance-attribute
¶
__init__
¶
Create a new PydanticAdapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_value
|
AsyncKeyValue
|
The KVStore to use. |
required |
pydantic_model
|
TypeForm[T]
|
The type to serialize/deserialize. Can be any pydantic-serializable type. |
required |
default_collection
|
str | None
|
The default collection to use. |
None
|
raise_on_validation_error
|
bool
|
Whether to raise a DeserializationError if validation fails during reads. Otherwise, calls will return None if validation fails. |
False
|
_check_needs_wrapping
¶
Check if a type needs to be wrapped in {"items": ...} for storage.
Types that serialize to dicts don't need wrapping. Other types do.
Returns:
| Type | Description |
|---|---|
bool
|
True if the type needs wrapping, False otherwise. |
_serializes_to_dict
¶
Check if a type serializes to a dict by inspecting the TypeAdapter's JSON schema.
This uses Pydantic's TypeAdapter.json_schema() to reliably determine the output structure. Types that produce a JSON object (schema type "object") are dict-serializable.
Uses a custom schema generator to skip fields that can't be represented in JSON schema (e.g., Callable fields), avoiding PydanticInvalidForJsonSchema errors.
Returns:
| Type | Description |
|---|---|
bool
|
True if the type serializes to a dict (JSON object), False otherwise. |
RaiseOnMissingAdapter¶
RaiseOnMissingAdapter
¶
Adapter around a KVStore that raises on missing values for get/get_many/ttl/ttl_many.
When raise_on_missing=True, methods raise MissingKeyError instead of returning None.
delete
async
¶
Delete a key-value pair from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete the value from. |
required |
collection
|
str | None
|
The collection to delete the value from. If no collection is provided, it will use the default collection. |
None
|
delete_many
async
¶
Delete multiple key-value pairs from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
The keys to delete the values from. |
required |
collection
|
str | None
|
The collection to delete keys from. If no collection is provided, it will use the default collection. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of keys deleted. |
get
async
¶
Retrieve a value by key from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to retrieve the value from. |
required |
collection
|
str | None
|
The collection to retrieve the value from. If no collection is provided, it will use the default collection. |
None
|
raise_on_missing
|
bool
|
Whether to raise a MissingKeyError if the key is not found. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The value associated with the key. If the key is not found, None will be returned. |
get_many
async
¶
Retrieve multiple values by key from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
The keys to retrieve the values from. |
required |
collection
|
str | None
|
The collection to retrieve keys from. If no collection is provided, it will use the default collection. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]] | list[dict[str, Any] | None]
|
The values for the keys, or [] if the key is not found. |
put
async
¶
Store a key-value pair in the specified collection with optional TTL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store the value in. |
required |
value
|
Mapping[str, Any]
|
The value to store. |
required |
collection
|
str | None
|
The collection to store the value in. If no collection is provided, it will use the default collection. |
None
|
ttl
|
SupportsFloat | None
|
The optional time-to-live (expiry duration) for the key-value pair. Defaults to no TTL. Note: The backend store will convert the provided format to its own internal format. |
None
|
put_many
async
¶
Store multiple key-value pairs in the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
The keys to store the values in. |
required |
values
|
Sequence[Mapping[str, Any]]
|
The values to store. |
required |
collection
|
str | None
|
The collection to store keys in. If no collection is provided, it will use the default collection. |
None
|
ttl
|
SupportsFloat | None
|
The optional time-to-live (expiry duration) for all key-value pairs. The same TTL will be applied to all items in the batch. Defaults to no TTL. Note: The backend store will convert the provided format to its own internal format. |
None
|
ttl
async
¶
Retrieve the value and TTL information for a key-value pair from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to retrieve the TTL information from. |
required |
collection
|
str | None
|
The collection to retrieve the TTL information from. If no collection is provided, it will use the default collection. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any] | None, float | None]
|
The value and TTL information for the key. If the key is not found, (None, None) will be returned. |
ttl_many
async
¶
Retrieve multiple values and TTL information by key from the specified collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Sequence[str]
|
The keys to retrieve the values and TTL information from. |
required |
collection
|
str | None
|
The collection to retrieve keys from. If no collection is provided, it will use the default collection. |
None
|