mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-06 02:48:13 +00:00
fix(cache): preserve dynamic cache value types
This commit is contained in:
parent
332e2ece44
commit
d4d676bdc5
1 changed files with 5 additions and 4 deletions
|
|
@ -15,7 +15,7 @@ import sys
|
|||
import threading
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Any, Final
|
||||
from typing import TYPE_CHECKING, Any, Final, TypeVar, cast
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from litellm.types.caching import RedisPipelineIncrementOperation
|
||||
|
|
@ -27,6 +27,7 @@ from litellm.constants import MAX_SIZE_PER_ITEM_IN_MEMORY_CACHE_IN_KB
|
|||
from .base_cache import BaseCache
|
||||
|
||||
DEFAULT_MAX_SIZE_IN_MEMORY: Final = 200
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
class InMemoryCache(BaseCache):
|
||||
|
|
@ -210,7 +211,7 @@ class InMemoryCache(BaseCache):
|
|||
return False
|
||||
|
||||
@staticmethod
|
||||
def _copy_cached_value(value: object) -> object:
|
||||
def _copy_cached_value(value: _T) -> _T:
|
||||
"""Return a read-isolated value without making the cache brittle.
|
||||
|
||||
In-memory cache entries can contain Pydantic models and mutable
|
||||
|
|
@ -223,11 +224,11 @@ class InMemoryCache(BaseCache):
|
|||
if not isinstance(value, (dict, list, set, tuple, frozenset, bytearray, BaseModel)):
|
||||
return value
|
||||
try:
|
||||
return copy.deepcopy(value)
|
||||
return cast(_T, copy.deepcopy(value))
|
||||
except Exception: # noqa: BLE001 - cache reads must tolerate non-copyable values
|
||||
return value
|
||||
|
||||
def _get_cache_value(self, key, *, copy_value: bool) -> object:
|
||||
def _get_cache_value(self, key, *, copy_value: bool):
|
||||
if key in self.cache_dict:
|
||||
if self.evict_element_if_expired(key):
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue