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(auth): cache team member default budget as a typed model (#37695)

devin-ai-integration[bot]3ea1c16b0d07
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 8708f96339f..e1cc91df1c7 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -247,14 +247,6 @@ def _raw_cache(cache: _RawCacheRead) -> _RawCacheRead:
return cache
-class _BudgetCacheRead(Protocol):
- async def async_get_cache(self, *, key: str) -> "LiteLLM_BudgetTable | Mapping[str, object] | None": ...
-
-
-def _budget_cache(cache: _BudgetCacheRead) -> _BudgetCacheRead:
- return cache
-
-
def _typed_request_body(request_body: dict) -> Mapping[str, object]:
return request_body
@@ -1190,33 +1182,35 @@ async def get_team_member_default_budget(
cache_key: Final = f"team_member_default_budget:{budget_id}"
- cached_budget: Final = await _budget_cache(user_api_key_cache).async_get_cache(key=cache_key)
- if isinstance(cached_budget, LiteLLM_BudgetTable):
+ cached_budget: Final = await user_api_key_cache.async_get_cache(
+ key=cache_key,
+ model_type=LiteLLM_BudgetTable,
+ )
+ if cached_budget is not None:
return cached_budget
- if isinstance(cached_budget, dict):
- return LiteLLM_BudgetTable.model_validate(cached_budget)
try:
budget_record: Final = await _dictable_table(BudgetRepository(prisma_client)).find_unique(
where={"budget_id": budget_id}
)
-
- if budget_record is None:
- verbose_proxy_logger.warning("Team-default member budget not found in database: %s", budget_id)
- return None
-
- await user_api_key_cache.async_set_cache(
- key=cache_key,
- value=budget_record.dict(),
- ttl=get_management_object_ttl(user_api_key_cache),
- )
-
- return LiteLLM_BudgetTable.model_validate(budget_record.dict())
-
except Exception:
verbose_proxy_logger.exception("Error fetching team-default member budget %s", budget_id)
return None
+ if budget_record is None:
+ verbose_proxy_logger.warning("Team-default member budget not found in database: %s", budget_id)
+ return None
+
+ budget: Final = LiteLLM_BudgetTable.model_validate(budget_record.dict())
+ await user_api_key_cache.async_set_cache(
+ key=cache_key,
+ value=budget,
+ model_type=LiteLLM_BudgetTable,
+ ttl=get_management_object_ttl(user_api_key_cache),
+ )
+
+ return budget
+
async def _apply_default_budget_to_end_user(
end_user_obj: LiteLLM_EndUserTable,
diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py
index 7a3288dec37..ccbf00a67b9 100644
--- a/tests/test_litellm/proxy/auth/test_auth_checks.py
+++ b/tests/test_litellm/proxy/auth/test_auth_checks.py
@@ -5695,6 +5695,64 @@ async def test_get_default_end_user_budget_db_fetch_returns_validated_budget(mon
assert mock_cache.async_set_cache.call_args.kwargs["value"] is result
+async def test_get_team_member_default_budget_caches_json_safe_payload():
+ """The Redis layer json.dumps() the cached value, so datetime columns on the budget row
+ must be dumped to ISO strings before the write, and the read side must give back a model.
+ """
+ from litellm.proxy.auth.auth_checks import get_team_member_default_budget
+ from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
+
+ budget_row = MagicMock()
+ budget_row.dict = lambda: {
+ "budget_id": "tm-budget-1",
+ "max_budget": 25.0,
+ "created_at": datetime(2026, 1, 1, tzinfo=timezone.utc),
+ "updated_at": datetime(2026, 1, 2, tzinfo=timezone.utc),
+ }
+
+ mock_prisma_client = MagicMock()
+ mock_prisma_client.db.litellm_budgettable.find_unique = AsyncMock(return_value=budget_row)
+
+ class _JsonOnlyRedis:
+ """Stands in for RedisCache, which serializes with a bare json.dumps()."""
+
+ def __init__(self):
+ self.writes = []
+
+ async def async_set_cache(self, key, value, **kwargs):
+ self.writes.append((key, json.dumps(value)))
+
+ async def async_get_cache(self, key, **kwargs):
+ return None
+
+ redis_cache = _JsonOnlyRedis()
+ cache = UserApiKeyCache(redis_cache=redis_cache)
+
+ budget = await get_team_member_default_budget(
+ budget_id="tm-budget-1",
+ prisma_client=mock_prisma_client,
+ user_api_key_cache=cache,
+ )
+
+ assert isinstance(budget, LiteLLM_BudgetTable)
+ assert budget.max_budget == 25.0
+ assert len(redis_cache.writes) == 1
+ written_key, written_payload = redis_cache.writes[0]
+ assert written_key == "team_member_default_budget:tm-budget-1"
+ assert json.loads(written_payload)["max_budget"] == 25.0
+
+ cached = await get_team_member_default_budget(
+ budget_id="tm-budget-1",
+ prisma_client=mock_prisma_client,
+ user_api_key_cache=cache,
+ )
+
+ assert isinstance(cached, LiteLLM_BudgetTable)
+ assert cached.max_budget == 25.0
+ mock_prisma_client.db.litellm_budgettable.find_unique.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_get_end_user_object_db_fetch_returns_validated_end_user():
from litellm.proxy.auth.auth_checks import get_end_user_object