mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-04 01:48:57 +00:00
* refactor(typing): clear basedpyright Any errors in proxy, router and core utils * refactor(typing): clear basedpyright Any errors in proxy server, logging and provider handlers * refactor(typing): clear basedpyright Any errors in types, proxy client and single-Any modules * refactor(typing): clear basedpyright Any errors in providers, responses and proxy endpoints * refactor(typing): clear basedpyright Any errors in key management, prometheus, gigachat and http handler * refactor(typing): clear basedpyright Any errors in a long-tail family sweep * refactor(typing): clear basedpyright Any errors in decode, guardrail and passthrough paths * refactor(typing): clear basedpyright Any errors in decode, realtime and management paths * refactor(typing): clear basedpyright Any errors in source-owned proxy, cache and provider paths * refactor(typing): clear basedpyright Any errors in wave 10 narrow deep pass * refactor(typing): clear basedpyright Any errors in wave 11 hotspot and route request pass * refactor(typing): clear basedpyright Any errors in wave 12 deep coupled pass * refactor(typing): clear basedpyright Any errors in wave 13 family pass * refactor(typing): clear basedpyright Any errors in wave 14 single-hotspot pass * refactor(typing): clear basedpyright Any errors in wave 15 single-hotspot pass * fix(typing): keep main's merged behavior typed after syncing with main * refactor(typing): share one int_of helper across batch, file and key endpoints * test: give mocked Anthropic calls a real message body The Anthropic chat transform now rejects a decoded body that is not a JSON object. Four tests mocked the HTTP client with a bare MagicMock, whose json() is never a real decoded response, so they now return a real Anthropic message. * fix(utils): keep FileTypes importable from litellm.utils The wave 14 pass dropped the now-unused FileTypes import, which also removed the litellm.utils.FileTypes attribute SDK users could import. Re-export it as a TypeAlias the same way TextChoices is. * fix: run real builtins instead of copied CPython errors and keep client SDK types Replace hand-copied CPython error sites with the real operations through small core_helpers wrappers, restore main's typed client SDK returns, and add tests that compare each helper's result and error with the builtin's * fix: raise instead of recursing in the typed narrowing helpers Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test: call the value directly so the helpers test collects on Python 3.10 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(typing): mark the three widened TypedDict fields read-only Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
"""
|
|
Add the event loop to the cache key, to prevent event loop closed errors.
|
|
"""
|
|
|
|
from typing import Final
|
|
|
|
from .evicted_client_closer import EvictedClientCloser, default_evicted_client_closer, running_loop_token
|
|
from .in_memory_cache import InMemoryCache
|
|
|
|
|
|
class LLMClientCache(InMemoryCache):
|
|
"""Cache for LLM HTTP clients (OpenAI, Azure, httpx, etc.).
|
|
|
|
An evicted client is never closed on the spot: a request handed the client
|
|
just before eviction is still using it, and closing it there raises
|
|
``RuntimeError: Cannot send a request, as the client has been closed.``
|
|
|
|
Nor can eviction be left to rely on garbage collection. The SDK clients are
|
|
reference cycles, so an evicted client and its open TCP connections survive
|
|
until a generational collection runs. Instead a client litellm created is
|
|
handed to ``EvictedClientCloser``, which closes it once a grace window has
|
|
passed. Clients the caller supplied are left untouched.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
max_size_in_memory: int | None = 200,
|
|
default_ttl: int | None = 600,
|
|
max_size_per_item: int | None = 1024,
|
|
evicted_client_closer: EvictedClientCloser | None = None,
|
|
) -> None:
|
|
super().__init__(
|
|
max_size_in_memory=max_size_in_memory,
|
|
default_ttl=default_ttl,
|
|
max_size_per_item=max_size_per_item,
|
|
)
|
|
self.evicted_client_closer = evicted_client_closer or default_evicted_client_closer
|
|
|
|
def _remove_key(self, key: object) -> None:
|
|
evicted: Final[object] = self.cache_dict.get(key)
|
|
super()._remove_key(key)
|
|
self.evicted_client_closer.schedule(evicted)
|
|
self.evicted_client_closer.reap()
|
|
|
|
def update_cache_key_with_event_loop(self, key: str) -> str:
|
|
"""
|
|
Add the event loop to the cache key, to prevent event loop closed errors.
|
|
If none, use the key as is.
|
|
"""
|
|
token: Final = running_loop_token()
|
|
return f"{key}-{token}" if token is not None else key
|
|
|
|
def _scoped_key(self, key: str, loop_bound: bool) -> str:
|
|
return self.update_cache_key_with_event_loop(key) if loop_bound else key
|
|
|
|
def set_cache(self, key: str, value: object, litellm_owned_client: bool = False, loop_bound: bool = True, **kwargs):
|
|
"""``litellm_owned_client`` marks a client litellm built, so it may be closed once evicted."""
|
|
if litellm_owned_client:
|
|
self.evicted_client_closer.mark_owned(value)
|
|
return super().set_cache(self._scoped_key(key, loop_bound), value, **kwargs)
|
|
|
|
async def async_set_cache(self, key: str, value: object, litellm_owned_client: bool = False, **kwargs):
|
|
if litellm_owned_client:
|
|
self.evicted_client_closer.mark_owned(value)
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
return await super().async_set_cache(key, value, **kwargs)
|
|
|
|
def get_cache(self, key: str, loop_bound: bool = True, **kwargs):
|
|
scoped_key: Final = self._scoped_key(key, loop_bound)
|
|
self.evicted_client_closer.reap()
|
|
|
|
return super().get_cache(scoped_key, **kwargs)
|
|
|
|
async def async_get_cache(self, key: str, **kwargs):
|
|
key = self.update_cache_key_with_event_loop(key)
|
|
|
|
return await super().async_get_cache(key, **kwargs)
|