Skip to main content
Pre-release

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

fastapi-lazyrouter

ci

fastapi-lazyrouter improves the cold start of a FastAPI application that has hundreds of Pydantic models. FastAPI scans all models at startup and Pydantic must generate a schema for each of them. This operation takes time, especially on nested and complex models.

At some point, the loading time exceeds the allowed time for the host to start the application. For example, on AWS Lambda, the application must be able to start in less than 10 seconds.

fastapi-lazyrouter prevents FastAPI from scanning routes at startup by dynamically mounting routers as requests come in.

  • startup time reduced by up to 3x
  • minimal overhead on the first requests since only the used routers are loaded
  • import strategy is preserved
  • minimally intrusive
  • API documentation loading is preserved: the first request to /docs or /openapi.json will take longer because all routers need to be loaded (not implemented yet)

Getting started

pip install fastapi-lazyrouter

The latest version

You can find the latest version to ...

git clone https://github.com/FabienArcellier/fastapi-lazyrouter.git

Usage

Auto (Recommended)

Configure Pydantic model

from pydantic import BaseModel, ConfigDict

class CustomBaseModel(BaseModel):
    model_config = ConfigDict(defer_build=True)  # enable defer_build, otherwise there is no effect

Enable FastAPI router

import fastapi
import fastapi_lazyrouter

app = fastapi.FastAPI()
fastapi_lazyrouter.autoconfigure(app)

Manual usage

If you want to understand what is happening under the hood.

Configure Pydantic model

from pydantic import BaseModel, ConfigDict

class CustomBaseModel(BaseModel):
    model_config = ConfigDict(defer_build=True)  # enable defer_build, otherwise there is no effect

Replace ApiRouter with LazyApiRouter

from fastapi_lazyrouter import LazyApiRouter

router = LazyApiRouter(prefix="/items")

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

Add middleware to FastAPI

from fastapi import FastAPI
from fastapi_lazyrouter import LazyApiRouterMiddleware

app = FastAPI()
app.add_middleware(LazyApiRouterMiddleware)

Publish the library on pypi

The publication use a tag-based workflow to run the publication on github action. First, a developper tag a commit using alfred publish, then github action publish the library on pypi.

The alfred publish take the version number from pyproject.toml. The command will raise an error if the current branch is not master, if changes are in progress, or if the tag already exists, if the branch is not synchronized with remote branch

alfred publish

Run in gitpod

gitpod can be used as an IDE. You can load the code inside to try the code.

Open in Gitpod

Developper guideline

Add a dependency

bash poetry add requests

Install development environment

Use make to instanciate a python virtual environment in ./venv and install the python dependencies.

poetry install

Update release dependencies

Use make to instanciate a python virtual environment in ./venv and freeze dependencies version

poetry update update

Activate the python environment

When you setup the requirements, a venv directory on python 3 is created. To activate the venv, you have to execute :

source .venv/bin/activate

Run the continuous integration process

Before commit or send a pull request, you have to execute pylint to check the syntax of your code and run the unit tests to validate the behavior.

$ poetry run alfred ci

Contributors

  • Fabien Arcellier

License

MIT License

Copyright (c) 2026-2026 Fabien Arcellier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for fastapi-lazyrouter 0.1.0rc4

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-lazyrouter 0.1.0rc4
File Size Uploaded
fastapi_lazyrouter-0.1.0rc4.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-lazyrouter 0.1.0rc4
File Interpreter ABI Platform
fastapi_lazyrouter-0.1.0rc4-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / fastapi_lazyrouter-0.1.0rc4.tar.gz

Download URL fastapi_lazyrouter-0.1.0rc4.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3b3671f7fb26bf6d557d1a0472b9b20854ce7d3088b0b40c853350414aee7f01
BLAKE2b-256 checksum
How to use checksums
e0a23e037fc71b63912162de729c0bedfda263679209d018dbf19ba1e4936c3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.0 CPython/3.12.13 Linux/6.17.0-1010-azure

Release files / fastapi_lazyrouter-0.1.0rc4-py3-none-any.whl

Download URL fastapi_lazyrouter-0.1.0rc4-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f75c8cac9c73ffbcf924a08129f02bb21cbfbee4bf5b474ac3f9b24aca14334
BLAKE2b-256 checksum
How to use checksums
2935aefeebe219a06a2c84d0e3d9d60832602130ac2698f2b41f8515d860de8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.0 CPython/3.12.13 Linux/6.17.0-1010-azure
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