litellm/tests/unit/files
mika-lt c74b785396
fix(router,files,batches)!: never resend a file or batch create the provider may already have (#51315)
* fix(files,batches): never resend a timed-out file or batch create

A file upload or batch create that hit the caller's timeout was sent again by the OpenAI SDK's retries and then by the router's retries and same-group fallbacks, so Azure and OpenAI kept up to 3 billed copies the caller never heard about. Creates now go out with SDK retries off, and the router re-raises a timed-out create instead of retrying or falling back

* fix(router): stop resending a create that times out on a retry or a fallback

A file or batch create that timed out after it was sent is no longer retried
or sent to the next order fallback, wherever the timeout happens. A connect
or pool timeout means the request never left LiteLLM, so it is still retried
and sent to a fallback like a connection error

* fix(files,batches): resend a create only when a resend cannot make a duplicate

File and batch creates sent without the router lost every SDK retry on this branch. They now go out with SDK retries off, and LiteLLM resends them itself only when a resend cannot create a second object. That is when the provider answered with a status the SDK would retry (x-should-retry wins, else 408, 409, 429 or 5xx), or when the request never left (a connect error, connect timeout or pool timeout). The budget is the client's own max_retries, and the wait comes from calculate_retry_after. The router uses the same "timed out after send" check, so the two layers cannot disagree

The httpx handler now chains the httpx error as the cause of litellm.Timeout, so a connect timeout on an Anthropic, Bedrock, Mistral or xAI upload is retried again like on main. A timed-out create through a model group keeps the "No fallback was attempted" note, and the router logs one info line when it stops

The proxy create-file and create-batch routes add x-should-retry false to a create that timed out after it was sent, so OpenAI and Anthropic SDK callers do not resend it. Status and body stay the same

Uploading a file to every deployment now waits for all of them and raises the after-send timeout when there is one, so a fast 429 from one deployment can no longer hide it and trigger a resend to the slow one

Tests cover the resend rules for sync and async on OpenAI and Azure, the Anthropic upload retries, the fallback note and log, the proxy header, the multi-deployment upload, and the aiohttp timeout classes

* fix(router,files,batches)!: do not resend a create the provider may already have made

A file or batch create is no longer resent by the SDK, the router's retries, or a router fallback when the provider may already have it. That covers a timeout after the request was sent, a connection that dropped after it was sent, a 504, and any answer that says x-should-retry: false. Creates on the httpx handler (Mistral and Bedrock batches, Gemini, Vertex AI and Manus uploads) now keep the transport error as the cause of the provider error, so the router sees the timeout too

A create that cannot have made anything is still resent, and a proxy write timeout or a refused egress proxy now count as that. A real 408 answer, 500, 502 and 503 keep the same handling as before

The proxy create-file and create-batch routes answer such a failure with x-should-retry: false and add a sentence to the error message that tells the caller to list their files or batches before they send it again

BREAKING CHANGE: a 504 on a file or batch create is no longer resent by the SDK or retried by the router unless the answer says x-should-retry: true

* fix(router,files,batches): keep retries and fallbacks after a create the provider rejected

Anthropic sends x-should-retry: false on every 4xx, so a model group upload with a bad key on its
first deployment stopped there with a 401 and a note saying the file may already exist. A 4xx other
than 408 now counts as nothing created, whatever the header says, so the router retries and falls
back the way it did before and the proxy adds no note or header to it

* fix(files,batches): number each resent create and keep stream timeouts as litellm.Timeout

Each resend of a file or batch create now carries its attempt number in
x-stainless-retry-count and keeps the caller's extra headers, the way the
SDK numbered its own resends. The httpx handler's timeout keeps base's
shape, so a Gemini stream that times out at open still raises
litellm.Timeout, and the create check follows the timeout's context to the
httpx error instead. A proxy integration test counts upstream POSTs for a
timed-out /v1/files and /v1/batches create on the default transport.

* fix(files,batches): stop resending a group upload that one deployment already created

A group upload where some deployments created the file and another failed now counts as a create that may exist, so the router does not resend it and the caller gets the notice. A failed GET never counts as may exist, and a write error or a local protocol error is resent, since the body never fully arrived. The notice now says to check the newest files or batches

* fix(router): keep the provider error when a group upload partly fails

A group upload where some deployments created the file and another failed now raises the failing deployment's own error, with a marker in its cause chain that says the file may exist. The caller gets the provider's message and the router's note again, instead of a generic error that showed the deployment's URL

* fix(router): keep the no-fallback note on every provider error from a create that may exist

A group upload that one Anthropic, Vertex AI, Gemini or Manus deployment rejected lost the router's note, since the note was added only to OpenAI errors

* fix(router): leave out the no-fallback note when a create stops after an order fallback

A create that fails at a later order level after an earlier level fell back now ends without
the "No fallback was attempted" note, since a fallback did run. The proxy's notice that the
provider may already hold the file or batch still comes through

* test: allowlist the bounded _cause_chain recursion in the recursion detector

* fix(router): keep retrying a create whose retry never connected, and keep the caller's create headers

The router's retry loop overwrote a retry error's __context__ with the hop's first error, which dropped the httpx ConnectTimeout behind an http-handler litellm.Timeout. The second attempt then read as a create that may already exist, so an Anthropic upload that never connected stopped retrying and skipped a healthy order fallback. The first error now goes at the end of the context chain

A create's extra_headers sent as a list are passed through unchanged again instead of being dropped, and a caller's own retry-count header in any case is no longer sent next to ours

Also applies cargo fmt to call.rs for the rust-lint job

* fix(files): treat a failed read as no create, whatever error wraps it

A GET that failed with an openai.APIStatusError no longer counts as a create that may already exist, so reads stay retryable. Tests now record the resend wait instead of timing it, and the duplicate Anthropic file fixture is gone

* fix(router,files): keep a create that may exist after a redirect or a fallback from being sent again

A gateway that takes an upload, answers 303 and fails the followed GET with a 504 now counts as a create that may exist, since the check reads the request the redirect started from

A fallbacks hop whose create may exist now ends the walk and is the error the caller gets, and a weighted failover whose create may exist skips the regular fallbacks

* test(router): mark the create-may-exist fallback tests with rstest

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 07:20:51 +00:00
..
__init__.py test: move tests/test_litellm root and small trees into tests/unit (#43186) 2026-09-25 11:30:43 -07:00
test_main.py fix(router,files,batches)!: never resend a file or batch create the provider may already have (#51315) 2026-11-25 07:20:51 +00:00