Replace deprecated @app.on_event with lifespan context manager - #389
Open
Qiaochu Hu (hobostay) wants to merge 1 commit into
Open
Replace deprecated @app.on_event with lifespan context manager#389Qiaochu Hu (hobostay) wants to merge 1 commit into
Qiaochu Hu (hobostay) wants to merge 1 commit into
Conversation
FastAPI deprecated @app.on_event("startup") since version 0.103.0
and it will be removed in a future release. Replace with the
recommended lifespan context manager pattern.
This also prevents potential issues with auto-reload loading the
model multiple times into GPU memory.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Rick Thomas (rickthomasjr)
approved these changes
Jun 14, 2026
Rick Thomas (rickthomasjr)
left a comment
There was a problem hiding this comment.
Review: Approve — replace deprecated @app.on_event with lifespan
Correctness: @app.on_event("startup") and @app.on_event("shutdown") are deprecated in FastAPI 0.95+ in favor of the lifespan context manager pattern. This is a forward-compatibility fix.
Changes are correct: The async context manager properly yields between startup (model loading) and shutdown (model cleanup), matching the original semantics exactly.
Verdict: Approve. Non-breaking forward-compat fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@app.on_event("startup")with the recommendedlifespancontext manager pattern indemo/web/app.py@app.on_event("startup")is deprecated since FastAPI 0.103.0 and will be removed in a future versionDetails
Affected file:
demo/web/app.py(lines 335-356)The deprecated
on_event("startup")pattern has known issues with auto-reload where the startup event may fire multiple times, loading multiple copies of the large model into GPU memory and causing OOM. The lifespan context manager is the recommended replacement.Test plan
MODEL_PATHset and verify it loads correctly🤖 Generated with Claude Code