fix(managed_files): file each repeat-upload delete under the upload that made its copy

POST /v1/files sets no request call id, so the cleanup delete passed none
and its usage ledger row had no parent. Each copy now keeps the call id of
the router upload that made it, and its delete runs under that id.
This commit is contained in:
mateo-berri 2026-11-02 15:44:17 -08:00
parent 695c01642a
commit 9a4d86e4ad
2 changed files with 31 additions and 16 deletions

View file

@ -407,6 +407,7 @@ _FILE_DELETE_RESERVED_FIELDS: Final = frozenset({"model", "file_id", "_litellm_i
class _ProviderUpload:
model_id: str
provider_file_id: str
upload_call_id: str | None = None
def _provider_uploads(file_object: OpenAIFileObject) -> tuple[_ProviderUpload, ...]:
@ -416,8 +417,13 @@ def _provider_uploads(file_object: OpenAIFileObject) -> tuple[_ProviderUpload, .
model_file_id_mapping: Final = _MODEL_FILE_ID_MAPPING.validate_python(
file_hidden_params.get("model_file_id_mapping") or {}
)
upload_call_id: Final = file_hidden_params.get("litellm_call_id")
return tuple(
_ProviderUpload(model_id=model_id, provider_file_id=provider_file_id)
_ProviderUpload(
model_id=model_id,
provider_file_id=provider_file_id,
upload_call_id=upload_call_id if isinstance(upload_call_id, str) else None,
)
for model_id, provider_file_id in model_file_id_mapping.items()
)
@ -477,11 +483,13 @@ async def _delete_redundant_upload(
llm_router: Router,
upload: _ProviderUpload,
delete_request: Mapping[str, object],
parent_call_id: str | None,
) -> None:
try:
await _delete_provider_file(
llm_router, upload.model_id, upload.provider_file_id, _with_own_nested_dicts(delete_request)
)
with internal_sub_call(parent_call_id):
await _delete_provider_file(
llm_router, upload.model_id, upload.provider_file_id, _with_own_nested_dicts(delete_request)
)
except Exception as error:
verbose_logger.warning(
f"Could not delete the redundant upload provider_file_id={sanitize_for_log(upload.provider_file_id)} "
@ -495,9 +503,9 @@ async def _delete_redundant_uploads(
uploads: Sequence[_ProviderUpload],
delete_request: Mapping[str, object],
) -> None:
parent_call_id: Final = delete_request.get("litellm_call_id")
with internal_sub_call(parent_call_id if isinstance(parent_call_id, str) else None):
await asyncio.gather(*(_delete_redundant_upload(llm_router, upload, delete_request) for upload in uploads))
await asyncio.gather(
*(_delete_redundant_upload(llm_router, upload, delete_request, upload.upload_call_id) for upload in uploads)
)
def _start_deleting_redundant_uploads(
@ -2827,8 +2835,11 @@ class _PROXY_LiteLLMManagedFiles(CustomLogger, BaseFileEndpoints):
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 = _carried_upload_missing_from(file_id, specific_model_file_id_mapping)
delete_call_id: Final = data.get("litellm_call_id")
if unrecorded_upload is not None:
await _delete_redundant_uploads(llm_router, (unrecorded_upload,), data)
await _delete_redundant_upload(
llm_router, unrecorded_upload, data, delete_call_id if isinstance(delete_call_id, str) else None
)
async def afile_content(
self,

View file

@ -2301,7 +2301,6 @@ class _FakeFilesRouter:
file: tuple[str, bytes, str],
purpose: str,
litellm_metadata: Mapping[str, object],
litellm_call_id: str,
) -> OpenAIFileObject:
deployment_ids: Final = self.deployments_by_model[model]
provider_file_ids: Final = tuple(
@ -2313,6 +2312,7 @@ class _FakeFilesRouter:
"model_id": deployment_ids[0],
"custom_llm_provider": self.provider,
"model_file_id_mapping": dict(zip(deployment_ids, provider_file_ids)),
"litellm_call_id": f"call-of-{provider_file_ids[0]}",
}
return response
@ -2329,7 +2329,7 @@ class _FakeFilesRouter:
file_id: str,
litellm_metadata: Mapping[str, object],
_litellm_internal_model_credentials: Mapping[str, str],
litellm_call_id: str,
litellm_call_id: str | None = None,
) -> None:
if self.delete_gate is not None:
await self.delete_gate.wait()
@ -2367,7 +2367,6 @@ async def _upload(
"file": ("notes.txt", b"the launch code is ORCHID-42\n", content_type),
"purpose": "user_data",
"litellm_metadata": litellm_metadata,
"litellm_call_id": "upload-call",
},
llm_router=router,
target_model_names_list=target_model_names,
@ -2478,14 +2477,19 @@ async def test_shutdown_drain_waits_for_the_repeat_upload_delete():
@pytest.mark.asyncio
async def test_repeat_upload_delete_runs_as_an_internal_call_of_the_upload_request():
router: Final = _FakeFilesRouter({"gpt-a": ("wildcard-dep",), "gpt-b": ("wildcard-dep",)})
async def test_each_repeat_upload_delete_runs_as_an_internal_call_of_the_upload_that_made_the_copy():
router: Final = _FakeFilesRouter(
{"gpt-a": ("wildcard-dep",), "gpt-b": ("wildcard-dep",), "gpt-c": ("wildcard-dep",)}
)
await _create_managed_file(router, ["gpt-a", "gpt-b"])
await _create_managed_file(router, ["gpt-a", "gpt-b", "gpt-c"])
await drain_logging_tasks(timeout=1)
assert router.deletes_marked_internal == (True,)
assert router.delete_parent_request_ids == ("upload-call",)
assert router.deletes_marked_internal == (True, True)
assert sorted(zip((delete[1] for delete in router.deletes), router.delete_parent_request_ids)) == [
("file-upload-2", "call-of-file-upload-2"),
("file-upload-3", "call-of-file-upload-3"),
]
assert is_internal_call.get() is False
assert internal_parent_request_id.get() is None