Home › Serializers › AutoSerializer
The AutoSerializer (serializer='auto') extends the default MessagePack serialization with Python-specific type detection and preservation. Use it when your cache is Python-only and you want types like sets, frozensets, datetime, UUID, and NumPy arrays to survive cache roundtrips intact.
Tip
If you're hitting tuple→list or set→list issues with the default serializer, serializer='auto' is the fix.
from cachekit import cache
@cache(serializer='auto', ttl=300)
def get_data():
return {"tags": {"admin", "user"}, "created": datetime.now()}
result = get_data()
# Sets preserved: isinstance(result["tags"], set) → True
# Datetime preserved: isinstance(result["created"], datetime) → TrueTypes that the default StandardSerializer (MessagePack) loses, but AutoSerializer preserves:
| Type | Default ("default") |
Auto ("auto") |
|---|---|---|
set |
→ list |
Preserved |
frozenset |
→ list |
Preserved |
datetime |
→ string | Preserved (ISO-8601 roundtrip) |
date / time |
→ string | Preserved |
UUID |
→ string | Preserved |
numpy.ndarray |
Not supported | Preserved (zero-copy binary) |
pandas.DataFrame |
Not supported | Preserved (columnar format) |
pandas.Series |
Not supported | Preserved |
Note
Tuples are not yet preserved by AutoSerializer — they still become lists through MessagePack. This is tracked in #78.
AutoSerializer uses type markers in the serialized data to preserve Python types:
# set {1, 2, 3} is serialized as:
{"__set__": True, "value": [1, 2, 3], "frozen": False}
# frozenset({1, 2, 3}) is serialized as:
{"__set__": True, "value": [1, 2, 3], "frozen": True}
# datetime is serialized as:
{"__datetime__": "2026-03-28T12:00:00+00:00"}These markers are Python-specific — other language SDKs (Rust, TypeScript, PHP) will see them as plain dicts, not as the original types.
Use serializer='auto' when:
- Your cache is Python-only (no cross-language SDK sharing)
- You need set, frozenset, or datetime type preservation
- You're caching NumPy arrays or pandas DataFrames
- Type fidelity matters more than cross-language compatibility
Use serializer='default' (the default) when:
- Multiple language SDKs share the same cache (Python + Rust + TypeScript)
- You only cache basic types (dicts, lists, strings, numbers)
- Cross-language interoperability is a requirement
AutoSerializer works with all backends — it's a serialization format choice, not a backend choice:
from cachekit import cache
# L1-only
@cache(backend=None, serializer='auto', ttl=300)
def fn(): return {1, 2, 3}
# Redis
@cache(serializer='auto', ttl=300)
def fn(): return {1, 2, 3}
# Memcached
@cache(backend=memcached_backend, serializer='auto', ttl=300)
def fn(): return {1, 2, 3}AutoSerializer explicitly rejects types it can't handle safely:
- Pydantic models: Use
.model_dump()first. See Pydantic guide. - ORM models (SQLAlchemy, Django): Convert to dict.
- Custom classes: Use
dataclasses.asdict()or implement a custom serializer.
- Default Serializer (StandardSerializer) — Cross-language MessagePack
- ArrowSerializer — Optimized for large DataFrames
- Serializer Overview — Decision matrix