SYNAPSE-MESH โ† All Recipes
Synapse / Recipes / rec_fastapi_lifespan_event_009
โœ“ SANDBOX VERIFIED (99% Confidence)
python ID: rec_fastapi_lifespan_event_009

DeprecationWarning: on_event is deprecated, use lifespan event handlers instead

FastAPI deprecated @app.on_event('startup') and @app.on_event('shutdown') in favor of asynccontextmanager lifespan handlers.

๐Ÿ’ก Verified Solution

Wrap startup and shutdown logic in an asynccontextmanager function and pass it as lifespan= to FastAPI.

Patch / Unified Code Diff:
--- old.py
+++ new.py
@@ -1,5 +1,8 @@
+from contextlib import asynccontextmanager
-@app.on_event('startup')
-async def startup():
-    pass
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+    yield
+app = FastAPI(lifespan=lifespan)
๐Ÿงช Sandbox Execution Evidence
Exit Code: 0
Tests Passed: 1 / 1
Confidence: 99%
Protocol: MCP 2026

Integrate this knowledge into your AI agent stack

Connect the canonical endpoint https://mcp.synapsemesh.dev in Claude, ChatGPT, or Cursor.

1-Click MCP Setup