▲ | BiteCode_dev 4 days ago | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Because they don't represent the same thing. Pydantic models represent your input, it's the result of the experience you expose to the outside world, and therefore comes with objectives and constraints matching this: - make it easy to provide - make it simple to understand - make it familiar - deal with security and authentication - be easily serializable through your communication layer On the other hand, internal representations have the goal to help you with your private calculations: - make it performant - make it work with different subsystems such as persistence, caching, queuing - provide convenience shortcuts or precalculations for your own benefits Sometimes they overlap, or the system is not big enough that it matters. But the more you get to big or old system, the less likely they will. However, I often pass around pydantic objects if I have them, and I do this until it becomes a problem. And I rarely reach that point. It's like using Python until you have performance problems. Practicality beasts premature optimization. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | JackSlateur 4 days ago | parent [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
My pydantic models represent a "Thing" (a concept or whatever), not an input You can translate many things into a Thing, model_validate will help you with that (with contextinfo etc) You can translate your Thing into multiple output format, with model_serialize In your model, you shall put every checks required to ensure that some input are, indeed, a Thing And from there, you can use this object everywhere, certain that this is, indeed, a Thing, and that it has all the properties that makes a thing a Thing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|