litellm/tests/unit/litellm_core_utils
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
..
audio_utils feat(realtime)!: support latest OpenAI audio models (#35600) 2026-11-03 07:27:52 +00:00
llm_cost_calc fix(gemini): bill flex image output tokens at the flex rate (#54331) 2026-11-24 18:23:34 -08:00
llm_response_utils fix(rerank): preserve provider usage and reported costs (#53893) 2026-11-23 19:59:34 -08:00
prompt_templates fix(bedrock_mantle): drop the JSON Schema keywords gemma-4 rejects from tool and response schemas (#54214) 2026-11-25 07:04:08 +00:00
specialty_caches test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
__init__.py test(unit): make every tests/unit directory a package so pytest collection is unique 2026-09-20 11:50:59 +00:00
attribute_forwarder.py refactor(typing): clear 1181 basedpyright Any errors across 431 files (#47789) 2026-10-24 11:31:09 -07:00
conftest.py test(ci): repair stale tests and flaky CI infrastructure (#43983) 2026-10-01 17:46:43 +00:00
event_loop_lag.py perf(token_counter): memoize token counts of long texts per tokenizer (#51904) 2026-11-16 19:21:55 +00:00
fake_secret_vault.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
handbuilt_pdf.py fix(router): preserve provider PDF preflight compatibility (#54208) 2026-11-24 12:36:47 -08:00
messages_with_counts.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_agentic_followup_kwargs.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_allowed_tools_tool_choice.py fix(mcp): drop hosted MCP tool_choice under drop_params and honor drop_params in Bedrock batch rows (#53697) 2026-11-23 08:51:59 -08:00
test_anthropic_dedup_factory.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_api_route_to_call_types.py fix(auth): enforce key budgets on Google generateContent calls that leave the model out of the URL (#53404) 2026-11-22 22:04:46 +00:00
test_app_crypto.py test: move offline logging, secret manager and provider tests from legacy dirs into tests/unit (#45554) 2026-10-09 09:48:10 -07:00
test_audio_utils.py fix(transcription): stop billing a FLAC with no sample count as 576 trillion seconds (#50842) 2026-11-10 06:19:33 +00:00
test_aws_partition.py feat(s3-log): replace the python s3_v2 logger with a rust callback (#51539) 2026-11-14 23:03:58 +00:00
test_batch_utils.py fix(router): keep chat and other non-batch calls off Azure batch-only deployments in mixed groups (#53454) 2026-11-23 01:59:52 +00:00
test_bedrock_converse_dedup_factory.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_bridged_audio_output.py fix(bridges): refuse audio output on Responses, Messages and Completions before the provider call (#49960) 2026-11-23 07:14:40 +00:00
test_bug_report.py fix(proxy): cooldown errors carry 429, Sentry starts at boot without request bodies, tracing failures log at ERROR, exporter counters exported (#46344) 2026-10-16 00:53:52 +00:00
test_chat_completion_agentic_loop.py refactor(typing): clear basedpyright Any errors across 35 files (#53599) 2026-11-24 00:39:02 +00:00
test_classifier_logging.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_cli_token_utils.py fix(cli): end the local login when lite logout cannot check the OS keychain (#50098) 2026-11-08 11:17:59 +00:00
test_cloud_storage_security.py fix(bedrock): send s3:// image, document and video URLs to Converse as S3 sources (#50577) 2026-11-13 21:33:21 -08:00
test_codestral_provider_routing.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_core_helpers.py fix(proxy): say Claude Code, Codex, and Copilot Chat keep drop_params on when litellm_settings.drop_params is unreadable (#54083) 2026-11-24 01:46:12 -08:00
test_coroutine_checker.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_cost_discount.py feat(cost): price Claude token calls, budget holds and auth price checks in Rust (#52484) 2026-11-23 22:00:28 +00:00
test_custom_logger_registry.py test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00
test_dd_tracing.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
test_decisions_pricing.py feat(decisions): add Parallel AI web-grounded decisions (#49188) 2026-11-03 04:47:02 +00:00
test_decode_special_tokens.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_default_encoding.py test: move 76 live top-level tests to offline unit and integration coverage (#45352) 2026-10-08 10:33:35 -07:00
test_dot_notation_indexing.py fix(proxy): count tokens with the thinking, effort, and tool_choice that inference sends (#51079) 2026-11-14 02:29:16 +00:00
test_duration_parser.py fix(budgets): preserve calendar periods across DST (#53065) 2026-11-20 15:27:05 -08:00
test_ecb_reference_rates.py feat(ui): show an optional currency estimate on the Usage spend card (#51979) 2026-11-16 16:00:20 -08:00
test_env_utils.py fix(router): cap the default OTPM reservation and reconcile token counters after each call (#49054) 2026-10-29 20:32:04 -07:00
test_error_normalization.py fix(anthropic): answer an upstream Anthropic 529 as 529 overloaded_error on /v1/messages (#46063) 2026-11-03 17:37:09 -08:00
test_exception_mapping_utils.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
test_extract_base64_image.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_fallback_generalizations.py fix(cost-map): declare mid-conversation system support on Databricks Claude Opus 5.5 (#52214) 2026-11-17 05:09:07 +00:00
test_fallback_utils.py test(sdk): drain streaming logs before the next fallback test (#52945) 2026-11-19 21:28:09 +00:00
test_get_litellm_params.py fix: keep LiteLLM-internal params out of provider request bodies (#46083) 2026-11-16 22:20:05 -08:00
test_get_llm_provider_endpoint_match.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_get_llm_provider_logic.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_get_model_cost_map.py fix(cost-map): restore backward compatibility after the Rust loader move (#51545) 2026-11-14 08:12:17 +09:00
test_get_supported_openai_params.py fix(bedrock): backfill published regional service tier prices (#49113) 2026-10-29 19:46:57 -07:00
test_harness_context.py feat(lens): strip agent harness context from what the gateway sends Lens (#53226) 2026-11-21 12:16:58 -08:00
test_health_check_helpers.py feat(elevenlabs): add scribe_v2_realtime realtime transcription (#48984) 2026-11-25 03:15:34 +00:00
test_hidden_params.py perf(streaming): stop raising caught AttributeErrors inside pydantic on every streamed chunk (#49166) 2026-10-30 10:01:44 -07:00
test_image_handling.py fix(anthropic): send an https PDF image_url to Claude as a document (#49708) 2026-11-03 07:05:15 +00:00
test_initialize_dynamic_callback_params.py test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00
test_internal_call_metadata.py fix(responses): resume background response streams with stream and starting_after (#48819) 2026-10-29 02:31:22 -07:00
test_json_fragment_accumulator.py test: deflake the json accumulator drain test and the armed routing prefetch test (#49505) 2026-10-31 15:14:33 -07:00
test_json_schema_validation.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_litellm_logging.py fix(proxy): track spend for no-log requests when the spend logs ledger is off (#54309) 2026-11-25 00:35:04 +00:00
test_llm_judge.py fix(guardrails): resolve decision models in caller team scope (#46085) 2026-10-15 11:43:36 -07:00
test_llm_request_utils.py fix(aiand)!: show ai&'s name and logo, and sync ai&'s model list (#49158) 2026-10-30 10:55:26 -07:00
test_logging_callback_manager.py fix(logging): reuse the cached GenericAPILogger so config reloads stop leaking flush tasks (#50879) 2026-11-17 19:23:31 +00:00
test_logging_utils.py fix(logging): log Anthropic base64 sources on /v1/messages and pass-through routes as a size placeholder (#54161) 2026-11-24 12:13:09 -08:00
test_logging_worker.py fix(proxy): persist tag spend and drain success callbacks within the shutdown budget (#48988) 2026-10-29 09:32:18 -07:00
test_max_streaming_duration.py fix(router): log each dropped /v1/messages attempt through its own logging copy and open the retry without its usage (#45194) 2026-10-20 21:46:45 -07:00
test_model_cost_table.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_model_latest_aliases.py chore(cost-map): rolling registry audit 2026-11-12 (#51395) 2026-11-12 08:03:26 -08:00
test_model_param_helper.py feat(realtime)!: support latest OpenAI audio models (#35600) 2026-11-03 07:27:52 +00:00
test_model_response_utils.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_moderation_standard_logging.py test: replace 61 live logging and otel tests with offline unit and integration coverage (#45362) 2026-10-08 15:55:57 -07:00
test_oauth_token_exchange.py feat(microsoft_365_copilot): add Microsoft 365 Copilot chat provider with OAuth token exchange (#45158) 2026-10-09 15:19:07 -07:00
test_offline_mode.py feat(proxy): add LITELLM_OFFLINE_MODE, one switch for the fetches LiteLLM makes on its own (#48096) 2026-10-23 16:54:19 -07:00
test_optional_imports.py refactor(sdk): separate core AWS and tokenizer dependencies (#44447) 2026-10-08 19:07:57 -07:00
test_path_utils.py refactor(core): move path_utils from proxy common_utils to litellm_core_utils (#45775) 2026-10-10 19:07:22 +00:00
test_private_json.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_provider_affinity.py fix(headroom): forward tenant-scoped session affinity (#49255) 2026-10-30 12:32:03 -07:00
test_provider_specific_headers.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_ptu_pricing.py feat(proxy): split Azure PTU deployments across teams with per-team PTU ceilings and PTU-hour reporting (#43043) 2026-10-15 18:44:30 +00:00
test_realtime_errors.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_realtime_guardrails.py fix(bedrock): run realtime guardrails on Nova Sonic sessions (#49833) 2026-11-06 13:18:48 -08:00
test_realtime_streaming.py feat(elevenlabs): add scribe_v2_realtime realtime transcription (#48984) 2026-11-25 03:15:34 +00:00
test_redact_messages.py fix(guardrails): scan refusal text in output guardrails on chat and responses (#54039) 2026-11-24 05:27:45 -08:00
test_request_timeout_resolver.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_retry_after_headers.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_safe_divide_seconds.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_safe_json_dumps.py feat(accounting)!: make the SpendLogs ledger the default accounting path (#49872) 2026-11-05 09:14:10 -08:00
test_sensitive_data_masker.py feat(usage-export): include key and user metadata objects in daily activity export rows (#49360) 2026-11-20 03:58:01 +00:00
test_sentry_scrubbing.py feat(gnani): add native Prisma v2.5 and v3 REST transcription support (#52520) 2026-11-23 03:01:32 +00:00
test_served_output_texts.py fix(guardrails): clear token ids and logprobs when a post-call hook rewrites a non-streamed answer (#53758) 2026-11-24 06:16:09 +00:00
test_socket_logging.py fix(proxy): refund a Responses WebSocket's key TPM reservation when the socket closes (#51086) 2026-11-23 23:05:26 -08:00
test_standard_logging_payload_documents.py feat(standard-logging): add the StandardLoggingPayload types in rust (#52783) 2026-11-19 15:21:52 +09:00
test_stream_usage_logging.py test: replace 61 live logging and otel tests with offline unit and integration coverage (#45362) 2026-10-08 15:55:57 -07:00
test_streaming_chunk_builder_cursor.py fix(responses): bill the provider-reported input on a cut bridged stream (#53394) 2026-11-23 06:20:25 +00:00
test_streaming_chunk_builder_server_tool_use.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_streaming_chunk_builder_utils.py fix(gemini): keep Google's reason on streamed chunks and the assembled response (#52242) 2026-11-17 16:01:46 -08:00
test_streaming_handler.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
test_streaming_overhead.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00
test_text_completion_conversion.py refactor(types): replace Any with proven types in 14 files (#48928) 2026-10-29 07:26:07 -07:00
test_thread_pool_executor.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_token_counter.py fix(router): count pre-call context-window tokens with each deployment's tokenizer (#49764) 2026-11-25 03:04:19 +00:00
test_token_counter_tool.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_token_counter_tool_data.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_tokenizer.py fix(tokenizer): match tiktoken on lone surrogates and out-of-u32 tokens (#51720) 2026-11-18 01:29:55 +00:00
test_tool_fields.py fix(mcp): drop hosted MCP tool_choice under drop_params and honor drop_params in Bedrock batch rows (#53697) 2026-11-23 08:51:59 -08:00
test_tool_search_spend_logging.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_total_deadline.py feat(proxy): total_timeout bounds a whole non-streaming request (#50496) 2026-11-08 00:43:50 -08:00
test_url_utils.py fix(proxy): make litellm --test pick a served model, and send the master key only to a local proxy (#53866) 2026-11-24 04:17:02 +00:00
test_xai_oauth_routing.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
weakly_parked_logger.py fix(logging): retain streaming success logging tasks until they finish (#52067) 2026-11-16 11:21:05 -08:00