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): register managed batch output files on terminal retrieve (#34092)

Souravrajvi0388943ac179f
diff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.py
index dd84b1c1a8d..deed665d3f2 100644
--- a/litellm/proxy/batches_endpoints/endpoints.py
+++ b/litellm/proxy/batches_endpoints/endpoints.py
@@ -27,6 +27,7 @@ from litellm.proxy.openai_files_endpoints.common_utils import (
decode_model_from_file_id,
encode_batch_response_ids,
encode_file_id_with_model,
+ ensure_batch_response_managed_file_ids,
get_batch_from_database,
get_batch_id_from_unified_batch_id,
get_credentials_for_model,
@@ -34,8 +35,6 @@ 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,
- resolve_output_file_ids_to_unified,
update_batch_in_database,
)
from litellm.proxy.utils import handle_exception_on_proxy, is_known_model
@@ -441,10 +440,16 @@ async def retrieve_batch(
)
# The DB may store raw provider file IDs (before hooks translate them).
- # Resolve any raw input/output/error file IDs to unified IDs.
+ # Register any missing managed-file rows and return unified IDs.
if unified_batch_id:
- await resolve_input_file_id_to_unified(response, prisma_client)
- await resolve_output_file_ids_to_unified(response, prisma_client)
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=managed_files_obj,
+ prisma_client=prisma_client,
+ verbose_proxy_logger=verbose_proxy_logger,
+ db_batch_object=db_batch_object,
+ unified_batch_id=unified_batch_id,
+ )
asyncio.create_task(
proxy_logging_obj.update_request_status(
@@ -562,10 +567,16 @@ async def retrieve_batch(
)
# Fix: bug_feb14_batch_retrieve_returns_raw_input_file_id
- # Resolve raw provider file IDs (input, output, error) to unified IDs.
+ # Register any missing managed-file rows and return unified IDs.
if unified_batch_id:
- await resolve_input_file_id_to_unified(response, prisma_client)
- await resolve_output_file_ids_to_unified(response, prisma_client)
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=managed_files_obj,
+ prisma_client=prisma_client,
+ verbose_proxy_logger=verbose_proxy_logger,
+ db_batch_object=db_batch_object,
+ unified_batch_id=unified_batch_id,
+ )
### 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 412d75cc82e..37b51e1d3af 100644
--- a/litellm/proxy/openai_files_endpoints/common_utils.py
+++ b/litellm/proxy/openai_files_endpoints/common_utils.py
@@ -13,9 +13,11 @@ from litellm.types.utils import SpecialEnums
if TYPE_CHECKING:
from fastapi import Request
+ from prisma.models import LiteLLM_ManagedObjectTable
from litellm.proxy._types import UserAPIKeyAuth
from litellm.router import Router
+ from litellm.types.utils import LiteLLMBatch
def _is_base64_encoded_unified_file_id(b64_uid: str) -> str | Literal[False]:
@@ -883,6 +885,72 @@ def _extract_model_param(request: "Request", request_body: dict) -> str | None:
# ============================================================================
+def _batch_response_model_id_candidates(
+ response,
+ unified_batch_id: str | Literal[False] | None,
+) -> tuple[str, ...]:
+ response_id: Final = getattr(response, "id", None)
+ decoded_response_id: Final = (
+ _is_base64_encoded_unified_file_id(response_id) if isinstance(response_id, str) else False
+ )
+ return tuple(
+ candidate
+ for candidate in (
+ unified_batch_id if isinstance(unified_batch_id, str) else None,
+ decoded_response_id or None,
+ response_id
+ if isinstance(response_id, str)
+ and not decoded_response_id
+ and response_id.startswith(SpecialEnums.LITELM_MANAGED_FILE_ID_PREFIX.value)
+ else None,
+ )
+ if candidate
+ )
+
+
+def _model_id_for_batch_response(
+ response: "LiteLLMBatch",
+ unified_batch_id: str | Literal[False] | None,
+) -> str | None:
+ hidden_params: Final = getattr(response, "_hidden_params", None) or {}
+ model_id: Final = hidden_params.get("model_id")
+ if model_id:
+ return model_id
+ return next(
+ (
+ candidate_model_id
+ for candidate in _batch_response_model_id_candidates(response, unified_batch_id)
+ if (candidate_model_id := get_model_id_from_unified_batch_id(candidate))
+ ),
+ None,
+ )
+
+
+def _model_name_for_batch_response(response: "LiteLLMBatch") -> str | None:
+ hidden_params: Final = getattr(response, "_hidden_params", None) or {}
+ model_name: Final = hidden_params.get("model_name")
+ if model_name:
+ return model_name
+ unified_file_id: Final = hidden_params.get("unified_file_id")
+ if not isinstance(unified_file_id, str):
+ return None
+ decoded_unified_file_id: Final = _is_base64_encoded_unified_file_id(unified_file_id) or unified_file_id
+ target_model_names: Final = get_models_from_unified_file_id(decoded_unified_file_id)
+ if target_model_names:
+ return ",".join(target_model_names)
+ return None
+
+
+def _batch_owner_auth_from_db_object(db_batch_object: "LiteLLM_ManagedObjectTable") -> "UserAPIKeyAuth | None":
+ from litellm.proxy._types import UserAPIKeyAuth
+
+ created_by: Final = getattr(db_batch_object, "created_by", None)
+ if not isinstance(created_by, str) or not created_by:
+ return None
+ raw_team_id: Final = getattr(db_batch_object, "team_id", None)
+ return UserAPIKeyAuth(user_id=created_by, team_id=raw_team_id if isinstance(raw_team_id, str) else None)
+
+
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
@@ -934,6 +1002,7 @@ async def ensure_batch_response_managed_file_ids(
verbose_proxy_logger,
user_api_key_dict=None,
db_batch_object=None,
+ unified_batch_id: str | Literal[False] | None = None,
) -> None:
"""Normalize batch file IDs to managed unified IDs before DB persistence."""
await resolve_input_file_id_to_unified(response, prisma_client)
@@ -942,27 +1011,15 @@ async def ensure_batch_response_managed_file_ids(
if managed_files_obj is None:
return
- hidden_params: Final = getattr(response, "_hidden_params", None) or {}
- model_id: Final = hidden_params.get("model_id")
+ model_id: Final = _model_id_for_batch_response(response, unified_batch_id)
if not model_id:
return
- model_name = hidden_params.get("model_name")
- unified_file_id: Final = hidden_params.get("unified_file_id")
- if not model_name and isinstance(unified_file_id, str):
- decoded_unified_file_id: Final = _is_base64_encoded_unified_file_id(unified_file_id) or unified_file_id
- target_model_names: Final = get_models_from_unified_file_id(decoded_unified_file_id)
- if target_model_names:
- model_name = ",".join(target_model_names)
-
- if user_api_key_dict is None and db_batch_object is not None:
- from litellm.proxy._types import UserAPIKeyAuth
-
- user_api_key_dict = UserAPIKeyAuth(
- user_id=getattr(db_batch_object, "created_by", None) or "default-user-id",
- team_id=getattr(db_batch_object, "team_id", None),
- )
- if user_api_key_dict is None:
+ model_name: Final = _model_name_for_batch_response(response)
+
+ owner_auth: Final = _batch_owner_auth_from_db_object(db_batch_object) if db_batch_object is not None else None
+ effective_auth: Final = owner_auth if owner_auth is not None else user_api_key_dict
+ if effective_auth is None:
return
for file_attr in ("output_file_id", "error_file_id"):
@@ -978,9 +1035,9 @@ async def ensure_batch_response_managed_file_ids(
await managed_files_obj.store_unified_file_id(
file_id=new_unified_file_id,
file_object=None,
- litellm_parent_otel_span=getattr(user_api_key_dict, "parent_otel_span", None),
+ litellm_parent_otel_span=getattr(effective_auth, "parent_otel_span", None),
model_mappings={model_id: raw_file_id},
- user_api_key_dict=user_api_key_dict,
+ user_api_key_dict=effective_auth,
)
setattr(response, file_attr, new_unified_file_id)
verbose_proxy_logger.debug("Converted batch %s %r to managed ID before DB write", file_attr, raw_file_id)
@@ -1039,8 +1096,16 @@ async def get_batch_from_database(
response: Final = LiteLLMBatch.model_validate(batch_data)
response.id = batch_id
- # The stored batch object has the raw provider input_file_id. Resolve to unified ID.
- await resolve_input_file_id_to_unified(response, prisma_client)
+ # The stored batch object may have raw provider file IDs. Register any missing
+ # managed-file rows and normalize output/error IDs before returning.
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=managed_files_obj,
+ prisma_client=prisma_client,
+ verbose_proxy_logger=verbose_proxy_logger,
+ db_batch_object=db_batch_object,
+ unified_batch_id=unified_batch_id,
+ )
verbose_proxy_logger.debug(
"Retrieved batch %s from ManagedObjectTable with status=%s", batch_id, response.status
diff --git a/tests/test_litellm/enterprise/proxy/test_batch_retrieve_registers_missing_output_file_id.py b/tests/test_litellm/enterprise/proxy/test_batch_retrieve_registers_missing_output_file_id.py
new file mode 100644
index 00000000000..55a4d05946e
--- /dev/null
+++ b/tests/test_litellm/enterprise/proxy/test_batch_retrieve_registers_missing_output_file_id.py
@@ -0,0 +1,261 @@
+"""Regression for #33989: terminal batch retrieve must register missing managed-file rows."""
+
+import base64
+import json
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from litellm.proxy._types import UserAPIKeyAuth
+from litellm.proxy.openai_files_endpoints.common_utils import (
+ ensure_batch_response_managed_file_ids,
+ get_batch_from_database,
+)
+from litellm.types.utils import LiteLLMBatch
+
+
+UNIFIED_BATCH_ID = "litellm_proxy;model_id:my-model;llm_batch_id:batch-raw-123"
+ENCODED_UNIFIED_BATCH_ID = (
+ base64.urlsafe_b64encode(UNIFIED_BATCH_ID.encode()).decode().rstrip("=")
+)
+
+
+def _build_batch_response(
+ *,
+ output_file_id: str = "file-rawoutput789",
+ error_file_id: str | None = None,
+) -> LiteLLMBatch:
+ return LiteLLMBatch(
+ id="batch-raw-123",
+ object="batch",
+ status="completed",
+ endpoint="/v1/chat/completions",
+ input_file_id="file-input123",
+ output_file_id=output_file_id,
+ error_file_id=error_file_id,
+ completion_window="24h",
+ created_at=1234567890,
+ )
+
+
+def _build_managed_files_mock(unified_id: str = "file-bWFuYWdlZF9vdXRwdXRfaWQ="):
+ mock = MagicMock()
+ mock.get_unified_output_file_id = MagicMock(return_value=unified_id)
+ mock.store_unified_file_id = AsyncMock()
+ return mock
+
+
+def _build_prisma_mock():
+ mock = MagicMock()
+ mock.db.litellm_managedfiletable.find_first = AsyncMock(return_value=None)
+ return mock
+
+
+async def test_ensure_batch_response_derives_model_id_from_unified_batch_id():
+ raw_output_file_id = "file-rawoutput789"
+ unified_output_file_id = "file-bWFuYWdlZF9vdXRwdXRfaWQ="
+ response = _build_batch_response(output_file_id=raw_output_file_id)
+
+ mock_managed_files = _build_managed_files_mock(unified_id=unified_output_file_id)
+ db_batch_object = SimpleNamespace(
+ created_by="batch-owner", team_id="team-owner", status="completed"
+ )
+
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=mock_managed_files,
+ prisma_client=_build_prisma_mock(),
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=db_batch_object,
+ unified_batch_id=UNIFIED_BATCH_ID,
+ )
+
+ assert response.output_file_id == unified_output_file_id
+ mock_managed_files.store_unified_file_id.assert_called_once()
+ store_kwargs = mock_managed_files.store_unified_file_id.call_args.kwargs
+ assert store_kwargs["model_mappings"] == {"my-model": raw_output_file_id}
+ assert store_kwargs["user_api_key_dict"].user_id == "batch-owner"
+ assert store_kwargs["user_api_key_dict"].team_id == "team-owner"
+
+
+async def test_ensure_batch_response_registers_output_and_error_file_ids():
+ unified_id = "file-bWFuYWdlZF9vdXRwdXRfaWQ="
+ response = _build_batch_response(
+ output_file_id="file-raw-output",
+ error_file_id="file-raw-error",
+ )
+ mock_managed_files = _build_managed_files_mock(unified_id=unified_id)
+
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=mock_managed_files,
+ prisma_client=_build_prisma_mock(),
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=SimpleNamespace(created_by="batch-owner", team_id=None),
+ unified_batch_id=UNIFIED_BATCH_ID,
+ )
+
+ assert response.output_file_id == unified_id
+ assert response.error_file_id == unified_id
+ assert mock_managed_files.store_unified_file_id.call_count == 2
+ mappings = [
+ call.kwargs["model_mappings"]
+ for call in mock_managed_files.store_unified_file_id.call_args_list
+ ]
+ assert {"my-model": "file-raw-output"} in mappings
+ assert {"my-model": "file-raw-error"} in mappings
+
+
+async def test_get_batch_from_database_registers_missing_output_file_id():
+ raw_output_file_id = "file-output-raw"
+ unified_output_file_id = "file-bWFuYWdlZF9vdXRwdXRfaWQ="
+ batch_data = {
+ "id": "batch-raw-123",
+ "completion_window": "24h",
+ "created_at": 1700000000,
+ "endpoint": "/v1/chat/completions",
+ "input_file_id": "file-input-raw",
+ "object": "batch",
+ "status": "completed",
+ "output_file_id": raw_output_file_id,
+ }
+
+ batch_db_record = SimpleNamespace(
+ file_object=json.dumps(batch_data),
+ created_by="batch-owner",
+ team_id="team-owner",
+ status="complete",
+ )
+ prisma = MagicMock()
+ prisma.db.litellm_managedobjecttable.find_first = AsyncMock(
+ return_value=batch_db_record
+ )
+ prisma.db.litellm_managedfiletable.find_first = AsyncMock(return_value=None)
+
+ mock_managed_files = _build_managed_files_mock(unified_id=unified_output_file_id)
+
+ _, response = await get_batch_from_database(
+ batch_id=ENCODED_UNIFIED_BATCH_ID,
+ unified_batch_id=UNIFIED_BATCH_ID,
+ managed_files_obj=mock_managed_files,
+ prisma_client=prisma,
+ verbose_proxy_logger=MagicMock(),
+ )
+
+ assert response is not None
+ assert response.output_file_id == unified_output_file_id
+ mock_managed_files.store_unified_file_id.assert_called_once()
+ store_kwargs = mock_managed_files.store_unified_file_id.call_args.kwargs
+ assert store_kwargs["model_mappings"] == {"my-model": raw_output_file_id}
+ assert store_kwargs["user_api_key_dict"].user_id == "batch-owner"
+
+
+async def test_ensure_batch_response_uses_batch_owner_when_db_batch_object_present():
+ response = _build_batch_response(output_file_id="file-raw-output")
+ mock_managed_files = _build_managed_files_mock()
+
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=mock_managed_files,
+ prisma_client=_build_prisma_mock(),
+ verbose_proxy_logger=MagicMock(),
+ user_api_key_dict=UserAPIKeyAuth(user_id="other-user", team_id="other-team"),
+ db_batch_object=SimpleNamespace(
+ created_by="batch-owner", team_id="team-owner", status="completed"
+ ),
+ unified_batch_id=UNIFIED_BATCH_ID,
+ )
+
+ # batch owner from db_batch_object wins over the caller auth context
+ forwarded_auth = mock_managed_files.store_unified_file_id.call_args.kwargs[
+ "user_api_key_dict"
+ ]
+ assert forwarded_auth.user_id == "batch-owner"
+ assert forwarded_auth.team_id == "team-owner"
+
+
+async def test_registered_output_file_row_denies_cross_user_access():
+ from litellm_enterprise.proxy.hooks.managed_files import (
+ _PROXY_LiteLLMManagedFiles,
+ )
+
+ raw_output_file_id = "file-raw-output"
+ prisma = MagicMock()
+ prisma.db.litellm_managedfiletable.upsert = AsyncMock()
+ prisma.db.litellm_managedfiletable.find_first = AsyncMock(return_value=None)
+ managed_files = _PROXY_LiteLLMManagedFiles(
+ internal_usage_cache=MagicMock(),
+ prisma_client=prisma,
+ )
+ response = _build_batch_response(output_file_id=raw_output_file_id)
+
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=managed_files,
+ prisma_client=prisma,
+ verbose_proxy_logger=MagicMock(),
+ user_api_key_dict=UserAPIKeyAuth(user_id="other-user", team_id="other-team"),
+ db_batch_object=SimpleNamespace(
+ created_by="batch-owner", team_id="team-owner", status="completed"
+ ),
+ unified_batch_id=UNIFIED_BATCH_ID,
+ )
+
+ unified_output_file_id = response.output_file_id
+ assert unified_output_file_id != raw_output_file_id
+ registered_row = prisma.db.litellm_managedfiletable.upsert.call_args.kwargs[
+ "data"
+ ]["create"]
+ assert registered_row["unified_file_id"] == unified_output_file_id
+ assert registered_row["created_by"] == "batch-owner"
+ assert registered_row["team_id"] == "team-owner"
+
+ prisma.db.litellm_managedfiletable.find_first = AsyncMock(
+ return_value=SimpleNamespace(**registered_row)
+ )
+
+ assert (
+ await managed_files.can_user_call_unified_file_id(
+ unified_file_id=unified_output_file_id,
+ user_api_key_dict=UserAPIKeyAuth(user_id="batch-owner"),
+ )
+ is True
+ )
+ assert (
+ await managed_files.can_user_call_unified_file_id(
+ unified_file_id=unified_output_file_id,
+ user_api_key_dict=UserAPIKeyAuth(
+ user_id="intruder-user", team_id="intruder-team"
+ ),
+ )
+ is False
+ )
+
+
+async def test_ensure_batch_response_derives_model_id_from_encoded_response_id():
+ unified_id = "file-bWFuYWdlZF9vdXRwdXRfaWQ="
+ response = _build_batch_response(output_file_id="file-raw-output")
+ response.id = ENCODED_UNIFIED_BATCH_ID
+
+ mock_managed_files = _build_managed_files_mock(unified_id=unified_id)
+
+ await ensure_batch_response_managed_file_ids(
+ response=response,
+ managed_files_obj=mock_managed_files,
+ prisma_client=_build_prisma_mock(),
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=SimpleNamespace(created_by="batch-owner", team_id=None),
+ )
+
+ assert response.output_file_id == unified_id
+ assert (
+ mock_managed_files.get_unified_output_file_id.call_args.kwargs["model_id"]
+ == "my-model"
+ )
diff --git a/tests/test_litellm/enterprise/proxy/test_batch_retrieve_returns_unified_input_file_id.py b/tests/test_litellm/enterprise/proxy/test_batch_retrieve_returns_unified_input_file_id.py
index 420f5f9789c..5d1b63a815e 100644
--- a/tests/test_litellm/enterprise/proxy/test_batch_retrieve_returns_unified_input_file_id.py
+++ b/tests/test_litellm/enterprise/proxy/test_batch_retrieve_returns_unified_input_file_id.py
@@ -78,9 +78,12 @@ async def test_should_resolve_raw_input_file_id_to_unified_id():
f"got raw: '{response.input_file_id}'"
)
- prisma.db.litellm_managedfiletable.find_first.assert_called_once_with(
+ prisma.db.litellm_managedfiletable.find_first.assert_any_call(
where={"flat_model_file_ids": {"has": raw_input_file_id}}
)
+ prisma.db.litellm_managedfiletable.find_first.assert_any_call(
+ where={"flat_model_file_ids": {"has": "file-output-raw"}}
+ )
@pytest.mark.asyncio
diff --git a/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py b/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
index 6a185988c9b..f2d37fbe842 100644
--- a/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
+++ b/tests/test_litellm/proxy/batches_endpoints/test_endpoints.py
@@ -935,8 +935,7 @@ class RetrieveHarness:
creds_resolver: MagicMock
get_batch_from_db: AsyncMock
update_batch_in_db: AsyncMock
- resolve_input: AsyncMock
- resolve_output: AsyncMock
+ ensure_managed_files: AsyncMock
@property
def router_aretrieve(self) -> AsyncMock:
@@ -973,8 +972,7 @@ def retrieve_harness():
# Default: DB miss -> always fall through to provider routing.
get_batch_from_db = AsyncMock(return_value=(None, None))
update_batch_in_db = AsyncMock(return_value=None)
- resolve_input = AsyncMock(return_value=None)
- resolve_output = AsyncMock(return_value=None)
+ ensure_managed_files = AsyncMock(return_value=None)
with ExitStack() as stack:
stack.enter_context(
@@ -1001,8 +999,7 @@ def retrieve_harness():
)
stack.enter_context(patch.object(endpoints, "get_batch_from_database", get_batch_from_db))
stack.enter_context(patch.object(endpoints, "update_batch_in_database", update_batch_in_db))
- stack.enter_context(patch.object(endpoints, "resolve_input_file_id_to_unified", resolve_input))
- stack.enter_context(patch.object(endpoints, "resolve_output_file_ids_to_unified", resolve_output))
+ stack.enter_context(patch.object(endpoints, "ensure_batch_response_managed_file_ids", ensure_managed_files))
stack.enter_context(patch.object(litellm, "aretrieve_batch", litellm_aretrieve))
stack.enter_context(patch.object(litellm, "enable_loadbalancing_on_batch_endpoints", False))
stack.enter_context(patch.object(proxy_server, "llm_router", router))
@@ -1024,8 +1021,7 @@ def retrieve_harness():
creds_resolver=router.get_deployment_credentials_with_provider,
get_batch_from_db=get_batch_from_db,
update_batch_in_db=update_batch_in_db,
- resolve_input=resolve_input,
- resolve_output=resolve_output,
+ ensure_managed_files=ensure_managed_files,
)
@@ -1150,9 +1146,8 @@ async def test_retrieve__unified_batch_id_routes_to_router(retrieve_harness):
assert resp._hidden_params["unified_batch_id"] == UNIFIED_BATCH_ID
assert resp._hidden_params["model_id"] == "gpt-4o-mini"
- # raw provider file ids on the response are resolved back to unified ids.
- retrieve_harness.resolve_input.assert_called_once()
- retrieve_harness.resolve_output.assert_called_once()
+ # raw provider file ids on the response are normalized to managed unified ids.
+ retrieve_harness.ensure_managed_files.assert_called_once()
@pytest.mark.asyncio
@@ -1168,9 +1163,8 @@ async def test_retrieve__loadbalancing_raw_id_routes_to_router(retrieve_harness)
# not a unified id -> hidden param reflects that, no model_id stamped.
assert resp._hidden_params["unified_batch_id"] is False
assert "model_id" not in resp._hidden_params
- # not a unified id -> no file-id resolution.
- retrieve_harness.resolve_input.assert_not_called()
- retrieve_harness.resolve_output.assert_not_called()
+ # not a unified id -> no managed-file normalization.
+ retrieve_harness.ensure_managed_files.assert_not_called()
# --------------------------------------------------------------------------- #
@@ -1261,14 +1255,18 @@ async def test_retrieve__db_terminal_state_short_circuits(retrieve_harness, stat
@pytest.mark.asyncio
async def test_retrieve__db_terminal_unified_resolves_file_ids(retrieve_harness):
db_response = make_batch(id="batch-from-db", status="completed")
- retrieve_harness.get_batch_from_db.return_value = (MagicMock(), db_response)
+ db_batch_object = MagicMock()
+ retrieve_harness.get_batch_from_db.return_value = (db_batch_object, db_response)
with patch.object(endpoints, "_is_base64_encoded_unified_file_id", return_value=UNIFIED_BATCH_ID):
await call_retrieve(retrieve_harness, "batch-unified-blob")
- # Terminal short-circuit still resolves raw provider file ids to unified.
- retrieve_harness.resolve_input.assert_called_once()
- retrieve_harness.resolve_output.assert_called_once()
+ # Terminal short-circuit still registers/normalizes raw provider file ids.
+ retrieve_harness.ensure_managed_files.assert_called_once()
+ ensure_kwargs = retrieve_harness.ensure_managed_files.call_args.kwargs
+ assert ensure_kwargs["response"] is db_response
+ assert ensure_kwargs["db_batch_object"] is db_batch_object
+ assert ensure_kwargs["unified_batch_id"] == UNIFIED_BATCH_ID
retrieve_harness.litellm_aretrieve.assert_not_called()
retrieve_harness.router_aretrieve.assert_not_called()