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): allow model instances in LiteLLM_EndUserTable validation (#41677)

Yusuf Efec9ce9a733d90
diff --git a/litellm/models/end_user.py b/litellm/models/end_user.py
index a5964d0d60a..afe9bfb03ea 100644
--- a/litellm/models/end_user.py
+++ b/litellm/models/end_user.py
@@ -7,7 +7,7 @@ Canonical definition for ``litellm_endusertable``. Re-exported from
from typing import Literal
-from pydantic import ConfigDict, Field, model_validator
+from pydantic import ConfigDict, Field, field_validator
from litellm.models.budget import LiteLLM_BudgetTable
from litellm.models.object_permission import LiteLLM_ObjectPermissionTable
@@ -27,11 +27,9 @@ class LiteLLM_EndUserTable(LiteLLMPydanticObjectBase):
object_permission_id: str | None = None
object_permission: LiteLLM_ObjectPermissionTable | None = None
- @model_validator(mode="before")
+ @field_validator("spend", mode="before")
@classmethod
- def set_model_info(cls, values):
- if values.get("spend") is None:
- values.update({"spend": 0.0})
- return values
+ def default_null_spend_to_zero(cls, spend: object) -> object:
+ return 0.0 if spend is None else spend
model_config = ConfigDict(protected_namespaces=())
diff --git a/tests/integration/authorization/test_end_user_block.py b/tests/integration/authorization/test_end_user_block.py
index 9f4fdb7f8f0..8b18bddfd00 100644
--- a/tests/integration/authorization/test_end_user_block.py
+++ b/tests/integration/authorization/test_end_user_block.py
@@ -114,7 +114,8 @@ def test_block_route_marks_the_customer_blocked_and_update_unblocks(gateway: Gat
assert created.status_code == 200, created.text
scenario.cleanups.callback(gateway.request, "POST", "/customer/delete", {"user_ids": [end_user]})
- gateway.request("POST", block_route, {"user_ids": [end_user]})
+ blocked: Final = gateway.request("POST", block_route, {"user_ids": [end_user]})
+ assert blocked.status_code == 200, blocked.text
assert eventually(lambda: _blocked_row(end_user), lambda value: value == [{"blocked": True}], seconds=15) == [
{"blocked": True}
]
@@ -165,7 +166,6 @@ def test_end_user_block_route_creates_and_blocks_an_unseen_customer(gateway: Gat
def test_customer_block_route_creates_and_blocks_an_unseen_customer(gateway: Gateway) -> None:
- pytest.skip("BUG: POST /customer/block for an unseen customer returns 500 instead of 200 BlockUsersResponse")
_assert_unseen_customer_is_blocked(gateway, "/customer/block")
@@ -313,7 +313,6 @@ def test_unblock_route_requires_the_blocked_user_check_callback(gateway: Gateway
def test_block_route_returns_the_blocked_records(gateway: Gateway) -> None:
- pytest.skip("BUG: POST /customer/block returns 500 instead of 200 BlockUsersResponse")
end_user: Final = f"integration-end-user-{uuid.uuid4().hex}"
with gateway.scenario() as scenario:
created: Final = gateway.request("POST", "/customer/new", {"user_id": end_user})
diff --git a/tests/unit/models/test_models.py b/tests/unit/models/test_models.py
index 139df21decc..e2c0edf6d9d 100644
--- a/tests/unit/models/test_models.py
+++ b/tests/unit/models/test_models.py
@@ -464,6 +464,18 @@ class TestEndUserTable:
eu = LiteLLM_EndUserTable(user_id="eu2", blocked=True, spend=None)
assert eu.spend == 0.0
+ def test_end_user_from_model_instance(self):
+ class MockPrismaRecord(BaseModel):
+ user_id: str
+ blocked: bool
+ spend: float | None = None
+
+ record = MockPrismaRecord(user_id="u3", blocked=True, spend=None)
+ table = LiteLLM_EndUserTable.model_validate(record, from_attributes=True)
+ assert table.user_id == "u3"
+ assert table.spend == 0.0
+ assert table.blocked is True
+
class TestBudgetTableFull:
def test_full_adds_server_managed_fields(self):
diff --git a/tests/unit/proxy/management_endpoints/test_customer_endpoints.py b/tests/unit/proxy/management_endpoints/test_customer_endpoints.py
index c99db2d8054..23cb328aabf 100644
--- a/tests/unit/proxy/management_endpoints/test_customer_endpoints.py
+++ b/tests/unit/proxy/management_endpoints/test_customer_endpoints.py
@@ -6,6 +6,7 @@ from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute
from fastapi.testclient import TestClient
+from pydantic import BaseModel
from litellm.proxy._types import (
LiteLLM_EndUserTable,
@@ -1122,6 +1123,29 @@ def test_customer_block_invalidates_end_user_and_registry_caches(mock_prisma_cli
]
+def test_customer_block_success_serializes_db_model_instance(mock_prisma_client, mock_user_api_key_auth):
+ class MockPrismaEndUser(BaseModel):
+ user_id: str
+ blocked: bool
+ spend: float | None = None
+
+ mock_prisma_client.db.litellm_endusertable.upsert = AsyncMock(
+ return_value=MockPrismaEndUser(user_id="c1", blocked=True, spend=None)
+ )
+
+ response = client.post(
+ "/customer/block",
+ json={"user_ids": ["c1"]},
+ headers={"Authorization": "Bearer k"},
+ )
+
+ assert response.status_code == 200, response.text
+ body = response.json()
+ assert body["blocked_users"][0]["user_id"] == "c1"
+ assert body["blocked_users"][0]["blocked"] is True
+ assert body["blocked_users"][0]["spend"] == 0.0
+
+
def test_customer_delete_invalidates_end_user_and_registry_caches(mock_prisma_client, mock_user_api_key_auth):
"""Without this a deleted customer keeps its cached budget and block enforced until the TTL expires."""
mock_prisma_client.db.litellm_endusertable.find_many = AsyncMock(