Usage

Manual configuration

If you prefer to be explicit about which routers are lazy, you can use the manual setup.

Step 1: defer Pydantic schema generation

Same as in the auto configuration:

from pydantic import BaseModel, ConfigDict

class CustomBaseModel(BaseModel):
    model_config = ConfigDict(defer_build=True)

Step 2: replace APIRouter with LazyApiRouter

Import LazyApiRouter instead of APIRouter in your route modules:

from fastapi_lazyrouter import LazyApiRouter

router = LazyApiRouter(prefix="/items")

@router.get("/")
def list_items():
    return []

Step 3: add the middleware

Register LazyApiRouterMiddleware on your FastAPI application. The middleware must be added before any other middleware that relies on routing information.

from fastapi import FastAPI
from fastapi_lazyrouter import LazyApiRouterMiddleware

app = FastAPI()
app.add_middleware(LazyApiRouterMiddleware)

app.include_router(router)

What to expect at runtime

  • Cold start – the application boots without scanning any lazy-router routes, so startup time is dramatically reduced.

  • First request – when a request arrives whose path matches a lazy router prefix, the middleware triggers the mount. That request incurs a small one-time overhead for building the Pydantic schemas of that router only.

  • Subsequent requests – the router is now a normal FastAPI router; all later requests are processed at full speed.

  • OpenAPI / docs – the first call to /docs or /openapi.json may take longer because FastAPI needs to force-load every lazy router in order to generate the complete schema.