serhii.net

In the middle of the desert you can say anything you want

12 Feb 2026

Pydantic dumping model in json mode with serializable objects

TL;DR pydanticmodel.model_dump(mode=>"json")

Serialization - Pydantic Validation

To dump a Pydantic model to a jsonable dict (that later might be part of another dict that will be dumped to disc through json.dump[s](..)):

settings.model_dump()
>>>
{'ds_pred_path': PosixPath('/home/sh/whatever')}
# json.dump...
json.dumps(settings.model_dump())
>>> TypeError: Object of type PosixPath is not JSON serializable

json.dumps(settings.model_dump(mode="json"))
# works!
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus