Skip to content

Fix asyncio get_event_loop deprecation warning #9478

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mucahitkayadan
Copy link

@mucahitkayadan mucahitkayadan commented Mar 23, 2025

Title

Fix asyncio get_event_loop() deprecation warning

Relevant issues

This addresses the same issue discussed in python/cpython#93453

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on (make test-unit)[https://docs.litellm.ai/docs/extras/contributing_code]
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
    Tested on Python 3.13.x with no deprecation warnings.

Type

🐛 Bug Fix

Changes

Updated the code to use asyncio.get_running_loop() with a fallback to asyncio.new_event_loop() when no loop is running, which follows the recommended pattern for modern Python versions.

  • Avoids the deprecation warning
  • Creates isolated event loops when needed
  • Maintains the same functionality without modifying thread state
  • Is compatible with Python 3.10 through 3.13

## Fix asyncio get_event_loop() deprecation warning

### Problem
The caching handler uses `asyncio.get_event_loop()` which raises a deprecation warning in Python 3.10+ and will be removed in future versions.

Error: "DeprecationWarning: There is no current event loop"

### Solution
Updated the code to use `asyncio.get_running_loop()` with a fallback to `asyncio.new_event_loop()` when no loop is running, which follows the recommended pattern for modern Python versions.

This approach:
- Avoids the deprecation warning
- Creates isolated event loops when needed
- Maintains the same functionality without modifying thread state
- Is compatible with Python 3.10 through 3.13

### Testing
Tested on Python 3.13.x with no deprecation warnings.

### Related Issues
This addresses the same issue discussed in python/cpython#93453
Copy link

vercel bot commented Mar 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 23, 2025 7:33am

@mucahitkayadan
Copy link
Author

As of Python 3.11 it will throw an error to call asyncio.get_event_loop when there is no running loop in the current thread. I updated it for Python 3.13. Tested locally.

@CLAassistant
Copy link

CLAassistant commented Apr 22, 2025

CLA assistant check
All committers have signed the CLA.

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