Skip to content

Fix FastAPI deprecation warnings by migrating to lifespan handlers #2402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

hafeezhmha
Copy link
Contributor

@hafeezhmha hafeezhmha commented Jun 10, 2025

Fix FastAPI deprecation warnings

Problem

Cog currently uses deprecated @app.on_event() handlers which cause deprecation warnings in FastAPI.

Solution

  • Migrated from @app.on_event("startup") and @app.on_event("shutdown") to FastAPI's lifespan handlers
  • Updated imports to include asynccontextmanager
  • No functional changes, just API modernization

Testing

  • Tested locally with cog predict
  • No deprecation warnings appear
  • Server startup/shutdown works correctly

Copy link
Member

@michaeldwan michaeldwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hafeezhmha! It looks like every file in the repo changed line endings. If you can get that sorted out we'll review!

@hafeezhmha hafeezhmha force-pushed the fix-fastapi-deprecation-warnings branch from 366cf0a to 4487e74 Compare June 10, 2025 16:15
@hafeezhmha hafeezhmha force-pushed the fix-fastapi-deprecation-warnings branch from 4487e74 to 07f0848 Compare June 10, 2025 16:20
@hafeezhmha
Copy link
Contributor Author

Thanks @hafeezhmha! It looks like every file in the repo changed line endings. If you can get that sorted out we'll review!

@michaeldwan Hi, can you have a look, I have sorted them out.

Signed-off-by: Hafeez <90968109+hafeezhmha@users.noreply.github.com>
@hafeezhmha
Copy link
Contributor Author

hafeezhmha commented Jun 10, 2025

Thanks @hafeezhmha! It looks like every file in the repo changed line endings. If you can get that sorted out we'll review!

@michaeldwan the PR works , there are no deprecation warnings anymore. Can you please test it out. The file already has some todos and fixes which i have kept as is. I have only updated the fastAPI part.

@hafeezhmha hafeezhmha requested a review from michaeldwan June 10, 2025 17:38
@michaeldwan
Copy link
Member

Awesome, thanks! I'll run the tests and take a look.

Signed-off-by: Hafeez <90968109+hafeezhmha@users.noreply.github.com>
@hafeezhmha
Copy link
Contributor Author

Awesome, thanks! I'll run the tests and take a look.

@michaeldwan Alright!

Signed-off-by: Hafeez <90968109+hafeezhmha@users.noreply.github.com>
Copy link
Member

@michaeldwan michaeldwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for the quick fixes!

@michaeldwan michaeldwan merged commit 5df702a into replicate:main Jun 10, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants