8000 ✨ Add support for BSON response by WrldEngine · Pull Request #13760 · fastapi/fastapi · GitHub
[go: up one dir, main page]

Skip to content

✨ Add support for BSON response #13760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

WrldEngine
Copy link
@WrldEngine WrldEngine commented Jun 3, 2025

BSON Response Feature

Discussion: #13768

Summary

Implemented a custom Response class for supporting BSON as a response serialization format in FastAPI. This enables seamless integration for applications using BSON instead of JSON, commonly used in MongoDB-based services or performance-critical APIs.

Additionally

  1. updated .gitignore to include missing .lock files from various package managers.
  2. wrote unit tests tests/test_bson_response_class.py

Motivation

BSON is a binary-encoded serialization of JSON-like documents, designed to be more efficient in both size and speed for certain use cases. FastAPI currently does not provide built-in support for returning responses in BSON format. This addition provides a clean and reusable way to return application/bson responses.

Next Steps

A Rust wrapper is planned soon. The existing BSON utility library (currently in Python) will be rewritten in Rust to achieve improved performance and tighter memory control, especially important for high-throughput applications. The Rust implementation will be exposed via bindings for Python, allowing FastAPI to use this enhanced BSON handling behind the scenes.

Copy link
Contributor
github-actions bot commented Jun 4, 2025

📝 Docs preview for commit b22ef05 at: https://4d3d16dd.fastapitiangolo.pages.dev

Copy link
Contributor
github-actions bot commented Jun 4, 2025

📝 Docs preview for commit 6b3fc52 at: https://5853bf8f.fastapitiangolo.pages.dev

@WrldEngine WrldEngine changed the title feat: add bson in FastAPI and update .gitignore for pdm.lock & other … feat: add bson in FastAPI and update .gitignore for pdm.lock & add tests for bson serialization Jun 4, 2025
@YuriiMotov
Copy link
Contributor

Thanks for your interest in FastAPI!

To increase the chances of this PR being merged, please avoid mixing multiple changes in a single PR. It's best to follow the principle: one PR = one fix/feature.

Additionally, it would be helpful to include some context - for example, why you believe this change is important, and any related links to discussions or issues.

I couldn't find any existing discussions about introducing a BSONResponse class. It might be a good idea to start a discussion first to gather feedback from the community.

@WrldEngine
Copy link
Author

Thanks for your interest in FastAPI!

To increase the chances of this PR being merged, please avoid mixing multiple changes in a single PR. It's best to follow the principle: one PR = one fix/feature.

Additionally, it would be helpful to include some context - for example, why you believe this change is important, and any related links to discussions or issues.

I couldn't find any existing discussions about introducing a BSONResponse class. It might be a good idea to start a discussion first to gather feedback from the community.

Thank you for reply, so, am i right?
I need to:

  1. Describe more about this feature in comments
  2. Start disscussion and collect positive feedbacks from the community

@YuriiMotov
Copy link
Contributor
YuriiMotov commented Jun 4, 2025
  1. Describe more about this feature in comments

Better edit initial description

  1. Start disscussion and collect positive feedbacks from the community

Yes

Also, exclude and don't include any unrelated things. For example, editing .gitignore looks not necessary for this PR.

@WrldEngine

This comment was marked as resolved.

@YuriiMotov YuriiMotov changed the title feat: add bson in FastAPI and update .gitignore for pdm.lock & add tests for bson serialization ✨ Add support for BSON response Jun 5, 2025
@YuriiMotov YuriiMotov added the feature New feature or request label Jun 5, 2025
Copy link
Contributor
github-actions bot commented Jun 6, 2025

📝 Docs preview for commit 1bce814 at: https://b1f51cf3.fastapitiangolo.pages.dev

@wbarnha
Copy link
wbarnha commented Jun 15, 2025

I would appreciate support for BSON in FastAPI! I think having https://github.com/mongodb-labs/python-bsonjs as an optional dependency for faster BSON parsing would be nice to include in this PR, at least until the Rust implementation is ready.

@WrldEngine
Copy link
Author

I would appreciate support for BSON in FastAPI! I think having https://github.com/mongodb-labs/python-bsonjs as an optional dependency for faster BSON parsing would be nice to include in this PR, at least until the Rust implementation is ready.

Okay, no problem, but i think i want to make some benchmarks to compare bson & bsonjs with serialisation/deserialisation . As i was looking, this lib using c++ backend, so im sure about benchmark results

Copy link
Contributor

📝 Docs preview for commit 8e13a97 at: https://814d0fef.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit a39b768 at: https://93187171.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 5c6d791 at: https://7c0421eb.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 9fbd418 at: https://a4a27ceb.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 6d1d2ba at: https://f310b213.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit a4f95c4 at: https://462e0ed8.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 3187602 at: https://e2750fff.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 03f615c at: https://6d3df932.fastapitiangolo.pages.dev

Copy link
Contributor

📝 Docs preview for commit 3ea3686 at: https://864334d3.fastapitiangolo.pages.dev

@WrldEngine WrldEngine requested a review from dolfinus June 20, 2025 10:17
@WrldEngine
Copy link
Author
WrldEngine commented Jun 20, 2025

@dolfinus , I accidentally requested a review. Dear contributor, if you're not responsible for merges, please feel free to ignore this. The review above isn't meaningful — it's just a simple mistake caused by inattention and PDM's poor dependency management.

@WrldEngine
Copy link
Author

@YuriiMotov hi, how many likes should be in discussion for pr to merge to happen? Tests and other checks already passed successfully

@YuriiMotov
Copy link
Contributor

@YuriiMotov hi, how many likes should be in discussion for pr to merge to happen? Tests and other checks already passed successfully

Please be patient!
When time comes, we will take a look at that discussion and see whether this feature is supported by community or not.

We appreciate your interest in FastAPI, but we have a lot of PRs we need to review.
If you want to speed up this process, you can help us by reviewing PRs, this is very much appreciated!

"""
BSON response using the current Python bson library for data serialization.

Note: This is a temporary solution. Soon, a custom Rust wrapper will be implemented for BSON serialization/deserialization to improve performance and reliability.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this temporary solution should become a part of the framework? Why not make a dedicated fastapi-bson package which can be installed and imported by user if needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best that I delete that part in the comment. But again, back to the old topic of dependencies. According to your thinking, I should remove one of the two dependencies related to bson, but then look at orjson and ujson, in 99% of cases everyone would choose orjson because ujson has no superior features compared to it. That's why I decided to add two different dependency options too. Bson is not Json.

Otherwise it would be better to open a discussion on removing the ujson response.

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.

4 participants
0