Marshmallow vs. Pydantic - Python’s 2 Best Libraries for Data Serialization and Validation

rw-book-cover

Metadata

Highlights

  • Pydantic is not a direct competitor to marshmallow, because the goal of Pydantic is to add validation to (schema-)objects throughout their lifetime. Pydantic basically applies dynamic type checking at run-time, e.g. when instantiating an object, at configurable levels. In contrast, marshmallow only applies type checking (including data validation) at specific points, whenever you call schema.load(), schema.dump() or schema.validate(). (View Highlight)

  • Be sure to closely study Pydantic’s manual, because there are some interesting caveats. For instance, Pydantic is actually not that pedantic when it comes to type matching. The call BookModel(title=1337, isbn=1234) works (no validation error is raised), even though 1337 is a number and not a string. Pydantic converts provided data where no (or little) loss would occur. To avoid this behavior, you have to annotate the attributes using strict types, e.g. pydantic.StrictStr instead of str, as documented here. (View Highlight)

  • If all you need is serialization and deserialization at specific points in your application, I recommend marshmallow. Pydantic is a good choice if you want type safety throughout the whole lifetime of your objects at run-time, better interoperability with standards, or require very good run-time performance. (View Highlight)

  • If you use marshmallow, you need to call a schema’s dump(obj) method. Note that this call will validate your data again, which may raise unexpected errors for objects you created manually (using their normal constructor, not using schema.load()), with actually invalid data. With Pydantic you call the o.dict() method on a model object o which inherits from pydantic.BaseModel, to get a nested dict. Note that this dict might still contain non-primitive types, such as datetime objects, which many converters (including Python’s json module) cannot handle. Consider using o.json() instead. (View Highlight)

  • Fortunately, both marshmallow and Pydantic offer support to actually rename fields dynamically. See here for marshmallow and here for Pydantic. In case of Pydantic, the linked docs just cover _de_serialization – for serialization you simply need to call o.dict(by_alias=True) instead of o.dict(). (View Highlight)


📂 Articles | Последнее изменение: 23.11.2024 16:34