redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.12k stars 112 forks source link

Unable to change underlying model config #343

Open tylerhutcherson opened 2 years ago

tylerhutcherson commented 2 years ago

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 :)

sav-norem commented 2 years ago

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!

tylerhutcherson commented 2 years ago

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!