From cbe5b9e77db28dbad82bdd14a33eebd5474d5449 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 21 Oct 2026 01:47:32 -0700 Subject: [PATCH] fix(files): keep admin provider lists and index the owner-scoped walk Under require_managed_files, an admin keeps the provider list as on main, and only a non-admin list reads the caller's own and team managed files. A stored file now also shows on the provider its model name routes to through a wildcard, with exact model names first, and a list routed to a model takes the default page size of the provider serving it. A new index on created_by and created_at keeps each chunk of the walk an index read, and the model check runs before the file object is parsed. --- .../proxy/hooks/managed_files.py | 24 ++- .../migration.sql | 2 + .../litellm_proxy_extras/schema.prisma | 1 + litellm/llms/base_llm/files/transformation.py | 23 ++- .../openai_files_endpoints/files_endpoints.py | 87 ++++++--- litellm/proxy/schema.prisma | 1 + schema.prisma | 1 + .../security/test_managed_file_listing.py | 8 +- .../proxy/test_managed_files_hook.py | 3 +- .../test_files_endpoint.py | 177 ++++++++++++++---- 10 files changed, 236 insertions(+), 91 deletions(-) create mode 100644 litellm-proxy-extras/litellm_proxy_extras/migrations/20261021000000_managed_file_created_by_created_at_index/migration.sql diff --git a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py index f1c087c47df..0716666284b 100644 --- a/enterprise/litellm_enterprise/proxy/hooks/managed_files.py +++ b/enterprise/litellm_enterprise/proxy/hooks/managed_files.py @@ -50,7 +50,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import ( extract_file_metadata, ) from litellm.llms.base_llm.files.storage_backend_factory import get_storage_backend -from litellm.llms.base_llm.files.transformation import BaseFileEndpoints +from litellm.llms.base_llm.files.transformation import BaseFileEndpoints, FileListModelScope from litellm.llms.base_llm.managed_resources.isolation import ( build_list_page, build_owner_filter, @@ -2252,7 +2252,7 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints): user_api_key_dict: UserAPIKeyAuth, limit: Optional[int] = None, after: Optional[str] = None, - model_mapping_keys: Optional[frozenset[str]] = None, + model_scope: Optional[FileListModelScope] = None, before: Optional[str] = None, **data: Dict, ) -> FileListPage: @@ -2264,9 +2264,10 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints): each page still ordered newest first. ``purpose`` is applied after parsing, because the managed file table keeps it inside the ``file_object`` blob instead of a column, and - ``model_mapping_keys`` keeps only files mapped to one of those - deployment ids, or model names for storage-backed files, read from - ``model_mappings``, a JSON column. Rows + ``model_scope`` keeps only files whose ``model_mappings``, a JSON + column, name one of its deployment ids or model names, or for a + storage-backed file a model name the router sends to the listed + provider. Rows whose blob will not parse drop out there too, so a chunk of rows can yield fewer matches than the page holds. Successive chunks are read until the page is full or the caller's rows run out, which keeps @@ -2276,8 +2277,8 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints): ``FILE_LIST_CONTINUATION_CHUNK_SIZE``, so the walk costs one query per that many rows instead of one per page. That bound is per query, not per request: the work is still linear in the rows the caller owns, and - a filter matching nothing reads every one of them, with no index - covering either the owner filter or the sort. + a filter matching nothing reads every one of them, each chunk through + the owner and creation-time indexes instead of a table scan. """ validate_file_list_limit(limit) validate_file_list_purpose(purpose) @@ -2309,7 +2310,7 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints): openai_code="invalid_value", ) - if model_mapping_keys is not None and not model_mapping_keys: + if model_scope is not None and not model_scope.model_mapping_keys: return FileListPage.model_validate(build_list_page([])) page_size: Final = min(limit or MAX_FILE_LIST_LIMIT, MAX_FILE_LIST_LIMIT) @@ -2329,9 +2330,12 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints): matches.extend( _public_file_object(parsed_file_object, row.unified_file_id) for row in chunk - if (parsed_file_object := _parse_managed_file_object(row.file_object, row.unified_file_id)) is not None + if ( + model_scope is None + or model_scope.holds(row.model_mappings, stored=row.storage_backend is not None) + ) + and (parsed_file_object := _parse_managed_file_object(row.file_object, row.unified_file_id)) is not None and (purpose is None or parsed_file_object.purpose == purpose) - and (model_mapping_keys is None or not model_mapping_keys.isdisjoint(row.model_mappings)) ) if len(chunk) < chunk_size: break diff --git a/litellm-proxy-extras/litellm_proxy_extras/migrations/20261021000000_managed_file_created_by_created_at_index/migration.sql b/litellm-proxy-extras/litellm_proxy_extras/migrations/20261021000000_managed_file_created_by_created_at_index/migration.sql new file mode 100644 index 00000000000..8bbcb67bddf --- /dev/null +++ b/litellm-proxy-extras/litellm_proxy_extras/migrations/20261021000000_managed_file_created_by_created_at_index/migration.sql @@ -0,0 +1,2 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS "LiteLLM_ManagedFileTable_created_by_created_at_idx" +ON "LiteLLM_ManagedFileTable" ("created_by", "created_at" DESC); diff --git a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma index 83a0e6f9e05..4187f76c97c 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma +++ b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma @@ -1163,6 +1163,7 @@ model LiteLLM_ManagedFileTable { @@index([unified_file_id]) @@index([flat_model_file_ids], type: Gin) @@index([team_id, created_at(sort: Desc)]) + @@index([created_by, created_at(sort: Desc)]) } model LiteLLM_ManagedObjectTable { // for batches or finetuning jobs which use the diff --git a/litellm/llms/base_llm/files/transformation.py b/litellm/llms/base_llm/files/transformation.py index 6660873e8b1..491fb1919b6 100644 --- a/litellm/llms/base_llm/files/transformation.py +++ b/litellm/llms/base_llm/files/transformation.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod -from collections.abc import AsyncGenerator, Iterator, Mapping -from typing import TYPE_CHECKING, Any, Union +from collections.abc import AsyncGenerator, Callable, Iterable, Iterator, Mapping +from dataclasses import dataclass +from typing import TYPE_CHECKING, Any, Final, Union import httpx from openai.types.file_deleted import FileDeleted @@ -38,6 +39,22 @@ else: UserAPIKeyAuth = Any +def _routes_nowhere(_model_name: str) -> bool: + return False + + +@dataclass(frozen=True, slots=True) +class FileListModelScope: + model_mapping_keys: frozenset[str] + routes_stored_model_name: Callable[[str], bool] = _routes_nowhere + + def holds(self, model_mapping_keys: Iterable[str], stored: bool) -> bool: + mapped_keys: Final = frozenset(model_mapping_keys) + return not self.model_mapping_keys.isdisjoint(mapped_keys) or ( + stored and any(map(self.routes_stored_model_name, mapped_keys)) + ) + + class BaseFileUploadStream(ABC): """Re-iterable request body that yields an upload's bytes lazily. @@ -274,7 +291,7 @@ class BaseFileEndpoints(ABC): user_api_key_dict: UserAPIKeyAuth, limit: int | None = None, after: str | None = None, - model_mapping_keys: frozenset[str] | None = None, + model_scope: FileListModelScope | None = None, before: str | None = None, **data: dict, ) -> FileListPage: diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.py index 616519a9869..878ea08bf23 100644 --- a/litellm/proxy/openai_files_endpoints/files_endpoints.py +++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py @@ -6,6 +6,7 @@ ###################################################################### import asyncio +import functools import itertools import traceback from collections.abc import Iterable, Iterator, Mapping, Sequence @@ -36,7 +37,7 @@ from litellm.litellm_core_utils.cloud_storage_security import ( ) from litellm.litellm_core_utils.core_helpers import get_or_create_metadata_bucket from litellm.llms.base_llm.files.litellm_db_storage_backend import LITELLM_DB_STORAGE_BACKEND_NAME -from litellm.llms.base_llm.files.transformation import BaseFileEndpoints +from litellm.llms.base_llm.files.transformation import BaseFileEndpoints, FileListModelScope from litellm.llms.base_llm.managed_resources.isolation import build_list_page from litellm.proxy._types import * from litellm.proxy.auth.user_api_key_auth import user_api_key_auth @@ -1685,7 +1686,7 @@ async def _list_owned_managed_files( limit: int | None, after: str | None, before: str | None, - model_mapping_keys: frozenset[str] | None, + model_scope: FileListModelScope | None, ) -> FileListPage: return await managed_files_obj.afile_list( purpose=purpose, @@ -1694,7 +1695,7 @@ async def _list_owned_managed_files( limit=limit, after=after, before=before, - model_mapping_keys=model_mapping_keys, + model_scope=model_scope, ) @@ -1704,6 +1705,12 @@ class _UnscopedFileList: detail: str +@dataclass(frozen=True, slots=True) +class _ListedFiles: + model_scope: FileListModelScope | None + custom_llm_provider: str | None + + def _raise_unscoped_file_list(failure: _UnscopedFileList) -> NoReturn: raise HTTPException(status_code=failure.status_code, detail=failure.detail) @@ -1739,13 +1746,11 @@ def _model_mapping_keys(deployments: Iterable[Deployment]) -> frozenset[str]: return frozenset(itertools.chain.from_iterable(map(_deployment_model_mapping_keys, deployments))) -def _model_mapping_keys_serving_model(llm_router: Router, model: str) -> frozenset[str]: +def _deployments_serving_model(llm_router: Router, model: str) -> tuple[Deployment, ...]: deployment: Final = llm_router.get_deployment(model_id=model) if llm_router.has_model_id(model) else None if deployment is not None: - return _deployment_model_mapping_keys(deployment) - return _model_mapping_keys( - Deployment.model_validate(listed) for listed in llm_router.get_model_list(model_name=model) or () - ) + return (deployment,) + return tuple(Deployment.model_validate(listed) for listed in llm_router.get_model_list(model_name=model) or ()) def _resolved_deployment_provider(deployment: Deployment) -> str | None: @@ -1769,28 +1774,51 @@ def _deployments_on_provider(llm_router: Router | None, custom_llm_provider: str ) -def _model_mapping_keys_on_provider( - llm_router: Router | None, custom_llm_provider: str -) -> frozenset[str] | _UnscopedFileList: +def _pattern_routes_model_name_to_provider(llm_router: Router, custom_llm_provider: str, model_name: str) -> bool: + return model_name not in llm_router.model_names and llm_router.pattern_router.routes_to_provider( + model_name, custom_llm_provider + ) + + +def _files_on_provider(llm_router: Router | None, custom_llm_provider: str) -> _ListedFiles | _UnscopedFileList: from litellm.proxy.pass_through_endpoints.managed_id_rewriter import passthrough_file_model_id if custom_llm_provider not in litellm.provider_list: return _UnscopedFileList(status_code=400, detail=f"Unknown provider '{custom_llm_provider}' for listing files.") passthrough_model_id: Final = passthrough_file_model_id(custom_llm_provider) - return _model_mapping_keys(_deployments_on_provider(llm_router, custom_llm_provider)) | frozenset( - (passthrough_model_id,) if passthrough_model_id is not None else () + model_mapping_keys: Final = _model_mapping_keys( + _deployments_on_provider(llm_router, custom_llm_provider) + ) | frozenset((passthrough_model_id,) if passthrough_model_id is not None else ()) + if llm_router is None: + return _ListedFiles(FileListModelScope(model_mapping_keys), custom_llm_provider) + return _ListedFiles( + FileListModelScope( + model_mapping_keys, + routes_stored_model_name=functools.partial( + _pattern_routes_model_name_to_provider, llm_router, custom_llm_provider + ), + ), + custom_llm_provider, ) -async def _listed_model_mapping_keys( +def _files_serving_model(llm_router: Router, model: str) -> _ListedFiles: + deployments: Final = _deployments_serving_model(llm_router, model) + providers: Final = frozenset(map(_resolved_deployment_provider, deployments)) + return _ListedFiles( + FileListModelScope(_model_mapping_keys(deployments)), next(iter(providers)) if len(providers) == 1 else None + ) + + +async def _listed_files( llm_router: Router | None, user_api_key_dict: UserAPIKeyAuth, custom_llm_provider: str | None, target_model_names: str | None, routed_model: str | None, -) -> frozenset[str] | None | _UnscopedFileList: +) -> _ListedFiles | _UnscopedFileList: if routed_model is not None and llm_router is not None: - return _model_mapping_keys_serving_model(llm_router, routed_model) + return _files_serving_model(llm_router, routed_model) if target_model_names: target_model_name: Final = _single_target_model_name(target_model_names) if isinstance(target_model_name, _UnscopedFileList): @@ -1805,10 +1833,10 @@ async def _listed_model_mapping_keys( user_api_key_dict=user_api_key_dict, operation_context="file list", ) - return _model_mapping_keys_serving_model(llm_router, target_model_name) + return _files_serving_model(llm_router, target_model_name) if custom_llm_provider is None: - return None - return _model_mapping_keys_on_provider(llm_router, custom_llm_provider) + return _ListedFiles(None, None) + return _files_on_provider(llm_router, custom_llm_provider) @router.get( @@ -1880,8 +1908,8 @@ async def list_files( response: Any | None = None owned_files_hook: Final = proxy_logging_obj.get_proxy_hook("managed_files") - list_owned_files_only: Final = litellm.require_managed_files is True and ( - isinstance(owned_files_hook, BaseFileEndpoints) or not user_api_key_has_admin_view(user_api_key_dict) + list_owned_files_only: Final = litellm.require_managed_files is True and not user_api_key_has_admin_view( + user_api_key_dict ) # Check for model-based credential routing (no file_id encoding check for list) @@ -1905,26 +1933,23 @@ async def list_files( status_code=500, detail="Managed file listing is unavailable, so require_managed_files cannot scope the list to the caller.", ) - requested_custom_llm_provider: Final = await _requested_custom_llm_provider( - request=request, provider=provider - ) - listed_model_mapping_keys: Final = await _listed_model_mapping_keys( + listed_files: Final = await _listed_files( llm_router=llm_router, user_api_key_dict=user_api_key_dict, - custom_llm_provider=requested_custom_llm_provider, + custom_llm_provider=await _requested_custom_llm_provider(request=request, provider=provider), target_model_names=target_model_names, routed_model=model_used if should_route else None, ) - if isinstance(listed_model_mapping_keys, _UnscopedFileList): - _raise_unscoped_file_list(listed_model_mapping_keys) + if isinstance(listed_files, _UnscopedFileList): + _raise_unscoped_file_list(listed_files) response = await _list_owned_managed_files( # rebind-ok: each dispatch branch binds response managed_files_obj=owned_files_hook, user_api_key_dict=user_api_key_dict, purpose=purpose, - limit=limit if limit is not None else _default_file_list_limit(requested_custom_llm_provider), + limit=limit if limit is not None else _default_file_list_limit(listed_files.custom_llm_provider), after=after or after_id, before=before_id, - model_mapping_keys=listed_model_mapping_keys, + model_scope=listed_files.model_scope, ) elif should_route and credentials is not None: # Use model-based routing with credentials from config @@ -1969,7 +1994,7 @@ async def list_files( limit=limit, after=after, before=None, - model_mapping_keys=None, + model_scope=None, ) else: resolved_custom_llm_provider: Final = custom_llm_provider or "openai" diff --git a/litellm/proxy/schema.prisma b/litellm/proxy/schema.prisma index 83a0e6f9e05..4187f76c97c 100644 --- a/litellm/proxy/schema.prisma +++ b/litellm/proxy/schema.prisma @@ -1163,6 +1163,7 @@ model LiteLLM_ManagedFileTable { @@index([unified_file_id]) @@index([flat_model_file_ids], type: Gin) @@index([team_id, created_at(sort: Desc)]) + @@index([created_by, created_at(sort: Desc)]) } model LiteLLM_ManagedObjectTable { // for batches or finetuning jobs which use the diff --git a/schema.prisma b/schema.prisma index 83a0e6f9e05..4187f76c97c 100644 --- a/schema.prisma +++ b/schema.prisma @@ -1163,6 +1163,7 @@ model LiteLLM_ManagedFileTable { @@index([unified_file_id]) @@index([flat_model_file_ids], type: Gin) @@index([team_id, created_at(sort: Desc)]) + @@index([created_by, created_at(sort: Desc)]) } model LiteLLM_ManagedObjectTable { // for batches or finetuning jobs which use the diff --git a/tests/integration/security/test_managed_file_listing.py b/tests/integration/security/test_managed_file_listing.py index 88c5126dae0..8c736b2b2bf 100644 --- a/tests/integration/security/test_managed_file_listing.py +++ b/tests/integration/security/test_managed_file_listing.py @@ -258,8 +258,6 @@ def test_anthropic_sdk_pages_backwards_to_the_newest_file_and_stops(rig: Rig) -> assert listed == list(reversed(newest_first[:-1])) -@pytest.mark.parametrize("deployment", DEPLOYMENTS) -def test_master_key_lists_managed_files_instead_of_the_provider_key(rig: Rig, deployment: Deployment) -> None: - listed: Final = rig.list(ListCall(f"/{deployment.provider}/v1/files")).ids - assert UNMANAGED_FILE not in listed - assert set(rig.files_by_provider[deployment.provider]) <= set(listed) +def test_master_key_still_lists_the_files_on_the_shared_provider_key(rig: Rig) -> None: + listed: Final = rig.list(ListCall(f"/{ANTHROPIC.provider}/v1/files")).ids + assert UNMANAGED_FILE in listed diff --git a/tests/unit/enterprise/proxy/test_managed_files_hook.py b/tests/unit/enterprise/proxy/test_managed_files_hook.py index 4a35c95efc5..f9f4e17c760 100644 --- a/tests/unit/enterprise/proxy/test_managed_files_hook.py +++ b/tests/unit/enterprise/proxy/test_managed_files_hook.py @@ -15,6 +15,7 @@ import pytest from typing import Final, Optional from unittest.mock import AsyncMock, MagicMock, patch +from litellm.llms.base_llm.files.transformation import FileListModelScope from litellm.proxy._types import LitellmUserRoles, ProxyException, UserAPIKeyAuth from litellm.proxy.openai_files_endpoints.common_utils import BATCH_CREATE_HIDDEN_PARAM from litellm.types.llms.openai import FileListPage, OpenAIFileObject @@ -657,7 +658,7 @@ async def test_afile_list_reads_no_rows_when_no_deployment_can_hold_a_file() -> purpose=None, litellm_parent_otel_span=None, user_api_key_dict=_make_user_api_key_dict(), - model_mapping_keys=frozenset(), + model_scope=FileListModelScope(frozenset()), ) assert response.data == [] diff --git a/tests/unit/proxy/openai_files_endpoint/test_files_endpoint.py b/tests/unit/proxy/openai_files_endpoint/test_files_endpoint.py index f3079f8c901..27e27ce99eb 100644 --- a/tests/unit/proxy/openai_files_endpoint/test_files_endpoint.py +++ b/tests/unit/proxy/openai_files_endpoint/test_files_endpoint.py @@ -1,3 +1,4 @@ +import itertools import json from typing import Final, List, Optional from urllib.parse import quote @@ -4928,6 +4929,7 @@ def _managed_file_row( created_by: str = "test-user", team_id: Optional[str] = None, model_mappings: Optional[dict[str, str]] = None, + storage_backend: Optional[str] = None, ) -> MagicMock: file_object = OpenAIFileObject( id=stored_file_id or unified_file_id, @@ -4944,6 +4946,7 @@ def _managed_file_row( created_by=created_by, team_id=team_id, model_mappings=model_mappings or {}, + storage_backend=storage_backend, ) @@ -4970,16 +4973,16 @@ class _ManagedFileTableOverRows: return next(iter(self._owned_rows(where)), None) async def find_many(self, where, take=None, order=None, cursor=None, skip=0): - owned_rows = self._owned_rows(where) - oldest_first = bool(order) and order[0].get("created_at") == "asc" - rows = owned_rows[::-1] if oldest_first else owned_rows - if cursor is not None: - start = next( - index - for index, row in enumerate(rows) - if row.unified_file_id == cursor["unified_file_id"] - ) - rows = rows[start + skip :] + owned_rows: Final = self._owned_rows(where) + oldest_first: Final = bool(order) and order[0].get("created_at") == "asc" + ordered_rows: Final = owned_rows[::-1] if oldest_first else owned_rows + start: Final = ( + 0 + if cursor is None + else skip + + next(index for index, row in enumerate(ordered_rows) if row.unified_file_id == cursor["unified_file_id"]) + ) + rows: Final = ordered_rows[start:] return rows if take is None else rows[:take] @@ -6316,16 +6319,40 @@ def _callers_files_on_wildcard_unprefixed_and_passthrough_deployments() -> list[ "_passthrough_provider_marker": "_passthrough_provider:openai", }, ), - _managed_file_row("unified-storage", model_mappings={"claude-unprefixed": "litellm_db://unified-storage"}), + _managed_file_row( + "unified-storage", + model_mappings={"claude-unprefixed": "litellm_db://unified-storage"}, + storage_backend="litellm_db", + ), + _managed_file_row( + "unified-wildcard-storage", + model_mappings={"anthropic/claude-haiku-5-5": "litellm_db://unified-wildcard-storage"}, + storage_backend="litellm_db", + ), ] @pytest.mark.parametrize( ("path", "params", "headers", "expected_ids"), [ - ("/v1/files", {"target_model_names": "anthropic/claude-haiku-5-5"}, {}, ["unified-wildcard"]), - ("/v1/files", {}, {"x-litellm-model": "anthropic/claude-haiku-5-5"}, ["unified-wildcard"]), - ("/anthropic/v1/files", {}, {}, ["unified-wildcard", "unified-unprefixed", "unified-storage"]), + ( + "/v1/files", + {"target_model_names": "anthropic/claude-haiku-5-5"}, + {}, + ["unified-wildcard", "unified-wildcard-storage"], + ), + ( + "/v1/files", + {}, + {"x-litellm-model": "anthropic/claude-haiku-5-5"}, + ["unified-wildcard", "unified-wildcard-storage"], + ), + ( + "/anthropic/v1/files", + {}, + {}, + ["unified-wildcard", "unified-unprefixed", "unified-storage", "unified-wildcard-storage"], + ), ("/openai/v1/files", {}, {}, ["unified-gpt", "unified-passthrough"]), ("/v1/files", {"target_model_names": "claude-unprefixed"}, {}, ["unified-unprefixed", "unified-storage"]), ], @@ -6360,6 +6387,71 @@ def test_require_managed_files_lists_the_callers_files_wherever_the_router_put_t provider_list.assert_not_awaited() +def _router_with_a_catch_all_openai_deployment() -> Router: + unprefixed_anthropic_model: Final = next(model for model in sorted(litellm.anthropic_models) if "/" not in model) + return Router( + model_list=[ + { + "model_name": "gpt-3.5-turbo", + "litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "openai_api_key"}, + "model_info": {"id": "gpt-3.5-turbo-id"}, + }, + { + "model_name": "*", + "litellm_params": {"model": "openai/*", "api_key": "openai_api_key"}, + "model_info": {"id": "catch-all-id"}, + }, + { + "model_name": "claude-unprefixed", + "litellm_params": {"model": unprefixed_anthropic_model, "api_key": "anthropic_api_key"}, + "model_info": {"id": "claude-unprefixed-id"}, + }, + ] + ) + + +@pytest.mark.parametrize( + ("path", "expected_ids"), + [ + ("/openai/v1/files", ["unified-gpt", "unified-catch-all-storage"]), + ("/anthropic/v1/files", ["unified-unprefixed", "unified-storage"]), + ], + ids=["catch-all-provider", "explicit-provider"], +) +def test_require_managed_files_keeps_a_catch_all_route_from_claiming_files_on_other_providers( + mocker: MockerFixture, + monkeypatch: pytest.MonkeyPatch, + path: str, + expected_ids: list[str], +) -> None: + _, provider_list = _setup_unscoped_list_files_route_over_real_hook( + mocker, + monkeypatch, + _router_with_a_catch_all_openai_deployment(), + [ + _managed_file_row("unified-unprefixed", model_mappings={"claude-unprefixed-id": "file-unprefixed"}), + _managed_file_row("unified-gpt", model_mappings={"gpt-3.5-turbo-id": "file-gpt"}), + _managed_file_row( + "unified-storage", + model_mappings={"claude-unprefixed": "litellm_db://unified-storage"}, + storage_backend="litellm_db", + ), + _managed_file_row( + "unified-catch-all-storage", + model_mappings={"model-only-the-catch-all-serves": "litellm_db://unified-catch-all-storage"}, + storage_backend="litellm_db", + ), + ], + ) + monkeypatch.setattr("litellm.require_managed_files", True) + + response: Final = _list_files_as_test_user(path, {}, {}) + + assert response.status_code == 200, response.text + assert [file["id"] for file in response.json()["data"]] == expected_ids + provider_list.assert_not_awaited() + + @pytest.mark.parametrize( ("path", "params"), [ @@ -6515,19 +6607,12 @@ def test_require_managed_files_list_fails_closed_without_the_managed_files_hook( provider_list.assert_not_awaited() -@pytest.mark.parametrize( - ("hook_loaded", "expected_ids"), - [ - (True, ["unified-gpt-newer", "unified-theirs", "unified-gpt-older"]), - (False, ["file-on-the-shared-key"]), - ], -) -def test_require_managed_files_lists_managed_files_for_an_admin_and_the_provider_list_without_the_hook( +@pytest.mark.parametrize("hook_loaded", [True, False], ids=["hook-loaded", "no-hook"]) +def test_require_managed_files_keeps_the_provider_list_for_an_admin( mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch, llm_router: Router, hook_loaded: bool, - expected_ids: list[str], ) -> None: import litellm.proxy.proxy_server as ps @@ -6542,10 +6627,10 @@ def test_require_managed_files_lists_managed_files_for_an_admin_and_the_provider ) monkeypatch.setattr("litellm.require_managed_files", True) - response: Final = _list_files_as_test_user("/openai/v1/files", {}, {}) + response: Final = _list_files_as_test_user("/anthropic/v1/files", {}, {}) assert response.status_code == 200, response.text - assert [file["id"] for file in response.json()["data"]] == expected_ids + assert [file["id"] for file in response.json()["data"]] == ["file-on-the-shared-key"] def test_managed_file_list_without_require_managed_files_ignores_cursor_params_openai_does_not_define( @@ -6564,30 +6649,40 @@ def test_managed_file_list_without_require_managed_files_ignores_cursor_params_o assert [file["id"] for file in response.json()["data"]] == ["unified-azure", "unified-gemini", "unified-gpt-older"] -def test_require_managed_files_pages_the_anthropic_list_at_the_anthropic_default_size( - mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch +@pytest.mark.parametrize( + ("path", "headers", "expected_count", "has_more"), + [ + ("/anthropic/v1/files", {}, ANTHROPIC_DEFAULT_FILE_LIST_LIMIT, True), + ("/v1/files", {"x-litellm-model": "claude-unprefixed"}, ANTHROPIC_DEFAULT_FILE_LIST_LIMIT, True), + ("/anthropic/v1/files", {"x-litellm-model": "gpt-3.5-turbo"}, ANTHROPIC_DEFAULT_FILE_LIST_LIMIT + 1, False), + ("/v1/files", {}, 2 * (ANTHROPIC_DEFAULT_FILE_LIST_LIMIT + 1), False), + ], + ids=["anthropic-provider", "anthropic-model-header", "openai-model-header", "every-provider"], +) +def test_require_managed_files_pages_an_anthropic_list_at_the_anthropic_default_size( + mocker: MockerFixture, + monkeypatch: pytest.MonkeyPatch, + path: str, + headers: dict[str, str], + expected_count: int, + has_more: bool, ) -> None: - import litellm.proxy.proxy_server as ps - _setup_unscoped_list_files_route_over_real_hook( mocker, monkeypatch, _router_with_wildcard_and_unprefixed_anthropic_deployments(), [ - _managed_file_row(f"unified-claude-{index:03d}", model_mappings={"claude-unprefixed-id": f"file-{index:03d}"}) - for index in range(ANTHROPIC_DEFAULT_FILE_LIST_LIMIT + 1) + _managed_file_row( + f"unified-{deployment_id}-{index:03d}", model_mappings={deployment_id: f"file-{index:03d}"} + ) + for deployment_id, index in itertools.product( + ("claude-unprefixed-id", "gpt-3.5-turbo-id"), range(ANTHROPIC_DEFAULT_FILE_LIST_LIMIT + 1) + ) ], ) monkeypatch.setattr("litellm.require_managed_files", True) - try: - anthropic_page: Final = client.get("/anthropic/v1/files", headers={"Authorization": "Bearer test-key"}) - unified_list: Final = client.get("/v1/files", headers={"Authorization": "Bearer test-key"}) - finally: - app.dependency_overrides.pop(ps.user_api_key_auth, None) + response: Final = _list_files_as_test_user(path, {}, headers) - assert (anthropic_page.status_code, unified_list.status_code) == (200, 200), (anthropic_page.text, unified_list.text) - assert len(anthropic_page.json()["data"]) == ANTHROPIC_DEFAULT_FILE_LIST_LIMIT - assert anthropic_page.json()["has_more"] is True - assert len(unified_list.json()["data"]) == ANTHROPIC_DEFAULT_FILE_LIST_LIMIT + 1 - assert unified_list.json()["has_more"] is False + assert response.status_code == 200, response.text + assert (len(response.json()["data"]), response.json()["has_more"]) == (expected_count, has_more)