The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_fix-config-update-targeted-upserts
Yuneng Jiang650821b538e2
diff --cc tests/test_litellm/proxy/test_proxy_server.pyindex 1924b37dfdd,37e53005650..465ce579e09--- a/tests/test_litellm/proxy/test_proxy_server.py+++ b/tests/test_litellm/proxy/test_proxy_server.py@@@ -5473,197 -5505,335 +5505,528 @@@ async def test_reseed_warms_cache_even_ps.prisma_client = orig_prisma+# -----------------------------------------------------------------------------+# /config/update — critical paths only.+#+# These exercise the four behaviors that broke or changed in the rewrite of+# update_config (litellm/proxy/proxy_server.py): targeted per-section writes,+# the removal of the store_model_in_db gate, env var encryption, and the+# success_callback / litellm_settings merge semantics. All other branches+# (auth, missing-DB, slack auto-enable, router_settings merge) are covered+# implicitly or by upstream tests.+# -----------------------------------------------------------------------------+++class _FakeRow:+ def __init__(self, param_name, param_value):+ self.param_name = param_name+ self.param_value = param_value+++class _FakeLitellmConfig:+ def __init__(self, initial_rows=None):+ self.rows = dict(initial_rows or {})+ self.upsert_calls: list = []+ self.find_first = AsyncMock(side_effect=self._find_first)+ self.upsert = AsyncMock(side_effect=self._upsert)++ async def _find_first(self, where=None):+ if where and "param_name" in where:+ name = where["param_name"]+ if name in self.rows:+ return _FakeRow(name, self.rows[name])+ return None++ async def _upsert(self, where=None, data=None):+ name = where["param_name"]+ raw = data["update"]["param_value"]+ value = json.loads(raw) if isinstance(raw, str) else raw+ self.rows[name] = value+ self.upsert_calls.append((name, value))+++class _FakePrismaClient:+ def __init__(self, initial_rows=None):+ self.db = mock.MagicMock()+ self.db.litellm_config = _FakeLitellmConfig(initial_rows=initial_rows)+ self.jsonify_object = lambda obj: obj+++def _update_config_setup(monkeypatch):+ """Install fakes for the /config/update endpoint and return (client, prisma)."""+ from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth+ from litellm.proxy.auth.user_api_key_auth import user_api_key_auth as auth_dep++ def _install(initial_rows=None, store_model_in_db=True):+ prisma = _FakePrismaClient(initial_rows=initial_rows)+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", prisma)+ monkeypatch.setattr(+ "litellm.proxy.proxy_server.store_model_in_db", store_model_in_db+ )+ monkeypatch.setattr(+ "litellm.proxy.proxy_server.encrypt_value_helper",+ lambda value, **_: f"enc:{value}",+ )+ monkeypatch.setattr(+ "litellm.proxy.proxy_server.invalidate_config_param",+ AsyncMock(return_value=None),+ )+ from litellm.proxy.proxy_server import proxy_config as real_proxy_config++ monkeypatch.setattr(+ real_proxy_config, "add_deployment", AsyncMock(return_value=None)+ )++ original_overrides = app.dependency_overrides.copy()+ app.dependency_overrides[auth_dep] = lambda: UserAPIKeyAuth(+ user_id="test_admin",+ user_role=LitellmUserRoles.PROXY_ADMIN,+ api_key="sk-1234",+ )+ client = TestClient(app)++ def _restore():+ app.dependency_overrides = original_overrides++ return client, prisma, _restore++ return _install+++def test_update_config_writes_only_sent_section(_update_config_setup):+ """A request that only touches general_settings must not write any other+ section row, and must leave previously-written rows byte-identical."""+ client, prisma, restore = _update_config_setup(+ initial_rows={+ "litellm_settings": {"drop_params": True},+ "environment_variables": {"FOO": "enc:bar"},+ }+ )+ try:+ resp = client.post(+ "/config/update",+ json={"general_settings": {"store_prompts_in_spend_logs": True}},+ )+ assert resp.status_code == 200+ written = {name for name, _ in prisma.db.litellm_config.upsert_calls}+ assert written == {"general_settings"}+ assert prisma.db.litellm_config.rows["litellm_settings"] == {+ "drop_params": True+ }+ assert prisma.db.litellm_config.rows["environment_variables"] == {+ "FOO": "enc:bar"+ }+ finally:+ restore()+++def test_update_config_can_flip_store_model_in_db_when_currently_false(+ _update_config_setup,+):+ """The endpoint used to refuse all writes when store_model_in_db was+ False, blocking the very request that would flip it to True."""+ client, prisma, restore = _update_config_setup(store_model_in_db=False)+ try:+ resp = client.post(+ "/config/update", json={"general_settings": {"store_model_in_db": True}}+ )+ assert resp.status_code == 200+ assert (+ prisma.db.litellm_config.rows["general_settings"]["store_model_in_db"]+ is True+ )+ finally:+ restore()+++def test_update_config_environment_variables_encrypted_before_write(+ _update_config_setup,+):+ """env var values must be encrypted before they hit the DB row."""+ client, prisma, restore = _update_config_setup()+ try:+ resp = client.post(+ "/config/update",+ json={"environment_variables": {"OPENAI_API_KEY": "sk-secret"}},+ )+ assert resp.status_code == 200+ stored = prisma.db.litellm_config.rows["environment_variables"]+ assert stored == {"OPENAI_API_KEY": "enc:sk-secret"}+ finally:+ restore()+++def test_update_config_litellm_settings_request_wins_for_non_callback_keys(+ _update_config_setup,+):+ """Sending {"drop_params": False} when the row holds drop_params: True+ must persist False (request wins). Untouched keys preserved."""+ client, prisma, restore = _update_config_setup(+ initial_rows={+ "litellm_settings": {"drop_params": True, "set_verbose": True},+ }+ )+ try:+ resp = client.post(+ "/config/update", json={"litellm_settings": {"drop_params": False}}+ )+ assert resp.status_code == 200+ stored = prisma.db.litellm_config.rows["litellm_settings"]+ assert stored["drop_params"] is False+ assert stored["set_verbose"] is True+ finally:+ restore()+++def test_update_config_success_callback_normalizes_existing_mixed_case(+ _update_config_setup,+):+ """Existing mixed-case callback names (written elsewhere) must be+ normalized to lowercase before union, otherwise the union dedup misses+ against the lowercase incoming entry and delete_callback (lowercase+ lookup) cannot find the original."""+ client, prisma, restore = _update_config_setup(+ initial_rows={"litellm_settings": {"success_callback": ["Langfuse", "SQS"]}}+ )+ try:+ resp = client.post(+ "/config/update",+ json={"litellm_settings": {"success_callback": ["langfuse"]}},+ )+ assert resp.status_code == 200+ stored = prisma.db.litellm_config.rows["litellm_settings"]["success_callback"]+ assert set(stored) == {"langfuse", "sqs"}+ finally:+ restore()+++++ # ---------------------------------------------------------------------------+ # Lazy feature loading (LazyFeatureMiddleware) — verifies that optional+ # routers are NOT imported at module load and ARE imported on first request+ # to a matching path prefix. The same module isn't re-imported on subsequent+ # requests.+ # ---------------------------------------------------------------------------++-import sys--+ class TestLazyFeatureRegistry:+ """Sanity checks on the registry shape — guards against accidental edits."""++ def test_registry_entries_have_required_fields(self):+ from litellm.proxy._lazy_features import LAZY_FEATURES, LazyFeature++ assert len(LAZY_FEATURES) > 0+ for feat in LAZY_FEATURES:+ assert isinstance(feat, LazyFeature)+ assert feat.name+ assert feat.module_path+ assert feat.path_prefixes+ assert all(p.startswith("/") for p in feat.path_prefixes)+ assert callable(feat.register_fn)++ def test_registry_names_unique(self):+ from litellm.proxy._lazy_features import LAZY_FEATURES++ names = [f.name for f in LAZY_FEATURES]+ assert len(names) == len(set(names)), "duplicate feature names"+++ class TestLazyFeaturesNotImportedAtStartup:+ """+ The whole point of the refactor: gated feature modules must NOT be+ present in `sys.modules` immediately after `proxy_server` imports.+ """++ def test_heavy_modules_absent_at_startup(self):+ # Static scan of proxy_server.py source — catches any top-level+ # `from <lazy_module> import` that would defeat lazy loading.+ # Importing proxy_server in a subprocess and diffing sys.modules+ # would also work, but takes 60-120 s and flakes on slow CI runners.+ import re+ from pathlib import Path++ from litellm.proxy._lazy_features import LAZY_FEATURES++ proxy_server_src = (+ Path(__file__).resolve().parents[3] / "litellm/proxy/proxy_server.py"+ ).read_text()++ leaks = []+ for feat in LAZY_FEATURES:+ # Anchor at column 0 — indented imports inside function bodies+ # are fine (deferred until the function runs).+ pattern = (+ rf"^(from\s+{re.escape(feat.module_path)}\s+import|"+ rf"import\s+{re.escape(feat.module_path)})"+ )+ if re.search(pattern, proxy_server_src, re.MULTILINE):+ leaks.append(feat.module_path)++ assert not leaks, (+ "proxy_server.py top-level imports a lazy feature module — these "+ f"should be loaded via LazyFeatureMiddleware: {leaks}"+ )+++ class TestLazyFeatureMiddleware:+ """Behavior of the middleware itself, exercised in isolation."""++ @pytest.mark.asyncio+ async def test_first_request_triggers_load_subsequent_does_not(self):+ from fastapi import FastAPI++ from litellm.proxy._lazy_features import (+ LazyFeature,+ LazyFeatureMiddleware,+ )++ loads = []++ def fake_register(app, module):+ loads.append(getattr(module, "__name__", "?"))++ feat = LazyFeature(+ name="dummy",+ module_path="json", # any always-importable stdlib module+ path_prefixes=("/dummy",),+ register_fn=fake_register,+ )++ # Build a minimal ASGI receiver to satisfy the middleware contract+ async def downstream(scope, receive, send):+ # echo back; no-op handler+ await send({"type": "http.response.start", "status": 200, "headers": []})+ await send({"type": "http.response.body", "body": b""})++ target_app = FastAPI()+ mw = LazyFeatureMiddleware(downstream, fastapi_app=target_app, features=(feat,))++ async def receive():+ return {"type": "http.request", "body": b"", "more_body": False}++ sent: list = []++ async def send(message):+ sent.append(message)++ # First request matching the prefix triggers register+ await mw(+ {"type": "http", "path": "/dummy/x", "method": "GET", "headers": []},+ receive,+ send,+ )+ assert loads == ["json"]++ # Second matching request must NOT re-register+ sent.clear()+ await mw(+ {"type": "http", "path": "/dummy/y", "method": "GET", "headers": []},+ receive,+ send,+ )+ assert loads == ["json"], "register_fn called twice for the same feature"++ # Non-matching path must not trigger anything+ await mw(+ {"type": "http", "path": "/unrelated", "method": "GET", "headers": []},+ receive,+ send,+ )+ assert loads == ["json"]++ @pytest.mark.asyncio+ async def test_concurrent_first_requests_only_register_once(self):+ """+ Two requests to the same prefix arriving in parallel must result in+ exactly one `register_fn` invocation — the lock prevents the import ++ register from racing with itself.+ """+ from fastapi import FastAPI++ from litellm.proxy._lazy_features import (+ LazyFeature,+ LazyFeatureMiddleware,+ )++ loads = []++ def slow_register(app, module):+ loads.append(getattr(module, "__name__", "?"))++ feat = LazyFeature(+ name="dummy_concurrent",+ module_path="json",+ path_prefixes=("/dummy_c",),+ register_fn=slow_register,+ )++ async def downstream(scope, receive, send):+ await send({"type": "http.response.start", "status": 200, "headers": []})+ await send({"type": "http.response.body", "body": b""})++ target_app = FastAPI()+ mw = LazyFeatureMiddleware(downstream, fastapi_app=target_app, features=(feat,))++ async def receive():+ return {"type": "http.request", "body": b"", "more_body": False}++ sent: list = []++ async def send(message):+ sent.append(message)++ async def hit():+ await mw(+ {+ "type": "http",+ "path": "/dummy_c/x",+ "method": "GET",+ "headers": [],+ },+ receive,+ send,+ )++ await asyncio.gather(hit(), hit(), hit(), hit(), hit())+ assert loads == [+ "json"+ ], f"expected one registration despite concurrent first hits, got {loads}"++ @pytest.mark.asyncio+ async def test_failing_import_does_not_loop(self):+ """+ If a feature's module can't be imported, the middleware should mark it+ loaded anyway so subsequent requests don't repeatedly retry the failing+ import (which would amplify the cost on every request).+ """+ from fastapi import FastAPI++ from litellm.proxy._lazy_features import (+ LazyFeature,+ LazyFeatureMiddleware,+ )++ attempts = []++ def fail_register(app, module):+ attempts.append("called")+ raise RuntimeError("boom")++ feat = LazyFeature(+ name="failing",+ module_path="json",+ path_prefixes=("/fail",),+ register_fn=fail_register,+ )++ async def downstream(scope, receive, send):+ await send({"type": "http.response.start", "status": 200, "headers": []})+ await send({"type": "http.response.body", "body": b""})++ target_app = FastAPI()+ mw = LazyFeatureMiddleware(downstream, fastapi_app=target_app, features=(feat,))++ async def receive():+ return {"type": "http.request", "body": b"", "more_body": False}++ sent: list = []++ async def send(message):+ sent.append(message)++ for _ in range(3):+ await mw(+ {"type": "http", "path": "/fail/x", "method": "GET", "headers": []},+ receive,+ send,+ )+ assert attempts == [+ "called"+ ], f"failing register_fn should be invoked once, not on every request; got {attempts}"+++ @pytest.mark.asyncio+ async def test_get_current_spend_redis_clean_miss_skips_stale_in_memory():+ """When Redis is reachable and cleanly returns None (TTL expired,+ counter genuinely absent), the read must reseed from DB - NOT fall+ through to per-pod in-memory which only contains this pod's writes.++ Pre-fix in multi-pod deployments, in-memory contained a stale local+ subset (e.g. $30) while DB had the true cross-pod total ($500). The+ fall-through returned $30, enforcement passed, bypass.+ """+ from litellm.caching.dual_cache import DualCache+ from litellm.proxy.proxy_server import get_current_spend++ counter_cache = DualCache()+ counter_key = "spend:team_member:user-1:team-1"++ # Per-pod stale in-memory: only this pod's writes, not cross-pod truth.+ counter_cache.in_memory_cache.set_cache(key=counter_key, value=30.0)++ # Redis cleanly returns None (key expired or never written on this pod).+ fake_redis = AsyncMock()+ fake_redis.async_get_cache = AsyncMock(return_value=None)+ fake_redis.async_increment = AsyncMock(return_value=500.0)+ counter_cache.redis_cache = fake_redis++ # DB has the authoritative cross-pod spend.+ db_row = MagicMock()+ db_row.spend = 500.0+ fake_prisma = MagicMock()+ fake_prisma.db.litellm_teammembership.find_unique = AsyncMock(return_value=db_row)++ import litellm.proxy.proxy_server as ps++ orig_counter, orig_prisma = ps.spend_counter_cache, ps.prisma_client+ ps.spend_counter_cache = counter_cache+ ps.prisma_client = fake_prisma+ try:+ spend = await get_current_spend(counter_key=counter_key, fallback_spend=0.0)+ assert spend == 500.0, (+ f"expected DB-authoritative 500.0 on clean Redis miss, got {spend} "+ f"(stale per-pod in-memory $30 would have caused multi-pod bypass)"+ )+ finally:+ ps.spend_counter_cache = orig_counter+ ps.prisma_client = orig_prisma+++ @pytest.mark.asyncio+ async def test_get_current_spend_redis_error_falls_back_to_in_memory():+ """When Redis raises, the read should still degrade to in-memory rather+ than going straight to DB - in-memory is at least same-pod-fresh and+ cheaper than a DB query during a Redis outage."""+ from litellm.caching.dual_cache import DualCache+ from litellm.proxy.proxy_server import get_current_spend++ counter_cache = DualCache()+ counter_key = "spend:team_member:user-1:team-1"++ counter_cache.in_memory_cache.set_cache(key=counter_key, value=42.0)++ fake_redis = AsyncMock()+ fake_redis.async_get_cache = AsyncMock(side_effect=ConnectionError("redis down"))+ counter_cache.redis_cache = fake_redis++ fake_prisma = MagicMock()+ fake_prisma.db.litellm_teammembership.find_unique = AsyncMock(+ return_value=MagicMock(spend=999.0)+ )++ import litellm.proxy.proxy_server as ps++ orig_counter, orig_prisma = ps.spend_counter_cache, ps.prisma_client+ ps.spend_counter_cache = counter_cache+ ps.prisma_client = fake_prisma+ try:+ spend = await get_current_spend(counter_key=counter_key, fallback_spend=0.0)+ assert spend == 42.0, (+ f"expected in-memory fallback 42.0 on Redis error, got {spend} "+ f"(should not have hit DB when Redis errored)"+ )+ # DB query should NOT have fired - in-memory short-circuits.+ fake_prisma.db.litellm_teammembership.find_unique.assert_not_awaited()+ finally:+ ps.spend_counter_cache = orig_counter+ ps.prisma_client = orig_prisma