fix(proxy): keep soft_budget on bulk-created users and send one email per user soft budget crossing

POST /management/v1/users/bulk accepted soft_budget through NewUserRequest but
dropped it before create_many, so carry it into the created row. Skip the
generic SlackAlerting SMTP email for user soft budget crossings, since the soft
budget email logger already sends the user a full email for the same event.
This commit is contained in:
Jesus Velarde 2026-11-14 16:00:36 -08:00
parent 533abe99c6
commit 6812fc2162
4 changed files with 39 additions and 1 deletions

View file

@ -1422,6 +1422,8 @@ Model Info:
Returns -> True if sent, False if not.
"""
if webhook_event.event == "soft_budget_crossed" and webhook_event.event_group == Litellm_EntityType.USER:
return False
from litellm.proxy.proxy_server import premium_user
from litellm.proxy.utils import send_email

View file

@ -114,6 +114,7 @@ class _UserRow(LiteLLMBaseModel):
user_role: str | None = None
team_id: str | None = None
max_budget: float | None = None
soft_budget: float | None = None
spend: float | None = 0.0
models: tuple[str, ...] | None = None
metadata: Mapping[str, object] | None = None
@ -375,6 +376,7 @@ class _UserCreateData(TypedDict):
user_role: ReadOnly[str | None]
team_id: ReadOnly[str | None]
max_budget: ReadOnly[float | None]
soft_budget: ReadOnly[float | None]
spend: ReadOnly[float]
models: ReadOnly[tuple[str, ...]]
metadata: ReadOnly[str]
@ -409,6 +411,7 @@ def _user_create_payload(prepared: _PreparedUser) -> _UserCreateData:
"user_role": row.user_role,
"team_id": row.team_id,
"max_budget": row.max_budget,
"soft_budget": row.soft_budget,
"spend": row.spend or 0.0,
"models": row.models or (),
"metadata": metadata_json,

View file

@ -1326,3 +1326,35 @@ def test_budget_email_escapes_user_id() -> None:
assert "<a href" not in html
assert "&lt;a href=&quot;https://evil.example&quot;&gt;click&lt;/a&gt;" in html
@pytest.mark.asyncio
@pytest.mark.parametrize(
("event_group", "recipients"), [(Litellm_EntityType.USER, []), (Litellm_EntityType.KEY, ["u1@example.com"])]
)
async def test_user_soft_budget_crossing_leaves_email_to_the_soft_budget_email_logger(
monkeypatch: pytest.MonkeyPatch, event_group: Litellm_EntityType, recipients: list[str]
) -> None:
import litellm.proxy.utils as proxy_utils
from litellm.proxy._types import WebhookEvent
sent: Final[list[str]] = []
async def record_email(receiver_email: str, subject: str, html: str) -> None:
sent.append(receiver_email)
monkeypatch.setattr(proxy_utils, "send_email", record_email)
event: Final = WebhookEvent(
spend=6,
soft_budget=5,
user_id="u1",
user_email="u1@example.com",
token="hashed-token",
event="soft_budget_crossed",
event_group=event_group,
event_message="Soft budget crossed",
)
await SlackAlerting(alerting=["email"]).send_email_alert_using_smtp(webhook_event=event, alert_type="budget_alerts")
assert sent == recipients

View file

@ -212,7 +212,7 @@ async def test_creates_users_and_team_membership_in_every_store():
response = await _run(
prisma,
[
{"user_id": "u1", "user_email": "a@example.com", "teams": ["t1", "t2"], "max_budget": 50},
{"user_id": "u1", "user_email": "a@example.com", "teams": ["t1", "t2"], "max_budget": 50, "soft_budget": 20},
{"user_id": "u2", "user_email": "b@example.com", "teams": ["t1"]},
{"user_id": "u3", "user_email": "c@example.com"},
],
@ -225,6 +225,7 @@ async def test_creates_users_and_team_membership_in_every_store():
users = prisma.db.litellm_usertable.rows
assert users["u1"].teams == ["t1", "t2"] and users["u1"].max_budget == 50
assert users["u1"].soft_budget == 20 and getattr(users["u2"], "soft_budget", None) is None
assert users["u2"].teams == ["t1"] and users["u3"].teams == []
assert [m.user_id for m in prisma.db.litellm_teamtable.rows["t1"].members_with_roles] == ["existing", "u1", "u2"]
assert [m.user_id for m in prisma.db.litellm_teamtable.rows["t2"].members_with_roles] == ["u1"]