Replies: 1 comment
-
|
I have the exact same question. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I would like to better understand how to optimize JSON serialization when using FastAPI with Pydantic 2. My particular application is often sending and/or receiving somewhat large JSON objects and we've observed that serializing and deserializing these objects can be a significant bottleneck. We were therefore excited to take advantage of Pydantic 2 improved performance in model validation and JSON serialization. That said, we're not observing the speed ups we expected and so we'd like to understand how to optimize things a bit.
The example above highlights a few things:
TypeAdapterfor serializing a collection ofBaseModels. In our testing, we've found this to be an important tool for accelerating JSON serialization (details below).I assume the main reason for the performance gap here is that in route
/a, data is flowingBaseModel->Dict->JSONwhereas in route/b, Pydantic is able to go straight fromBaseModel-JSON.Questions:
Details comparing
json.dumpstoTypeAdapter.dump_json:Operating System
Linux, macOS
Operating System Details
Test above was run on macOS but our production systems are all Linux/Docker
FastAPI Version
0.108.0
Pydantic Version
2.5.3
Python Version
Python 3.11.7
Additional Context
Related issues/docs:
Beta Was this translation helpful? Give feedback.
All reactions