fix(sdk): avoid requiring Redis for streaming

Signed-off-by: amarrtech <272048731+amarrtech@users.noreply.github.com>
This commit is contained in:
amarrtech 2026-10-08 08:23:01 -07:00
parent f824d11a22
commit dbdb84d86a
2 changed files with 29 additions and 2 deletions

View file

@ -11,8 +11,6 @@ from datetime import datetime
from functools import reduce
from typing import TYPE_CHECKING, Any, Final, Literal, TypeAlias, TypeVar, cast
from redis.exceptions import RedisError
from litellm._internal_context import with_service_target
from litellm._logging import verbose_proxy_logger
from litellm.caching import RedisCache
@ -514,6 +512,8 @@ class RedisUpdateBuffer:
if self.redis_cache is None:
return
from redis.exceptions import RedisError
restore_configs: Final = (
(db_spend_update_transactions, REDIS_UPDATE_BUFFER_KEY),
(daily_spend_update_transactions, REDIS_DAILY_SPEND_UPDATE_BUFFER_KEY),

View file

@ -1,5 +1,8 @@
import builtins
import importlib.util
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Final
from unittest.mock import AsyncMock, MagicMock, patch
@ -10,6 +13,30 @@ from litellm.proxy.db.db_transaction_queue.redis_update_buffer import RedisUpdat
from litellm.proxy.proxy_server import ProxyStartupEvent
def test_importing_module_does_not_require_redis(monkeypatch: pytest.MonkeyPatch) -> None:
original_import = builtins.__import__
def import_without_redis(
name: str,
globals: dict[str, object] | None = None,
locals: dict[str, object] | None = None,
fromlist: tuple[str, ...] = (),
level: int = 0,
) -> object:
if name == "redis" or name.startswith("redis."):
raise ModuleNotFoundError("No module named 'redis'")
return original_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", import_without_redis)
module_path = Path(__file__).parents[5] / "litellm/proxy/db/db_transaction_queue/redis_update_buffer.py"
spec = importlib.util.spec_from_file_location("redis_update_buffer_without_redis", module_path)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
@pytest.fixture
def mock_redis_cache():
"""Create a mock RedisCache instance"""