This release is a pre-release and may not be stable for production use.
fastapi-pymongo
FastAPI integration for PyMongo:
PyObjectId: Pydantic-nativeObjectId. Validates fromObjectIdor hex string, serializes to a string in JSON mode (Python-mode dumps keep theObjectId, so documents round-trip throughinsert_one), correct OpenAPI/JSON schema.MongoSettings:pydantic-settingsbase class for Mongo URI and database name. Subclass to add app settings.mongo_lifespan/get_client/get_database: client lifecycle wired to FastAPI'slifespan, exposed viaDepends().beanie_lifespan(optional,fastapi-pymongo[beanie]): runs Beanie'sinit_beaniealongside the rest offastapi_pymongo, for an ODM on top.
Install
pip install fastapi-pymongo
Optional Beanie integration:
pip install "fastapi-pymongo[beanie]"
Examples
examples/quickstart: minimal CRUD app, raw PyMongo.examples/beanie: same CRUD app on Beanie. Verified against a real MongoDB instance (create/list/get/delete).
Development
pip install -e ".[dev]"
pytest # unit tests (integration and perf are deselected by default)
pytest -m perf # PyObjectId overhead benchmarks
ruff check src tests examples
mypy src
Integration tests need a running MongoDB; set MONGODB_URI (default
mongodb://localhost:27017). Run the full suite with a local server, for
example via mongodb-runner start --topology standalone.
Documentation
Sphinx docs (furo theme) under docs/source:
pip install -e ".[docs]"
sphinx-build -b html docs/source docs/build
open docs/build/index.html
References
- INTPYTHON-1087: [SPIKE] FastAPI integration library for MongoDB. This repo is the prototype (PYTHON-3372, epic, dev complete). DoD answers:
- Minimum setup?
AsyncMongoClientvia FastAPIlifespanandDepends(). Seemongo_lifespan/get_client/get_database,examples/quickstart. The official-template gap this package is an alternative path around: INTPYTHON-382, blocked. - Leverage FastAPI/Pydantic?
_idisn't a valid Python field name or a Pydantic/JSON-native type.PyObjectIdfixes serialization and schema.MongoSettingsreuses the same model pattern for config. This is the most common first-hour complaint in the FastAPI + MongoDB community (fastapi#9074, also requested upstream in pydantic-extra-types#133); this is the same pattern users already land on by hand, as a maintained, tested thin layer instead of copy-pasted snippets. Django-side precedent for a Pydantic-schema integration: INTPYTHON-565, backlog. - Thin wrapper feasible? Yes. Built and tested:
PyObjectId,MongoSettings,mongo_lifespan/get_client/get_database. - Quickstart CRUD prototype? Done.
examples/quickstart. - Packaging? Name
fastapi-pymongo, reserved on PyPI. - Beanie boundary? No first-party ODM. See
docs/source/reference.rstfor why (MongoDB's PyMODM precedent was paused and archived).beanie_lifespanruns Beanie'sinit_beaniealongside this package for anyone who wants one. Seeexamples/beanie, verified against real MongoDB.PyObjectIddoesn't apply to Beanie models, sinceDocumentalready provides its own ObjectId field (beanie.PydanticObjectId). - Client metadata? Set at client construction via
AsyncMongoClient(uri, driver=DriverInfo(name="fastapi-pymongo", version=...))inmongo_lifespan/beanie_lifespan. Verified against a real server ($currentOp.clientMetadata.driver.name). - Cost estimate? 0.
PyObjectId,MongoSettings,mongo_lifespan/get_client/get_database, the Beanie boundary, and client metadata are already built, tested, and published as an alpha (0.1.0a0on PyPI). No further implementation work is needed. (Pydantic/FastAPI version-compat friction at this boundary, precedent: PYTHON-5543, closed.)
- Minimum setup?
Metadata
Release files for fastapi-pymongo 0.1.0a3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_pymongo-0.1.0a3.tar.gz | 15.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_pymongo-0.1.0a3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.9 kB
Release files / fastapi_pymongo-0.1.0a3.tar.gz
| Download URL | fastapi_pymongo-0.1.0a3.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
047ed53bed8812e624fd3c101dc151084d3af5b89fd66f17ff98ab4dd78e7b90
|
|
BLAKE2b-256 checksum How to use checksums |
4f411d156b82cdf283cad83284e6c8c635c190d893d9f9750ba32ae5d5a15c19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency logRelease files / fastapi_pymongo-0.1.0a3-py3-none-any.whl
| Download URL | fastapi_pymongo-0.1.0a3-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b2c818aa1fb907f641f38097374695f12e777b02815517402f51ef1f7dd493f
|
|
BLAKE2b-256 checksum How to use checksums |
cdc92cb770edf94ad39595505d86998df609e129c6705807dec6c3a28542e69c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 13, 2026.
Transparency log