Skip to main content

Lazy, on-demand loading of FastAPI routers, mounted on the first matching request

Project description

fastapi-router-lazy

Latest Version MIT License Build Status Codecov semantic-release

Lazy, on-demand loading of FastAPI routers.

Large FastAPI applications pay for every router at startup: importing the module, building each route's dependency graph and response model, generating its OpenAPI schema. fastapi-router-lazy mounts a tiny stub per route and loads the real router only on the first request matching one of its paths.

The full startup win — unused router modules are never imported — comes from generating the route metadata ahead of time and shipping it, so the app reads that cache at startup instead of importing every module. The default in-process extractor is the simplest wiring, but it imports each module at startup to read its routes: it mounts on demand without deferring the imports. The documentation covers which strategy defers imports.

The core needs nothing but FastAPI and works with plain fastapi.APIRouter.

Install

pip install fastapi-router-lazy
# variant/version-aware extraction:
pip install "fastapi-router-lazy[variants]"

Usage

The examples below use "myapp" as the name of your importable Python package — the one the extractor walks to find modules named router.py, each exposing an APIRouter:

myapp/                  # <- the package name you pass to route_infos_extractor
├── __init__.py
├── users/
│   └── router.py       # exposes `router = APIRouter()`
└── items/
    └── router.py

Given that layout, lazy loading is two steps.

1. Generate the route metadata (at build time — imports each module once and writes routes.json):

from pathlib import Path

from fastapi_router_lazy import route_infos_extractor

route_infos_extractor("myapp", cache=True, cache_file=Path("routes.json"))

2. Run lazily (at runtime — reads the metadata and imports no router module until its first matching request):

from pathlib import Path

from fastapi import FastAPI

from fastapi_router_lazy import (
    RouterLoader,
    lazy_middleware_factory,
    route_infos_extractor,
)

app = FastAPI()

# strict=True: use the prebuilt metadata, never re-import at startup.
extractor = route_infos_extractor(
    "myapp", cache=True, cache_file=Path("routes.json"), strict=True
)
loader = RouterLoader(extractor, app)

middleware = lazy_middleware_factory(loader)
app.add_middleware(middleware)

# Register the stubs; real routers load on first matching request.
loader.load(middleware)

The default extractor (route_infos_extractor("myapp"), no cache) is the simplest wiring, but it imports every module at startup to read its routes — it mounts on demand without deferring imports. Eager loading, deployment filtering, and the variant/version-aware extractor ([variants]) are covered in the documentation.

Limitations

Lazy loading trades some runtime dynamism for startup speed:

  • lazily-declared routes are absent from /openapi.json and /docs until their first matching request mounts them (mount eagerly if you need a complete schema up front);
  • no prefix= is applied at include_router time — bake prefixes into the APIRouter itself;
  • a stub and a real route sharing a path are resolved by Starlette match order.

See the Limitations section of the docs for details.

Documentation

Full documentation is available at toilal.github.io/fastapi-router-lazy. A preview of the in-development develop branch is published at toilal.github.io/fastapi-router-lazy/dev/.

Requirements

Python 3.12+ and FastAPI.

License

MIT

CHANGELOG

v0.1.0 (2026-07-05)

  • Initial Release

Changelog entries are generated automatically by python-semantic-release from Conventional Commits on release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_router_lazy-0.1.0.tar.gz (133.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_router_lazy-0.1.0-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_router_lazy-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_router_lazy-0.1.0.tar.gz
  • Upload date:
  • Size: 133.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_router_lazy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5cb401fa787b06547ef1bd455befa265950f02b8457427a6b12fcff797ce8c4e
MD5 f0bf9897ec0e038c667c919561c46aa5
BLAKE2b-256 d0bcfffe0fa919828f40a49a5ab5e1f2fed10a42a54a96637d1fac50079f193f

See more details on using hashes here.

File details

Details for the file fastapi_router_lazy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_router_lazy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_router_lazy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f392818052a883a9022c99de390eb3d133121908a3cda6df7c52f5b5f6895709
MD5 461f142db566547e4b4152dd6e3219cf
BLAKE2b-256 9e136ef6a2ceb6c298aa70a75fc215a0d4f1a88739a81e2cc2fffe0e2c398a19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page