Skip to content

no-fastapi-on-event

Deprecated FastAPI or Starlette on_event lifecycle registration.

Why

The on_event API is deprecated and splits related startup and shutdown state across callbacks; lifespan keeps acquisition and cleanup in one async context manager and is the supported lifecycle contract.

Fix

Define an async lifespan context manager and pass it as FastAPI(lifespan=...) or Starlette(lifespan=...).

Examples

Before — flagged Do not register startup through on_event
app/main.py
from fastapi import FastAPI
app = FastAPI()
@app.on_event("startup")
async def start() -> None:
pass
After — preferred Pair startup and shutdown in lifespan
app/main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
app = FastAPI(lifespan=lifespan)