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]

Add end to end integration tests for batches

Ephrim Stanley479e40672ecd
diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
index 37eef176927..445d2b242b4 100644
--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py
@@ -890,7 +890,6 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
[file_id], litellm_parent_otel_span
)
- # PATCHED: Capture delete response from provider
delete_response = None
specific_model_file_id_mapping = model_file_id_mapping.get(file_id)
if specific_model_file_id_mapping:
@@ -903,7 +902,6 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
if stored_file_object:
return stored_file_object
- # PATCHED: Return provider response with unified ID when stored_file_object is None
elif delete_response:
delete_response.id = file_id
return delete_response
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index 9767e07bbc8..3865a4c65bc 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -3756,7 +3756,7 @@ class SpendUpdateQueueItem(TypedDict, total=False):
class LiteLLM_ManagedFileTable(LiteLLMPydanticObjectBase):
unified_file_id: str
- file_object: Optional[OpenAIFileObject] = None # PATCHED: Allow None for batch output files
+ file_object: Optional[OpenAIFileObject] = None
model_mappings: Dict[str, str]
flat_model_file_ids: List[str]
created_by: Optional[str]
diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.py
index 586f7840835..7e3f5820814 100644
--- a/litellm/proxy/openai_files_endpoints/files_endpoints.py
+++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py
@@ -885,7 +885,7 @@ async def get_file(
response = await managed_files_obj.afile_retrieve(
file_id=file_id,
litellm_parent_otel_span=user_api_key_dict.parent_otel_span,
- llm_router=llm_router, # PATCHED: Pass router to fetch from provider if file_object is None
+ llm_router=llm_router,
)
else:
response = await litellm.afile_retrieve(
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 5f66b03aad4..82c6821eb02 100644
--- a/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
+++ b/tests/enterprise/litellm_enterprise/proxy/hooks/test_managed_files.py
@@ -590,3 +590,247 @@ def test_update_responses_input_with_multiple_file_ids():
assert updated_input[0]["content"][2]["file_id"] == regular_file_id
# Verify text content was preserved
assert updated_input[0]["content"][1]["text"] == "Compare these files"
+
+
+async def test_store_unified_file_id_with_none_file_object():
+ """
+ Test that store_unified_file_id works when file_object is None
+ (e.g., for batch output files that are stored before file metadata is available).
+ """
+ from litellm.proxy._types import UserAPIKeyAuth
+
+ prisma_client = AsyncMock()
+ prisma_client.db.litellm_managedfiletable.create = AsyncMock(return_value=MagicMock())
+ internal_usage_cache = MagicMock()
+ internal_usage_cache.async_set_cache = AsyncMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Store with file_object=None (simulating batch output file storage)
+ await proxy_managed_files.store_unified_file_id(
+ file_id="test-unified-file-id",
+ file_object=None,
+ litellm_parent_otel_span=None,
+ model_mappings={"model-123": "file-provider-xyz"},
+ user_api_key_dict=UserAPIKeyAuth(user_id="test-user"),
+ )
+
+ # Verify DB create was called with expected data (without file_object)
+ prisma_client.db.litellm_managedfiletable.create.assert_called_once()
+ call_args = prisma_client.db.litellm_managedfiletable.create.call_args
+ assert call_args.kwargs["data"]["unified_file_id"] == "test-unified-file-id"
+ assert "file_object" not in call_args.kwargs["data"]
+
+
+async def test_afile_delete_returns_provider_response_when_stored_file_object_none():
+ """
+ Test that afile_delete returns the provider's delete response when the
+ stored file_object is None (e.g., for batch output files).
+ """
+ from litellm.types.llms.openai import OpenAIFileObject
+
+ unified_file_id = "bGl0ZWxsbV9wcm94eTphcHBsaWNhdGlvbi9qc29uO3VuaWZpZWRfaWQsdGVzdC1pZDt0YXJnZXRfbW9kZWxfbmFtZXMsZ3B0LTRvO2xsbV9vdXRwdXRfZmlsZV9pZCxmaWxlLXByb3ZpZGVyLXh5ejtsbG1fb3V0cHV0X2ZpbGVfbW9kZWxfaWQsbW9kZWwtMTIz"
+
+ prisma_client = AsyncMock()
+ db_record = MagicMock()
+ db_record.model_mappings = '{"model-123": "file-provider-xyz"}'
+ prisma_client.db.litellm_managedfiletable.find_first = AsyncMock(return_value=db_record)
+ prisma_client.db.litellm_managedfiletable.delete = AsyncMock()
+
+ internal_usage_cache = MagicMock()
+ internal_usage_cache.async_get_cache = AsyncMock(return_value={
+ "unified_file_id": unified_file_id,
+ "model_mappings": {"model-123": "file-provider-xyz"},
+ "flat_model_file_ids": ["file-provider-xyz"],
+ "file_object": None,
+ "created_by": "test-user",
+ "updated_by": "test-user",
+ })
+ internal_usage_cache.async_set_cache = AsyncMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Mock the delete_unified_file_id to return None (simulating file_object=None)
+ proxy_managed_files.delete_unified_file_id = AsyncMock(return_value=None)
+
+ # Mock router response
+ provider_delete_response = OpenAIFileObject(
+ id="file-provider-xyz",
+ object="file",
+ bytes=1234,
+ created_at=1234567890,
+ filename="test.jsonl",
+ purpose="batch",
+ )
+
+ mock_router = MagicMock()
+ mock_router.afile_delete = AsyncMock(return_value=provider_delete_response)
+
+ result = await proxy_managed_files.afile_delete(
+ file_id=unified_file_id,
+ litellm_parent_otel_span=None,
+ llm_router=mock_router,
+ )
+
+ # Should return the provider response with the unified file ID
+ assert result is not None
+ assert result.id == unified_file_id
+
+
+async def test_afile_retrieve_fetches_from_provider_when_file_object_none():
+ """
+ Test that afile_retrieve fetches from the provider when the stored
+ file_object is None (e.g., for batch output files).
+ """
+ from litellm.types.llms.openai import OpenAIFileObject
+
+ prisma_client = AsyncMock()
+ internal_usage_cache = MagicMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Mock get_unified_file_id to return a stored object with file_object=None
+ stored_file = MagicMock()
+ stored_file.file_object = None
+ stored_file.model_mappings = {"model-123": "file-provider-xyz"}
+ proxy_managed_files.get_unified_file_id = AsyncMock(return_value=stored_file)
+
+ # Mock the router and provider response
+ provider_file_response = OpenAIFileObject(
+ id="file-provider-xyz",
+ object="file",
+ bytes=5678,
+ created_at=1234567890,
+ filename="output.jsonl",
+ purpose="batch_output",
+ )
+
+ mock_router = MagicMock()
+ mock_router.get_deployment_credentials_with_provider = MagicMock(return_value={
+ "api_key": "test-key",
+ "api_base": "https://api.openai.com",
+ })
+
+ with patch("litellm.afile_retrieve", new_callable=AsyncMock) as mock_afile_retrieve:
+ mock_afile_retrieve.return_value = provider_file_response
+
+ unified_file_id = "test-unified-file-id"
+ result = await proxy_managed_files.afile_retrieve(
+ file_id=unified_file_id,
+ litellm_parent_otel_span=None,
+ llm_router=mock_router,
+ )
+
+ # Should return the provider response with the unified file ID
+ assert result is not None
+ assert result.id == unified_file_id
+ mock_afile_retrieve.assert_called_once()
+
+
+async def test_afile_retrieve_raises_error_when_no_router_and_file_object_none():
+ """
+ Test that afile_retrieve raises an appropriate error when file_object is None
+ and no llm_router is provided to fetch from the provider.
+ """
+ prisma_client = AsyncMock()
+ internal_usage_cache = MagicMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Mock get_unified_file_id to return a stored object with file_object=None
+ stored_file = MagicMock()
+ stored_file.file_object = None
+ stored_file.model_mappings = {"model-123": "file-provider-xyz"}
+ proxy_managed_files.get_unified_file_id = AsyncMock(return_value=stored_file)
+
+ unified_file_id = "test-unified-file-id"
+
+ with pytest.raises(Exception) as exc_info:
+ await proxy_managed_files.afile_retrieve(
+ file_id=unified_file_id,
+ litellm_parent_otel_span=None,
+ llm_router=None,
+ )
+
+ assert "llm_router is required" in str(exc_info.value)
+
+
+async def test_afile_retrieve_returns_stored_file_object_when_exists():
+ """
+ Test that afile_retrieve returns the stored file_object directly when it exists
+ (the normal case for user-uploaded files).
+ """
+ from litellm.types.llms.openai import OpenAIFileObject
+
+ prisma_client = AsyncMock()
+ internal_usage_cache = MagicMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Mock get_unified_file_id to return a stored object WITH file_object
+ stored_file_object = OpenAIFileObject(
+ id="test-unified-file-id",
+ object="file",
+ bytes=1234,
+ created_at=1234567890,
+ filename="input.jsonl",
+ purpose="batch",
+ )
+ stored_file = MagicMock()
+ stored_file.file_object = stored_file_object
+ proxy_managed_files.get_unified_file_id = AsyncMock(return_value=stored_file)
+
+ result = await proxy_managed_files.afile_retrieve(
+ file_id="test-unified-file-id",
+ litellm_parent_otel_span=None,
+ llm_router=None,
+ )
+
+ # Should return the stored file object directly
+ assert result == stored_file_object
+
+
+async def test_afile_retrieve_raises_error_for_non_managed_file():
+ """
+ Test that afile_retrieve raises an error when the file_id is not found
+ in the managed files table.
+ """
+ prisma_client = AsyncMock()
+ internal_usage_cache = MagicMock()
+
+ proxy_managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=internal_usage_cache,
+ prisma_client=prisma_client,
+ )
+
+ # Mock get_unified_file_id to return None (file not found)
+ proxy_managed_files.get_unified_file_id = AsyncMock(return_value=None)
+
+ with pytest.raises(Exception) as exc_info:
+ await proxy_managed_files.afile_retrieve(
+ file_id="non-existent-file-id",
+ litellm_parent_otel_span=None,
+ )
+
+ assert "not found" in str(exc_info.value)