Replies: 1 comment
-
|
Proof of concept: #13951 The drawback is that in case of body validation error, it is returned in JSON string form, not as parsed object: |
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
FastAPI provides options to serialize Pydantic models to JSON using multiple different libraries, including stdlib json, ujson, orjson:
https://fastapi.tiangolo.com/advanced/custom-response/
But deserialization of HTTP request body from JSON format is done using
json.loads:fastapi/fastapi/routing.py
Line 263 in 7b2631a
At the same time, Pydantic 2.x introduced methods TypeAdapter.validate_json(str | bytes) which uses Rust-bases JSON parser under the hood. It has higher performance than
json.loads,orjson.loadsandujson.loads:json_to_model_bench.py
Using
TypeAdapter.model_validate_json()is 2 times faster than usingjson.loadsand requires no additional dependencies.Is is possible to change FastAPI to deserialize JSON by default using this method?
Operating System
Linux
Operating System Details
Linux 6.15.8-1-MANJARO #1 SMP PREEMPT_DYNAMIC Thu, 24 Jul 2025 08:21:53 +0000 x86_64 GNU/Linu
FastAPI Version
0.115.14
Pydantic Version
2.11.7
Python Version
3.12.2
Additional Context
orjson==3.11.1
ujson==5.10.0
Beta Was this translation helpful? Give feedback.
All reactions