-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
✨ 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
base: master
Are you sure you want to change the base?
Conversation
📝 Docs preview for commit b22ef05 at: https://4d3d16dd.fastapitiangolo.pages.dev |
📝 Docs preview for commit 6b3fc52 at: https://5853bf8f.fastapitiangolo.pages.dev |
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?
|
Better edit initial description
Yes Also, exclude and don't include any unrelated things. For example, editing |
This comment was marked as resolved.
This comment was marked as resolved.
📝 Docs preview for commit 1bce814 at: https://b1f51cf3.fastapitiangolo.pages.dev |
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 |
📝 Docs preview for commit 8e13a97 at: https://814d0fef.fastapitiangolo.pages.dev |
📝 Docs preview for commit a39b768 at: https://93187171.fastapitiangolo.pages.dev |
📝 Docs preview for commit 5c6d791 at: https://7c0421eb.fastapitiangolo.pages.dev |
📝 Docs preview for commit 9fbd418 at: https://a4a27ceb.fastapitiangolo.pages.dev |
📝 Docs preview for commit 6d1d2ba at: https://f310b213.fastapitiangolo.pages.dev |
📝 Docs preview for commit a4f95c4 at: https://462e0ed8.fastapitiangolo.pages.dev |
📝 Docs preview for commit 3187602 at: https://e2750fff.fastapitiangolo.pages.dev |
📝 Docs preview for commit 03f615c at: https://6d3df932.fastapitiangolo.pages.dev |
📝 Docs preview for commit 3ea3686 at: https://864334d3.fastapitiangolo.pages.dev |
@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. |
@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! We appreciate your interest in FastAPI, but we have a lot of PRs we need to review. |
""" | ||
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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
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.