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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: afile_retrieve returns unified ID for batch output files
Ephrim Stanleye59c8d22afe9
diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.pyindex e11e827d454..32d7313aae4 100644--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py@@ -1011,6 +1011,7 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):# Case 2: Managed file and the file object exists in the databaseif stored_file_object and stored_file_object.file_object:+ stored_file_object.file_object.id = file_idreturn stored_file_object.file_object# Case 3: Managed file exists in the database but not the file object (for. e.g the batch task might not have run)diff --git a/fix_afile_retrieve_returns_unified_id.md b/fix_afile_retrieve_returns_unified_id.mdnew file mode 100644index 00000000000..c8cc72d6f99--- /dev/null+++ b/fix_afile_retrieve_returns_unified_id.md@@ -0,0 +1,47 @@+# Fix: afile_retrieve returns raw provider ID for batch output files++## Bug++`managed_files.afile_retrieve()` Case 2 (file_object already in DB) returned the stored `file_object` without replacing `.id` with the unified file ID. Case 3 (fetch from provider) did this correctly at line 1028.++## Fix++One-line change in `enterprise/litellm_enterprise/proxy/hooks/managed_files.py`:++```python+# Before (line 1013-1014)+if stored_file_object and stored_file_object.file_object:+ return stored_file_object.file_object++# After+if stored_file_object and stored_file_object.file_object:+ stored_file_object.file_object.id = file_id+ return stored_file_object.file_object+```++## Test++```bash+poetry run pytest tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py -s -vvvv+```++## Test failure (before fix)++```+FAILED tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py::test_should_return_unified_id_when_file_object_exists_in_db+AssertionError: afile_retrieve should return the unified ID 'bGl0ZWxsbV9wcm94eTp1bmlmaWVkX291dHB1dF9maWxl', but got raw provider ID 'batch_20260214-output-file-1'+assert 'batch_20260214-output-file-1' == 'bGl0ZWxsbV9wcm94eTp1bmlmaWVkX291dHB1dF9maWxl'+=================== 1 failed, 1 retried in 102.95s ===================+```++## Test pass (after fix)++```+tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py::test_should_return_unified_id_when_file_object_exists_in_db PASSED+============================== 1 passed in 0.11s ===============================+```++## Files changed++- `enterprise/litellm_enterprise/proxy/hooks/managed_files.py` — one-line fix+- `tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py` — new testdiff --git a/tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py b/tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.pynew file mode 100644index 00000000000..7040aef73e5--- /dev/null+++ b/tests/test_litellm/enterprise/proxy/test_afile_retrieve_returns_unified_id.py@@ -0,0 +1,67 @@+"""+Test that managed_files.afile_retrieve returns the unified file ID, not the+raw provider file ID, when file_object is already stored in the database.++Bug: managed_files.py Case 2 returns stored_file_object.file_object directly+without replacing .id with the unified ID. Case 3 (fetch from provider) does+it correctly at line 1028.+"""++import pytest+from unittest.mock import AsyncMock, MagicMock++from litellm.proxy._types import LiteLLM_ManagedFileTable+from litellm.types.llms.openai import OpenAIFileObject+++def _make_managed_files_instance():+ from litellm_enterprise.proxy.hooks.managed_files import (+ _PROXY_LiteLLMManagedFiles,+ )++ instance = _PROXY_LiteLLMManagedFiles(+ internal_usage_cache=MagicMock(),+ prisma_client=MagicMock(),+ )+ return instance+++async def test_should_return_unified_id_when_file_object_exists_in_db():+ """+ When get_unified_file_id returns a stored file_object (Case 2),+ afile_retrieve must set .id to the unified file ID before returning.+ """+ unified_id = "bGl0ZWxsbV9wcm94eTp1bmlmaWVkX291dHB1dF9maWxl"+ raw_provider_id = "batch_20260214-output-file-1"++ stored = LiteLLM_ManagedFileTable(+ unified_file_id=unified_id,+ file_object=OpenAIFileObject(+ id=raw_provider_id,+ bytes=489,+ created_at=1700000000,+ filename="batch_output.jsonl",+ object="file",+ purpose="batch_output",+ status="processed",+ ),+ model_mappings={"model-abc": raw_provider_id},+ flat_model_file_ids=[raw_provider_id],+ created_by="test-user",+ updated_by="test-user",+ )++ managed_files = _make_managed_files_instance()+ managed_files.get_unified_file_id = AsyncMock(return_value=stored)++ result = await managed_files.afile_retrieve(+ file_id=unified_id,+ litellm_parent_otel_span=None,+ llm_router=None,+ )++ assert result.id == unified_id, (+ f"afile_retrieve should return the unified ID '{unified_id}', "+ f"but got raw provider ID '{result.id}'"+ )