mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 02:34:29 +00:00
## 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
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""
|
|
Add the event loop to the cache key, to prevent event loop closed errors.
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
from .in_memory_cache import InMemoryCache
|
|
|
|
|
|
class LLMClientCache(InMemoryCache):
|
|
|
|
def update_cache_key_with_event_loop(self, key):
|
|
"""
|
|
Add the event loop to the cache key, to prevent event loop closed errors.
|
|
If none, use the key as is.
|
|
"""
|
|
try:
|
|
try:
|
|
event_loop = asyncio.get_running_loop()
|
|
except RuntimeError:
|
|
event_loop = asyncio.new_event_loop()
|
|
|
|
stringified_event_loop = str(id(event_loop))
|
|
return f"{key}-{stringified_event_loop}"
|
|
except Exception: # handle other potential errors
|
|
return key
|
|
|
|
def set_cache(self, key, value, **kwargs):
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
return super().set_cache(key, value, **kwargs)
|
|
|
|
async def async_set_cache(self, key, value, **kwargs):
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
return await super().async_set_cache(key, value, **kwargs)
|
|
|
|
def get_cache(self, key, **kwargs):
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
|
|
return super().get_cache(key, **kwargs)
|
|
|
|
async def async_get_cache(self, key, **kwargs):
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
|
|
return await super().async_get_cache(key, **kwargs)
|