question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pydantic models with nested non-JSON-friendly field types cannot be serialized

See original GitHub issue

What 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:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cretzcommented, Nov 7, 2022

After team discussion what we’ll do is:

  • Add support for UUID and maybe a couple of other types at https://pydantic-docs.helpmanual.io/usage/types/, but not ambiguous ones like datetime (yet, even though it can be argued ISO 8601 is close to universal)
  • Do not support 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.
  • Add a sample with easy to take code to allow all Pydantic models to work via the slower JSON string approach
0reactions
cretzcommented, Nov 3, 2022

I am still gathering feedback on whether we accept the perf penalty of using json() instead of dict() (only to parse it back again). If Pydantic had json_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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Fields for Nested Pydantic Models? · Issue #63 - GitHub
This allows for the type checking of the object, the A class requires a serialized version of J in order for it to...
Read more >
How to resolve pydantic model is not JSON serializable
Here the problem is that pydantic models are not json serializable by default, in your case, you can call data.dict() to serialize a...
Read more >
Serialization — Piccolo 0.101.0 documentation - Read the Docs
Piccolo uses Pydantic internally to serialize and deserialize data. ... To populate a nested Pydantic model with data from the database:.
Read more >
Exporting models - pydantic
This is the primary way of converting a model to a dictionary. Sub-models will be recursively converted to dictionaries. Arguments: include : fields...
Read more >
Why I use attrs instead of pydantic - The Three of Wands
Both attrs and Pydantic are libraries that make writing classes ... Your Pydantic model cannot have a field named json , since that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found