Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

fastapi-pymongo

FastAPI integration for PyMongo:

  • PyObjectId: Pydantic-native ObjectId. Validates from ObjectId or hex string, serializes to a string in JSON mode (Python-mode dumps keep the ObjectId, so documents round-trip through insert_one), correct OpenAPI/JSON schema.
  • MongoSettings: pydantic-settings base class for Mongo URI and database name. Subclass to add app settings.
  • mongo_lifespan / get_client / get_database: client lifecycle wired to FastAPI's lifespan, exposed via Depends().
  • beanie_lifespan (optional, fastapi-pymongo[beanie]): runs Beanie's init_beanie alongside the rest of fastapi_pymongo, for an ODM on top.

Install

pip install -e ".[dev]"

Examples

Development

pip install -e ".[dev]"
pytest

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? AsyncMongoClient via FastAPI lifespan and Depends(). See mongo_lifespan/get_client/get_database, examples/quickstart. The official-template gap this package is an alternative path around: INTPYTHON-382, blocked.
    • Leverage FastAPI/Pydantic? _id isn't a valid Python field name or a Pydantic/JSON-native type. PyObjectId fixes serialization and schema. MongoSettings reuses 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.rst for why (MongoDB's PyMODM precedent was paused and archived). beanie_lifespan runs Beanie's init_beanie alongside this package for anyone who wants one. See examples/beanie, verified against real MongoDB. PyObjectId doesn't apply to Beanie models — Document already provides its own ObjectId field (beanie.PydanticObjectId).
    • Client metadata? Set at client construction via AsyncMongoClient(uri, driver=DriverInfo(name="fastapi-pymongo", version=...)) in mongo_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.0a0 on PyPI). No further implementation work is needed. (Pydantic/FastAPI version-compat friction at this boundary, precedent: PYTHON-5543, closed.)

Metadata

Release files for fastapi-pymongo 0.1.0a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-pymongo 0.1.0a2
File Size Uploaded
fastapi_pymongo-0.1.0a2.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-pymongo 0.1.0a2
File Interpreter ABI Platform
fastapi_pymongo-0.1.0a2-py3-none-any.whl Python 3 none any Details

Total release size: 24.9 kB

Release files / fastapi_pymongo-0.1.0a2.tar.gz

Download URL fastapi_pymongo-0.1.0a2.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4e05e946ac2e618777efba4efb264f81615fb8dec6aac3c136db8a47461beb66
BLAKE2b-256 checksum
How to use checksums
ce636741495ad99d8c482c02cb331acc757ce489fbbed3af27866657930c64eb
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 8, 2026.

Transparency log

Release files / fastapi_pymongo-0.1.0a2-py3-none-any.whl

Download URL fastapi_pymongo-0.1.0a2-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b3ae12e07b0abcacd85f907b9dd45a3818f13a3d1bdd58aadd5e03a8cc80a79
BLAKE2b-256 checksum
How to use checksums
9ed714aff88d7c553464903b70b94752d5675ebc256c8a48b4621326dd11dc89
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 8, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page