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(managed_files): resolve model_name identically across all output file registration paths so full unified ids converge
mateo-berrif3bfa19ce5d0
diff --git a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.pyindex 22f9f40ecd8..3ed63b0d9ee 100644--- a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py+++ b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py@@ -296,17 +296,13 @@ class CheckBatchCost:underlying provider model (e.g. ``gpt-5.5``), which no key is allowed to call."""from litellm.proxy.openai_files_endpoints.common_utils import (- convert_b64_uid_to_unified_uid,- get_models_from_unified_file_id,+ resolve_managed_output_file_model_name,)- input_file_id = cls._get_input_file_id(job)- target_model_names = (- get_models_from_unified_file_id(convert_b64_uid_to_unified_uid(input_file_id)) if input_file_id else []+ return resolve_managed_output_file_model_name(+ unified_input_file_id=cls._get_input_file_id(job),+ fallback_model_name=deployment_info.model_name or None,)- if target_model_names:- return ",".join(target_model_names)- return deployment_info.model_name or None@staticmethoddef _get_input_file_id(job: "LiteLLM_ManagedObjectTable") -> Optional[str]:diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.pyindex 5fe7b38c06a..404dba6b919 100644--- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py+++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py@@ -34,8 +34,8 @@ from litellm.proxy.openai_files_endpoints.common_utils import (get_batch_id_from_unified_batch_id,get_content_type_from_file_object,get_model_id_from_unified_batch_id,- get_models_from_unified_file_id,normalize_mime_type_for_provider,+ resolve_managed_output_file_model_name,)from litellm.types.llms.openai import ( # pyright: ignore[reportAttributeAccessIssue]AllMessageValues,@@ -1102,21 +1102,13 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):) # managed batch idmodel_id = cast(Optional[str], response._hidden_params.get("model_id"))model_name = cast(Optional[str], response._hidden_params.get("model_name"))- resolved_model_name = model_name-- # Some providers (e.g. Vertex batch retrieve) do not set model_name on- # the response. In that case, recover target_model_names from the input- # managed file metadata so unified output IDs preserve routing metadata.- if not resolved_model_name and isinstance(unified_file_id, str):- decoded_unified_file_id = (- _is_base64_encoded_unified_file_id(unified_file_id)- or unified_file_id- )- target_model_names = get_models_from_unified_file_id(- decoded_unified_file_id- )- if target_model_names:- resolved_model_name = ",".join(target_model_names)++ resolved_model_name = resolve_managed_output_file_model_name(+ unified_input_file_id=unified_file_id+ if isinstance(unified_file_id, str)+ else None,+ fallback_model_name=model_name,+ )original_response_id = response.idif (unified_batch_id or unified_file_id) and model_id:diff --git a/litellm/proxy/openai_files_endpoints/common_utils.py b/litellm/proxy/openai_files_endpoints/common_utils.pyindex 412d75cc82e..0dd2c65bba6 100644--- a/litellm/proxy/openai_files_endpoints/common_utils.py+++ b/litellm/proxy/openai_files_endpoints/common_utils.py@@ -43,6 +43,17 @@ def convert_b64_uid_to_unified_uid(b64_uid: str) -> str:return b64_uid+def resolve_managed_output_file_model_name(+ unified_input_file_id: str | None, fallback_model_name: str | None+) -> str | None:+ if not unified_input_file_id:+ return fallback_model_name+ target_model_names: Final = get_models_from_unified_file_id(convert_b64_uid_to_unified_uid(unified_input_file_id))+ if target_model_names:+ return ",".join(target_model_names)+ return fallback_model_name++def get_models_from_unified_file_id(unified_file_id: str) -> list[str]:"""Extract model names from unified file ID.@@ -947,13 +958,11 @@ async def ensure_batch_response_managed_file_ids(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)+ model_name: Final = resolve_managed_output_file_model_name(+ unified_input_file_id=unified_file_id if isinstance(unified_file_id, str) else None,+ fallback_model_name=hidden_params.get("model_name"),+ )if user_api_key_dict is None and db_batch_object is not None:from litellm.proxy._types import UserAPIKeyAuthdiff --git a/tests/test_litellm/enterprise/proxy/test_managed_files_hook.py b/tests/test_litellm/enterprise/proxy/test_managed_files_hook.pyindex 2a1b6ee222e..26df6463351 100644--- a/tests/test_litellm/enterprise/proxy/test_managed_files_hook.py+++ b/tests/test_litellm/enterprise/proxy/test_managed_files_hook.py@@ -6,6 +6,7 @@ async_post_call_success_hook when processing completed batch responses."""import asyncio+import base64import jsonimport pytest@@ -522,3 +523,80 @@ async def test_concurrent_first_registrations_converge_on_one_row():}assert minted_ids[0] == minted_ids[1]assert upserted_row_keys == {minted_ids[0]}+++def _b64_unified_input_file_id(target_model_names: str) -> str:+ unified_input_file_id = (+ "litellm_proxy:application/octet-stream;unified_id,input-uuid;"+ f"target_model_names,{target_model_names}"+ )+ return base64.urlsafe_b64encode(unified_input_file_id.encode()).decode().rstrip("=")+++async def test_hook_mint_prefers_input_file_target_model_names():+ managed_files = _make_managed_files_instance()+ batch_response = _make_batch_response(model_name="model-a")+ batch_response._hidden_params["unified_file_id"] = _b64_unified_input_file_id(+ "model-a,model-b"+ )++ mock_router = MagicMock()+ mock_router.get_deployment_credentials_with_provider = MagicMock(return_value={})++ with (+ patch("litellm.afile_retrieve", AsyncMock(return_value=_make_file_object())),+ patch("litellm.proxy.proxy_server.llm_router", mock_router),+ ):+ await managed_files.async_post_call_success_hook(+ data={},+ user_api_key_dict=_make_user_api_key_dict(),+ response=batch_response,+ )++ assert batch_response.output_file_id == managed_files.get_unified_output_file_id(+ output_file_id="file-output-abc",+ model_id="model-deploy-xyz",+ model_name="model-a,model-b",+ )+++def test_cost_job_and_retrieve_paths_mint_identical_unified_output_file_ids():+ from litellm.proxy.openai_files_endpoints.common_utils import (+ resolve_managed_output_file_model_name,+ )+ from litellm_enterprise.proxy.common_utils.check_batch_cost import CheckBatchCost++ managed_files = _make_managed_files_instance()+ unified_input_file_id = _b64_unified_input_file_id("model-a,model-b")++ retrieve_path_model_name = resolve_managed_output_file_model_name(+ unified_input_file_id=unified_input_file_id,+ fallback_model_name="model-a",+ )++ job = MagicMock()+ job.file_object = {+ "id": "batch-123",+ "completion_window": "24h",+ "created_at": 1700000000,+ "endpoint": "/v1/chat/completions",+ "input_file_id": unified_input_file_id,+ "object": "batch",+ "status": "completed",+ }+ cost_job_model_name = CheckBatchCost._get_managed_file_model_name(+ job=job, deployment_info=MagicMock(model_name="model-a")+ )++ assert retrieve_path_model_name == cost_job_model_name == "model-a,model-b"++ minted_ids = {+ managed_files.get_unified_output_file_id(+ output_file_id="file-output-abc",+ model_id="model-deploy-xyz",+ model_name=model_name,+ )+ for model_name in (retrieve_path_model_name, cost_job_model_name)+ }+ assert len(minted_ids) == 1