Open xDiaym opened 7 months ago
Hi @xDiaym ,
Thank you for opening this issue and for the valuable suggestion. Using Pydantic for serializing models to JSON sounds like a promising approach, but сould you please clarify how this approach would be more beneficial compared to our current method of using return vars(self) for the to_dict method? We're interested in understanding the specific advantages this change would bring to our project.
Sure. Here are the advantages and disadvantages of pydantic
May be useful in more code support (eg GPT5 release) Unfortunately this can be understood as a violation of YAGNI :(
class Role(BaseModel):
def to_dict(self) -> Dict:
return self.model_dump(exclude="user_id")
On my machine, the code serialized via pydantic was approx.15 times slower, which is sad, since pydanic was rewritten in rust :(
But we can use var(self)
and pydantic, which will give comparable speed and avoid writing __init__
methods (and makes this issue meaningless)
On the one hand, this may look like overengineering and a violation of the golden rule of design: “the more nodes in the system, the less reliable it is,” but on the other hand, I hope this approach can make it easier to maintain and read. Sorry, if I waste your time :(
Hey there @xDiaym !
Actually, I agree with you, let's try it. What do you think if you did this and I'll test it in my environment?
I'm looking forward to collaborate together!
Serializing models to json provided in a commit #baeb343 can be done using pydantic(see serialization chapter), which is already included in the dependencies here To avoid breaking backward compatibility, we can use something like
Pydantic can also provide more rigorous and easier-to-use model validation. For example, the API may change over time and some fields may need to be omitted.
I can implement this, but can't test it in my environment, so I opened issue instead of PR. :disappointed: