fix(batches): route filtered list_batches through provider fallback

Issue: #36242
Root cause: list_batches unconditionally routes through the managed-files
hook when available, even for provider/target_model_name-filtered requests
the hook cannot satisfy. The hook raised a bare Exception, which became an
opaque HTTP 500.

Fix:
- Gate managed-files path in list_batches on unfiltered requests only
- Raise HTTPException(400) in the hook for filtered requests
- Update tests to expect HTTP 400 instead of bare Exception

Test plan:
- PYTHONPATH=... .venv/bin/python -m pytest
  tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
  -k provider_filter_raises_exception -v
This commit is contained in:
Lavya Tandel 2026-08-08 12:29:28 +05:30
parent 457be8f00a
commit 4720ff448a
3 changed files with 22 additions and 9 deletions

View file

@ -420,13 +420,19 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
# This is because the encoded object ids stored in the managed objects table do not contain the provider information
# To support provider filtering, we would need to store the provider information in the encoded object ids
if provider:
raise Exception("Filtering by 'provider' is not supported when using managed batches.")
raise HTTPException(
status_code=400,
detail="Filtering by 'provider' is not supported when using managed batches.",
)
# Model name filtering is not supported for managed batches
# This is because the encoded object ids stored in the managed objects table do not contain the model name
# A hash of the model name + litellm_params for the model name is encoded as the model id. This is not sufficient to reliably map the target model names to the model ids.
if target_model_names:
raise Exception("Filtering by 'target_model_names' is not supported when using managed batches.")
raise HTTPException(
status_code=400,
detail="Filtering by 'target_model_names' is not supported when using managed batches.",
)
owner_filter = build_owner_filter(user_api_key_dict)
if owner_filter is None:

View file

@ -695,7 +695,12 @@ async def list_batches(
# Try to use managed objects table for listing batches (returns encoded IDs).
managed_files_obj: Final = proxy_logging_obj.get_proxy_hook("managed_files")
if managed_files_obj is not None and hasattr(managed_files_obj, "list_user_batches"):
if (
managed_files_obj is not None
and hasattr(managed_files_obj, "list_user_batches")
and not provider
and not target_model_names
):
verbose_proxy_logger.debug("Using managed objects table for batch listing")
response = await cast(Any, managed_files_obj).list_user_batches(
user_api_key_dict=user_api_key_dict,

View file

@ -2007,15 +2007,16 @@ async def test_list_batches_from_managed_objects_table_provider_filter_raises_ex
DualCache(), prisma_client=prisma_client
)
# Filtering by provider should raise Exception
with pytest.raises(Exception) as exc_info:
# Filtering by provider should raise HTTPException with 400
with pytest.raises(HTTPException) as exc_info:
await proxy_managed_files.list_user_batches(
user_api_key_dict=UserAPIKeyAuth(user_id="test-user"),
limit=10,
provider="openai",
)
assert str(exc_info.value) == (
assert exc_info.value.status_code == 400
assert str(exc_info.value.detail) == (
"Filtering by 'provider' is not supported when using managed batches."
)
@ -2033,15 +2034,16 @@ async def test_list_batches_from_managed_objects_table_target_model_name_filter_
DualCache(), prisma_client=prisma_client
)
# Filtering by provider should raise Exception
with pytest.raises(Exception) as exc_info:
# Filtering by target_model_names should raise HTTPException with 400
with pytest.raises(HTTPException) as exc_info:
await proxy_managed_files.list_user_batches(
user_api_key_dict=UserAPIKeyAuth(user_id="test-user"),
limit=10,
target_model_names="gpt-5.5,gpt-3.5",
)
assert str(exc_info.value) == (
assert exc_info.value.status_code == 400
assert str(exc_info.value.detail) == (
"Filtering by 'target_model_names' is not supported when using managed batches."
)