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): reject out-of-range limit on GET /v1/batches with OpenAI-parity 400

mateo-berrif55a1936283d
diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
index f1b4c6b5b17..aebfa9723af 100644
--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
@@ -431,6 +431,9 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
if target_model_names:
raise Exception("Filtering by 'target_model_names' is not supported when using managed batches.")
+ if limit == 0:
+ return build_list_page([])
+
owner_filter = build_owner_filter(user_api_key_dict)
if owner_filter is None:
return build_list_page([])
diff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.py
index 6952c0c6f89..1cd46fd6dcb 100644
--- a/litellm/proxy/batches_endpoints/endpoints.py
+++ b/litellm/proxy/batches_endpoints/endpoints.py
@@ -642,6 +642,23 @@ async def retrieve_batch(
raise handle_exception_on_proxy(e)
+def validate_batch_list_limit(limit: int | None) -> None:
+ if limit is None or 0 <= limit <= 100:
+ return
+ bound, expected, openai_code = (
+ ("below minimum", ">= 0", "integer_below_min_value")
+ if limit < 0
+ else ("above maximum", "<= 100", "integer_above_max_value")
+ )
+ raise ProxyException(
+ message=f"Invalid 'limit': integer {bound} value. Expected a value {expected}, but got {limit} instead.",
+ type="invalid_request_error",
+ param="limit",
+ code=400,
+ openai_code=openai_code,
+ )
+
+
@router.get(
"/{provider}/v1/batches",
dependencies=[Depends(user_api_key_auth)],
@@ -679,6 +696,7 @@ async def list_batches(
```
"""
+ validate_batch_list_limit(limit)
from litellm.proxy.proxy_server import (
general_settings,
llm_router,
diff --git a/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py b/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
index fde1feb80e2..12de090cec3 100644
--- a/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
+++ b/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
@@ -97,6 +97,25 @@ async def test_async_pre_call_hook_batch_retrieve():
assert response["model"] == "my-general-azure-deployment"
+async def test_list_user_batches_limit_zero_returns_empty_page_without_db_query():
+ """OpenAI parity for GET /v1/batches?limit=0: an empty page, never the
+ default page of 20 (issue #37149). `min(limit or 20, 100)` treated 0 as
+ unset before this regression guard existed."""
+ from litellm.proxy._types import UserAPIKeyAuth
+
+ prisma_client = MagicMock()
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(DualCache(), prisma_client=prisma_client)
+
+ page = await proxy_managed_files.list_user_batches(
+ user_api_key_dict=UserAPIKeyAuth(user_id="123"),
+ limit=0,
+ )
+
+ assert page == {"object": "list", "data": [], "first_id": None, "last_id": None, "has_more": False}
+ prisma_client.db.litellm_managedobjecttable.find_many.assert_not_called()
+
+
@pytest.mark.asyncio
async def test_async_pre_call_deployment_hook_resolves_model_id_from_litellm_metadata():
"""
diff --git a/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py b/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
index 824654dcf66..b8f4a43d771 100644
--- a/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
+++ b/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
@@ -1513,6 +1513,59 @@ async def test_list__managed_files_beats_model_param(list_harness):
list_harness.creds_resolver.assert_not_called()
+ "limit, expected_message, expected_openai_code",
+ [
+ (
+ -1,
+ "Invalid 'limit': integer below minimum value. Expected a value >= 0, but got -1 instead.",
+ "integer_below_min_value",
+ ),
+ (
+ 101,
+ "Invalid 'limit': integer above maximum value. Expected a value <= 100, but got 101 instead.",
+ "integer_above_max_value",
+ ),
+ (
+ 1000,
+ "Invalid 'limit': integer above maximum value. Expected a value <= 100, but got 1000 instead.",
+ "integer_above_max_value",
+ ),
+ ],
+)
+async def test_list__out_of_range_limit_rejected_with_400(list_harness, limit, expected_message, expected_openai_code):
+ """OpenAI parity: GET /v1/batches rejects limit < 0 and limit > 100 with an
+ OpenAI-shaped 400 before any listing branch runs (issue #37149)."""
+ list_user_batches = list_harness.set_managed_files(FakeListPage([]))
+
+ with pytest.raises(ProxyException) as exc:
+ await call_list(list_harness, limit=limit)
+
+ assert exc.value.code == "400"
+ assert exc.value.param == "limit"
+ assert exc.value.type == "invalid_request_error"
+ assert exc.value.openai_code == expected_openai_code
+ assert exc.value.message == expected_message
+ list_user_batches.assert_not_called()
+ list_harness.litellm_alist.assert_not_called()
+ list_harness.router_alist.assert_not_called()
+
+
[email protected]("limit", [None, 0, 1, 100])
+async def test_list__in_range_limit_dispatches(list_harness, limit):
+ """OpenAI parity: live OpenAI accepts limit=0 (empty page) and 1..100, so
+ those values must keep flowing through to the listing branch untouched."""
+ page = FakeListPage([])
+ list_user_batches = list_harness.set_managed_files(page)
+
+ resp = await call_list(list_harness, limit=limit)
+
+ assert resp is page
+ assert list_user_batches.call_args.kwargs["limit"] == limit
+
+
# --------------------------------------------------------------------------- #
# Branch 2 - model from body/query/header. The endpoint resolves credentials
# for the body model, forwards custom_llm_provider once (it pops it from data