Skip to content

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]

fix(proxy): make team cache invalidation best-effort

mateo-berri769f434bd252
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 4c998f997d9..21b455c5d44 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -1767,7 +1767,15 @@ async def _cache_team_object(
key = "team_id:{}".format(team_id)
if proxy_logging_obj is not None:
- await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=key)
+ try:
+ await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=key)
+ except Exception as e:
+ verbose_proxy_logger.warning(
+ "Failed to invalidate internal usage cache entry %s; "
+ "a stale team object may be served until its TTL expires: %s",
+ key,
+ e,
+ )
# team_id is the table primary key — guaranteed unique, safe to write.
await _cache_management_object(
@@ -1793,9 +1801,17 @@ async def _cache_team_object(
# the cache from a verified single row.
if team_table.team_alias:
alias_key = "team_alias:{}".format(team_table.team_alias)
- user_api_key_cache.delete_cache(key=alias_key)
- if proxy_logging_obj is not None:
- await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=alias_key)
+ try:
+ user_api_key_cache.delete_cache(key=alias_key)
+ if proxy_logging_obj is not None:
+ await proxy_logging_obj.internal_usage_cache.dual_cache.async_delete_cache(key=alias_key)
+ except Exception as e:
+ verbose_proxy_logger.warning(
+ "Failed to invalidate cached team alias entry %s; "
+ "a stale team object may be served until its TTL expires: %s",
+ alias_key,
+ e,
+ )
async def _cache_key_object(
diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py
index b7bf5728cbd..fa7ee5b531a 100644
--- a/tests/test_litellm/proxy/auth/test_auth_checks.py
+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py
@@ -4343,6 +4343,45 @@ async def test_team_update_not_shadowed_by_internal_usage_cache_lit_4391():
)
+async def test_cache_team_object_tolerates_cache_invalidation_failures():
+ """
+ Greptile review on the LIT-4391 fix: `_cache_team_object` runs after a
+ successful DB fetch (inside `get_team_object`) and after every team
+ mutation's DB write. A cache-backend error during the best-effort
+ invalidations must NOT fail those operations — otherwise a Redis blip
+ turns a healthy team lookup into a 404 and a committed /team/update into
+ a 500. The authoritative team_id-keyed write must still happen.
+ """
+ from litellm.proxy._types import LiteLLM_TeamTableCachedObj
+ from litellm.proxy.auth.auth_checks import _cache_team_object
+
+ cache = MagicMock()
+ cache.async_set_cache = AsyncMock()
+ cache.delete_cache = MagicMock(side_effect=Exception("redis down"))
+ logging_obj = MagicMock()
+ logging_obj.internal_usage_cache.dual_cache.async_delete_cache = AsyncMock(
+ side_effect=Exception("redis down")
+ )
+
+ await _cache_team_object(
+ team_id="team-cache-outage",
+ team_table=LiteLLM_TeamTableCachedObj(
+ team_id="team-cache-outage",
+ team_alias="cache-outage-alias",
+ models=["model-a"],
+ ),
+ user_api_key_cache=cache,
+ proxy_logging_obj=logging_obj,
+ )
+
+ written_keys = [
+ (c.kwargs.get("key") or c.args[0])
+ for c in cache.async_set_cache.await_args_list
+ ]
+ assert written_keys == ["team_id:team-cache-outage"]
+
+
MODEL_DISCOVERY_ROUTES = [
"/v1/models",
"/models",