fix(managed_files): read the copy DELETE removes from the id's gateway-written fields

This commit is contained in:
mateo-berri 2026-11-01 13:59:29 -08:00
parent 81038f82c1
commit d1b7af1e66
2 changed files with 35 additions and 16 deletions

View file

@ -433,6 +433,20 @@ def _is_kept_upload(uploads: Sequence[_ProviderUpload], index: int, per_target_m
return all(other.model_id != upload.model_id for other in other_uploads)
def _upload_from_trailing_id_fields(decoded_unified_file_id: str) -> _ProviderUpload:
trailing_fields: Final = decoded_unified_file_id.rpartition(";llm_output_file_id,")[2]
provider_file_id, _, model_id = trailing_fields.partition(";llm_output_file_model_id,")
return _ProviderUpload(model_id=model_id, provider_file_id=provider_file_id)
def _carried_upload_missing_from(unified_file_id: str, model_file_ids: Mapping[str, str]) -> _ProviderUpload | None:
decoded: Final = _is_base64_encoded_unified_file_id(unified_file_id)
carried: Final = _upload_from_trailing_id_fields(decoded) if decoded else None
if carried is None or model_file_ids.get(carried.model_id) in (None, carried.provider_file_id):
return None
return carried
async def _delete_provider_file(
llm_router: Router,
model_id: str,
@ -2811,22 +2825,10 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
return
for model_id, model_file_id in specific_model_file_id_mapping.items():
await _delete_provider_file(llm_router, model_id, model_file_id, data)
unrecorded_upload: Final = self._carried_upload_missing_from(file_id, specific_model_file_id_mapping)
unrecorded_upload: Final = _carried_upload_missing_from(file_id, specific_model_file_id_mapping)
if unrecorded_upload is not None:
await _delete_redundant_upload(llm_router, unrecorded_upload, data)
def _carried_upload_missing_from(
self, unified_file_id: str, model_file_ids: Mapping[str, str]
) -> _ProviderUpload | None:
decoded: Final = _is_base64_encoded_unified_file_id(unified_file_id)
if not decoded or "llm_output_file_model_id," not in decoded:
return None
carried: Final = _ProviderUpload(
model_id=self.get_model_id_from_unified_file_id(decoded),
provider_file_id=self.get_output_file_id_from_unified_file_id(decoded),
)
return carried if model_file_ids.get(carried.model_id) not in (None, carried.provider_file_id) else None
async def afile_content(
self,
file_id: str,

View file

@ -2352,10 +2352,11 @@ async def _upload(
router: _FakeFilesRouter,
target_model_names: list[str],
litellm_metadata: Mapping[str, object] = _REQUEST_METADATA,
content_type: str = "text/plain",
) -> OpenAIFileObject:
return await managed_files.acreate_file(
create_file_request={
"file": ("notes.txt", b"the launch code is ORCHID-42\n", "text/plain"),
"file": ("notes.txt", b"the launch code is ORCHID-42\n", content_type),
"purpose": "user_data",
"litellm_metadata": litellm_metadata,
},
@ -2492,9 +2493,11 @@ async def test_litellm_proxy_wildcard_keeps_its_last_per_target_upload_and_delet
assert router.deletes == ()
async def _upload_then_delete(router: _FakeFilesRouter, target_model_names: list[str]) -> tuple[str, ...]:
async def _upload_then_delete(
router: _FakeFilesRouter, target_model_names: list[str], content_type: str = "text/plain"
) -> tuple[str, ...]:
managed_files, mock_prisma = _make_real_managed_files_instance()
response: Final = await _upload(managed_files, router, target_model_names)
response: Final = await _upload(managed_files, router, target_model_names, content_type=content_type)
await drain_logging_tasks(timeout=1)
deletes_during_upload: Final = len(router.deletes)
managed_files.internal_usage_cache.async_get_cache = AsyncMock(
@ -2528,6 +2531,20 @@ async def test_deleting_a_wildcard_file_removes_its_kept_upload_once():
assert await _upload_then_delete(router, ["gpt-a", "gpt-b"]) == ("file-upload-1",)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"content_type",
[
"text/plain;llm_output_file_id,file-of-another-team;llm_output_file_model_id,wildcard-dep",
"text/plain;llm_output_file_id,file-of-another-team",
],
)
async def test_deleting_a_file_removes_only_the_uploads_the_gateway_made_for_it(content_type: str):
router: Final = _FakeFilesRouter({"gpt-a": ("wildcard-dep",), "gpt-b": ("wildcard-dep",)})
assert await _upload_then_delete(router, ["gpt-a", "gpt-b"], content_type) == ("file-upload-1",)
@pytest.mark.asyncio
async def test_deleting_a_file_still_succeeds_when_the_upload_its_id_carries_is_already_gone(
caplog: pytest.LogCaptureFixture,