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:
.dict()
or .schema()
methodsExample 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:
Enter the URL of a public GitHub repository