domains / ai-agents / stanfordnlp/dspy
Cache key serialization
When implementing caching for functions that accept complex objects (dictionaries with mixed types, Pydantic models, callables), ensure proper cache key generation by filtering out non-serializable objects and converting complex types to serializable formats.
When implementing caching for functions that accept complex objects (dictionaries with mixed types, Pydantic models, callables), ensure proper cache key generation by filtering out non-serializable objects and converting complex types to serializable formats.
Key requirements:
- Filter out callables: Exclude callable objects from cache key generation as they cannot be reliably serialized or hashed
- Convert Pydantic models: Transform Pydantic BaseModel instances to dictionaries using
.dict()or.schema()methods - Use consistent serialization: Apply deterministic JSON serialization with sorted keys for reliable cache key generation
Example implementation:
def cache_key(request: Dict[str, Any]) -> str:
# Transform Pydantic models and exclude unhashable objects
params = {
k: (v.dict() if isinstance(v, pydantic.BaseModel) else v)
for k, v in request.items()
if not callable(v)
}
return sha256(ujson.dumps(params, sort_keys=True).encode()).hexdigest()
This approach prevents serialization errors like “TypeError: