Skip to content

✨ Serialize JSON response with Pydantic (in Rust), when there's a Pydantic return type or response model#14962

Merged
tiangolo merged 7 commits intomasterfrom
serialize-pydantic
Feb 22, 2026
Merged

✨ Serialize JSON response with Pydantic (in Rust), when there's a Pydantic return type or response model#14962
tiangolo merged 7 commits intomasterfrom
serialize-pydantic

Conversation

@tiangolo
Copy link
Member

✨ Serialize JSON response with Pydantic (in Rust), when there's a Pydantic return type or response model

@tiangolo tiangolo added the feature New feature or request label Feb 22, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 22, 2026

Merging this PR will improve performance by ×2.1

⚡ 4 improved benchmarks
✅ 16 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_sync_return_large_dict_with_response_model 13.2 ms 7 ms +88.26%
test_sync_return_large_model_with_response_model 13.3 ms 7.1 ms +89.05%
test_async_return_large_dict_with_response_model 11.7 ms 5.6 ms ×2.1
test_async_return_large_model_with_response_model 11.8 ms 5.6 ms ×2.1

Comparing serialize-pydantic (880ad19) with master (1e78a36)

Open in CodSpeed

@tiangolo tiangolo changed the title ✨ Serialize JSON response in Rust, with Pydantic, when there's a Pydantic return type or response model ✨ Serialize JSON response with Pydantic (in Rust), when there's a Pydantic return type or response model Feb 22, 2026
@tiangolo tiangolo marked this pull request as ready for review February 22, 2026 16:05
@tiangolo tiangolo merged commit 590a5e5 into master Feb 22, 2026
39 checks passed
@tiangolo tiangolo deleted the serialize-pydantic branch February 22, 2026 16:07
@a-babarytskyi
Copy link

A bit stupid question maybe, but If I was already returning pydantic model and response model was that model, I will still get performance boost?

@tiangolo
Copy link
Member Author

Yes, you will 🚀

@pmsmanishchauhan
Copy link

@tiangolo currently we are using response_model will it also performance improvement ?

@router.get("/sample", response_model=Sample)
def sample_endpoint(
    *,
    db: Session = Depends(deps.get_db),
    store_id: int,
    user_id: Optional[int] = None,
):

@TheoPascoli
Copy link

@pmsmanishchauhan Yes it will

tiangolo added a commit that referenced this pull request Feb 23, 2026
…'s a Pydantic return type or response model (#14962)"

This reverts commit 590a5e5.
@dolfinus
Copy link
Contributor

Same but for parsing HTTP body: #13951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants