redis-om-python
redis-om-python copied to clipboard
Unable to change underlying model config
Hi!
Working with a HashModel
that contains some bytes data (for Vector data fields).
I need to update the default jsonable_encoder
with a custom encoder for bytes
.
However, I noticed that my HashModel
would not override the Config
class from Pydantic. Example:
class MyModel(HashModel):
foo: str
bar: bytes
class Config:
json_encoders = {bytes: lambda bs: bs.hex()}
After looking through the src code a bit - I believe I found the culprit. When a model calls the .save()
method, it first converts itself to a dictionary and then does the encoding:
https://github.com/redis/redis-om-python/blob/a00a68b414d50b7a096a98f8d00ee10ddd8cd99f/aredis_om/model/model.py#L1320
The issue here is that the jsonable_encoder
method doesn't properly pass through the custom encoder when this happens. It only passes through custom encoders when the instance is of type BaseModel
:
https://github.com/redis/redis-om-python/blob/a00a68b414d50b7a096a98f8d00ee10ddd8cd99f/aredis_om/model/encoders.py#L72
I'd love to be able to change the underlying model config so that when the object is serialized, it works with special data types that need more care :)
Hi! So HashModel extends from BaseModel which means that if isinstance()
actually should pass - we'll take a look at some of the json_encoder stuff and check it out!
It looks like the model.save()
method converts to a dictionary first before it passes to the encoder. This may be ok. I think the piece that's missing here, and the big picture feature request, is support for the vector data types so we can use this library for https://redis.io/docs/stack/search/reference/vectors/
That said - I am ok if we want to just close this thread and use it as a reference for future work on supporting vectors!