Open Ezhvsalate opened 3 years ago
I recently ran into this issue myself! From your code snippet it appears that you may have tried the same fix and indicated that it didn't solve the issue. Here is an endpoint where I was encountering the same error:
@router.get("/details", response_model=PlayerSchema)
@cache()
def get_player_details(mlb_id: int, app = Depends(get_app)):
return get_player(mlb_id, app)
PlayerSchema
is a pydantic model class for a SQLAlchemy model class named Player
, and get_player
retrieves the requested Player
object from the database. This endpoint works perfectly, until the @cache
decorator is added and the FAILED_TO_CACHE_KEY
errors start to appear.
The fix I found was to manually convert the SQLAlchemy type to a dict
using the from_orm
method of the pydantic model class as shown below:
def convert_player_to_dict(player: Player) -> Dict[str, Union[str, int]]:
return PlayerSchema.from_orm(player).dict()
Player
is a SQLAlchemy model class, PlayerSchema
is a pydantic model class, and player
is an instance of Player
. I simply wrapped the get_player
function with convert_player_to_dict
as shown below and everything is being cached as expected:
@router.get("/details", response_model=PlayerSchema)
@cache()
def get_player_details(mlb_id: int, app = Depends(get_app)):
return convert_player_to_dict(get_player(mlb_id, app))
Let me know if this works!
@a-luna, thanks for your response.
I tried your type of fix too and it doesn't work for me: I get the following:
FAILED_TO_CACHE_KEY: Object of type <class 'dict'> is not JSON-serializable: key=cache:prj.api
.routes.dictionaries.bjcp_styles.get_bjcp_style_by_id(id=49895337-df68-4d07-819b-f08ebabfefaa)
After some hacking I found the reason in JSON encoder and modified a fastapi_redis_cache.util.BetterJsonEncoder
in such way:
from uuid import UUID
from enum import Enum
prom pydantic import BaseModel
class BetterJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return {"val": obj.strftime(DATETIME_AWARE), "_spec_type": str(datetime)}
elif isinstance(obj, date):
return {"val": obj.strftime(DATE_ONLY), "_spec_type": str(date)}
elif isinstance(obj, Decimal):
return {"val": str(obj), "_spec_type": str(Decimal)}
elif isinstance(obj, BaseModel):
return obj.dict()
elif isinstance(obj, UUID):
return str(obj)
elif isinstance(obj, Enum):
return str(obj.value)
else: # pragma: no cover
return super().default(obj)
So it can handle Enums and UUIDs, and we don't more need to use .dict() (it's included in encoder, but will cause Pydantic as dependency so make this decision yourself plz). Anyway I couldn't get rid of PydanticModel.from_orm() cause we don't know what pydantic class should be parsed from ORM model.
And one more thing: I saw your mapping with additional '_spec_type' fields but have no idea why it will be required in this case. Hope that will help :)
Hi everybody, I have the same issue and have to edit BetterJsonEncoder
and make it even more better!
Hello!
Just tried to use your package to cache frequent GET requests in my FastApi app.
Endpoint looks like this:
When I try to make requests I can see following in the logs:
FAILED_TO_CACHE_KEY: Object of type <class '....db.models.dictionaries.malt.Malt'> is not JSON-serializable: key=cache:mypkg.api.routes.dictionaries.malts.get_malt_by_id(id=7af9090c-dcb5-4778-b090-5b6890618566)
Pydantic schema looks like following (basically it is much more complex but I removed everything but name and still get same result.
My example seems to be quite the same to your example with User model from documentation. So why doesn't it work?