Pydantic models with nested non-JSON-friendly field types cannot be serialized
See original GitHub issueWhat are you really trying to do?
Use this as a param:
class MyClass(BaseModel):
field_a: list[UUID]
Describe the bug
Today we basically just call dict()
on this which then tries to json.dumps
that which it can’t because that is not a JSON-friendly dict. Unfortunately Pydantic does not provide a way to turn a model to a JSON-friendly dict. We cannot directly return the string result of json()
in our encoder because we need it for nested purposes (i.e. JSONEncoder.default
, not JSONEncoder.encode
which is only called at the top level, not nested).
The best solution right now I think is to accept anything that has a callable json
attribute and invoke it, then turn it back to Python object form via json.loads
. This is the only easy way I am aware but has an obvious performance penalty. Was also mentioned at https://stackoverflow.com/questions/65622045/pydantic-convert-to-jsonable-dict-not-full-json-string.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (5 by maintainers)
After team discussion what we’ll do is:
UUID
and maybe a couple of other types at https://pydantic-docs.helpmanual.io/usage/types/, but not ambiguous ones likedatetime
(yet, even though it can be argued ISO 8601 is close to universal)json()
from Pydantic models so we don’t have to parse right back out to dict for nested use. Rather we will wait until Pydantic supports JSON-able dicts.I am still gathering feedback on whether we accept the perf penalty of using
json()
instead ofdict()
(only to parse it back again). If Pydantic hadjson_dict()
this would be no problem.Until we either choose to support stringified JSON in models or Pydantic adds support for JSON-able dicts, a custom converter can be used.