fix: skip budget lookup failure log for expected user-not-found case

Avoid logging 'cache will not be populated' when the user simply doesn't
exist - not caching is correct behavior in that case. Only log for
unexpected errors (schema, DB, etc.) where the message is meaningful.
This commit is contained in:
Alexsander Hamir 2026-02-06 09:23:46 -08:00
parent 2745c5133e
commit 4b839f9c5e
2 changed files with 15 additions and 2 deletions

View file

@ -79,8 +79,13 @@ def _log_budget_lookup_failure(entity: str, error: Exception) -> None:
"""
Log a warning when budget lookup fails; cache will not be populated.
Adds a schema migration hint when the error appears schema-related.
Skips logging for expected "user not found" cases (bare Exception from
get_user_object when user_id_upsert=False). Adds a schema migration hint
when the error appears schema-related.
"""
# Skip logging for expected "user not found" - not caching is correct
if str(error) == "" and type(error).__name__ == "Exception":
return
err_str = str(error).lower()
hint = ""
if any(

View file

@ -275,7 +275,7 @@ async def test_default_internal_user_params_with_get_user_object(monkeypatch):
def test_log_budget_lookup_failure_dry_run():
"""Dry run: verify _log_budget_lookup_failure logs without raising."""
"""Dry run: verify _log_budget_lookup_failure logs for schema/DB errors."""
with patch("litellm.proxy.auth.auth_checks.verbose_proxy_logger") as mock_logger:
err = Exception("column 'policies' does not exist in prisma schema")
_log_budget_lookup_failure("user", err)
@ -287,6 +287,14 @@ def test_log_budget_lookup_failure_dry_run():
assert "prisma db push" in call_msg
def test_log_budget_lookup_failure_skips_user_not_found():
"""Verify _log_budget_lookup_failure does NOT log for expected user-not-found."""
with patch("litellm.proxy.auth.auth_checks.verbose_proxy_logger") as mock_logger:
err = Exception() # bare Exception from get_user_object when user not found
_log_budget_lookup_failure("user", err)
mock_logger.error.assert_not_called()
@pytest.mark.asyncio
@patch("litellm.proxy.management_endpoints.team_endpoints.new_team", new_callable=AsyncMock)
async def test_get_team_db_check_calls_new_team_on_upsert(mock_new_team, monkeypatch):