Olivier Mehani<p><strong>Pydantic for data validation and hydration</strong></p><p><a href="https://blog.narf.ssji.net/2025/03/30/pydantic-for-data-validation-and-hydration/" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">blog.narf.ssji.net/2025/03/30/</span><span class="invisible">pydantic-for-data-validation-and-hydration/</span></a></p><p>When using Python in any serious way, one quickly encounters Pydantic. While I’ve run into it in many frameworks, up to now, I never took the time to sit down and work out what it was doing, beyond something like “data models and validation based on type annotations”.</p><p>I have so far used dataclasses for my data models and data transfer objects (DTO). But it finally happened, and I needed finer validation on the data within my model. So I decided to take Pydantic for a spin myself, and see what it was doing for real.</p><p>tl;dr:</p><p>Pydantic _is_ data models and validation based on type annotations (amongst other things)</p><p>It’s a (almost) drop-in replacement for dataclasses</p><p>Type resolution and validation does add one order of magnitude over dataclasses, but it’s quite acceptable as a tradeoff for not having to write any bespoke validation code</p><p>Not only can it validate data, but it can also construct nested Pydantic objects, with support for polymorphism.</p><p>The pydantic.TypeAdapter exposes some lower-level mechanisms allowing to compose Pydantic behaviours as needed </p><p> […]</p><p><a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://blog.narf.ssji.net/tag/pydantic/" target="_blank">#Pydantic</a> <a rel="nofollow noopener" class="hashtag u-tag u-category" href="https://blog.narf.ssji.net/tag/python/" target="_blank">#Python</a></p>