Marshmallow vs. Pydantic - Python’s 2 Best Libraries for Data Serialization and Validation
Metadata
- Author: Denis 2022-04-01 at 2:26 AM
- Full Title: Marshmallow vs. Pydantic - Python’s 2 Best Libraries for Data Serialization and Validation
- Category:#articles
- Document Tags: marshmallow pydantic
- Summary: An introduction and comparison of the Python libraries marshmallow vs. pydantic, which (de-) serialize data from and to Python objects and validate your data.
- URL: https://www.augmentedmind.de/2020/10/25/marshmallow-vs-pydantic-python/
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()
orschema.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 though1337
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 ofstr
, 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 usingschema.load()
), with actually invalid data. With Pydantic you call theo.dict()
method on a model objecto
which inherits frompydantic.BaseModel
, to get a nesteddict
. Note that thisdict
might still contain non-primitive types, such asdatetime
objects, which many converters (including Python’sjson
module) cannot handle. Consider usingo.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 ofo.dict()
. (View Highlight)
📂 Articles | Последнее изменение: 23.11.2024 16:34