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]

Addressed greptile comments to extract common helpers and return 404

Ephrim Stanleya3762e7d4986
diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
index d9514bf28bc..f341a1e9634 100644
--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
@@ -230,14 +230,14 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
if managed_file:
return managed_file.created_by == user_id
- # When DB record is missing (file was deleted), allow through so downstream returns 404.
- # Matches can_user_call_unified_object_id which also returns True for missing records.
- return True
+ raise HTTPException(
+ status_code=404,
+ detail=f"File not found: {unified_file_id}",
+ )
async def can_user_call_unified_object_id(
self, unified_object_id: str, user_api_key_dict: UserAPIKeyAuth
) -> bool:
- ## check if the user has access to the unified object id
## check if the user has access to the unified object id
user_id = user_api_key_dict.user_id
managed_object = (
@@ -248,7 +248,10 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
if managed_object:
return managed_object.created_by == user_id
- return True # don't raise error if managed object is not found
+ raise HTTPException(
+ status_code=404,
+ detail=f"Object not found: {unified_object_id}",
+ )
async def list_user_batches(
self,
diff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.py
index b125b90338b..783ac9d6f19 100644
--- a/litellm/proxy/batches_endpoints/endpoints.py
+++ b/litellm/proxy/batches_endpoints/endpoints.py
@@ -29,6 +29,7 @@ from litellm.proxy.openai_files_endpoints.common_utils import (
get_models_from_unified_file_id,
get_original_file_id,
prepare_data_with_credentials,
+ resolve_input_file_id_to_unified,
update_batch_in_database,
)
from litellm.proxy.utils import handle_exception_on_proxy, is_known_model
@@ -379,22 +380,9 @@ async def retrieve_batch(
)
# async_post_call_success_hook replaces batch.id and output_file_id with unified IDs
- # but not input_file_id. Look up the unified ID from flat_model_file_ids.
- if (
- unified_batch_id
- and hasattr(response, "input_file_id")
- and response.input_file_id
- and not _is_base64_encoded_unified_file_id(response.input_file_id)
- and prisma_client
- ):
- try:
- _managed_file = await prisma_client.db.litellm_managedfiletable.find_first(
- where={"flat_model_file_ids": {"has": response.input_file_id}}
- )
- if _managed_file:
- response.input_file_id = _managed_file.unified_file_id
- except Exception:
- pass
+ # but not input_file_id. Resolve raw provider ID to unified ID.
+ if unified_batch_id:
+ await resolve_input_file_id_to_unified(response, prisma_client)
asyncio.create_task(
proxy_logging_obj.update_request_status(
@@ -497,23 +485,10 @@ async def retrieve_batch(
data=data, user_api_key_dict=user_api_key_dict, response=response
)
- # Fix: bug_feb14_batch_retrieve_returns_raw_input_file_id (terminal state path)
- # Same as above — resolve raw provider input_file_id to unified ID.
- if (
- unified_batch_id
- and hasattr(response, "input_file_id")
- and response.input_file_id
- and not _is_base64_encoded_unified_file_id(response.input_file_id)
- and prisma_client
- ):
- try:
- _managed_file = await prisma_client.db.litellm_managedfiletable.find_first(
- where={"flat_model_file_ids": {"has": response.input_file_id}}
- )
- if _managed_file:
- response.input_file_id = _managed_file.unified_file_id
- except Exception:
- pass
+ # Fix: bug_feb14_batch_retrieve_returns_raw_input_file_id
+ # Resolve raw provider input_file_id to unified ID.
+ if unified_batch_id:
+ await resolve_input_file_id_to_unified(response, prisma_client)
### ALERTING ###
asyncio.create_task(
diff --git a/litellm/proxy/openai_files_endpoints/common_utils.py b/litellm/proxy/openai_files_endpoints/common_utils.py
index 0e3b31b2aa7..75f64cddf59 100644
--- a/litellm/proxy/openai_files_endpoints/common_utils.py
+++ b/litellm/proxy/openai_files_endpoints/common_utils.py
@@ -644,6 +644,28 @@ def _extract_model_param(request: "Request", request_body: dict) -> Optional[str
# ============================================================================
+async def resolve_input_file_id_to_unified(response, prisma_client) -> None:
+ """
+ If the batch response contains a raw provider input_file_id (not already a
+ unified ID), look up the corresponding unified file ID from the managed file
+ table and replace it in-place.
+ """
+ if (
+ hasattr(response, "input_file_id")
+ and response.input_file_id
+ and not _is_base64_encoded_unified_file_id(response.input_file_id)
+ and prisma_client
+ ):
+ try:
+ managed_file = await prisma_client.db.litellm_managedfiletable.find_first(
+ where={"flat_model_file_ids": {"has": response.input_file_id}}
+ )
+ if managed_file:
+ response.input_file_id = managed_file.unified_file_id
+ except Exception:
+ pass
+
+
async def get_batch_from_database(
batch_id: str,
unified_batch_id: Union[str, Literal[False]],
@@ -689,15 +711,7 @@ async def get_batch_from_database(
response.id = batch_id
# The stored batch object has the raw provider input_file_id. Resolve to unified ID.
- if response.input_file_id and not _is_base64_encoded_unified_file_id(response.input_file_id):
- try:
- managed_file = await prisma_client.db.litellm_managedfiletable.find_first(
- where={"flat_model_file_ids": {"has": response.input_file_id}}
- )
- if managed_file:
- response.input_file_id = managed_file.unified_file_id
- except Exception:
- pass
+ await resolve_input_file_id_to_unified(response, prisma_client)
verbose_proxy_logger.debug(
f"Retrieved batch {batch_id} from ManagedObjectTable with status={response.status}"
diff --git a/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py b/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py
index 042796c9640..7ad564dc8f9 100644
--- a/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py
+++ b/tests/test_litellm/enterprise/proxy/test_deleted_file_returns_403_not_404.py
@@ -1,13 +1,9 @@
"""
-Regression test: deleted managed files should not return 403.
+Regression test: deleted managed files should return 404, not 403.
When a managed file's DB record has been deleted, can_user_call_unified_file_id()
-returns False (record not found → treated as "access denied"). This causes
-check_managed_file_id_access() to raise 403 instead of allowing the request
-through so downstream code can return a proper 404.
-
-The equivalent method for objects (can_user_call_unified_object_id) already
-returns True when the record is missing.
+raises HTTPException(404) directly — rather than returning True (which would
+weaken access control) or False (which would cause a misleading 403).
"""
import base64
@@ -49,20 +45,19 @@ def _make_managed_files_with_no_db_record():
@pytest.mark.asyncio
-async def test_should_not_raise_403_for_deleted_file():
+async def test_should_raise_404_for_deleted_file():
"""
When a managed file record has been deleted from the DB,
- check_managed_file_id_access should NOT raise 403.
- It should allow the request through so downstream can return 404.
+ check_managed_file_id_access should raise 404 (not 403).
"""
unified_file_id = _make_unified_file_id()
managed_files = _make_managed_files_with_no_db_record()
user = _make_user_api_key_dict("any-user")
data = {"file_id": unified_file_id}
- # This should NOT raise — deleted file should pass through access check
- result = await managed_files.check_managed_file_id_access(data, user)
- assert result is True
+ with pytest.raises(HTTPException) as exc_info:
+ await managed_files.check_managed_file_id_access(data, user)
+ assert exc_info.value.status_code == 404
@pytest.mark.asyncio