litellm/tests/unit/caching
Omar BOUKHERYS 28bf4ee646
fix(gemini): bill batch embeddings from usageMetadata so media inputs are not free (#49682)
* fix(gemini): bill batch embeddings from usageMetadata so media inputs are not free

* fix(gemini): read batchEmbedContents usageMetadata from the response itself and cover video tokens

process_response now reads usageMetadata from the batch response it already receives, so the
handler and the Vertex passthrough no longer pass it as a separate argument. The test now covers a
video input alongside the image input

* fix(gemini): treat usageMetadata without a prompt count as absent and keep resolved files on the batch path

* fix(gemini): give each cached batch embedding its own modality tokens

The embedding cache split a batch's usage evenly across its inputs, so a cached text input came back with half of an image's tokens. The Gemini batch response now carries each input's own usage, attributed by modality, and the cache stores that when it is present

A usage detail entry with no tokenCount no longer drops Google's whole prompt count. The gemini-embeddings-002 cost case now expects Google's 7 tokens over the local count

* fix(gemini): keep per-input embedding usage off logged hidden params and keep valid usage entries

The per-input usage the cache reads now rides on a private EmbeddingResponse field instead of a hidden param, so it no longer reaches callback metadata, Langfuse, or Datadog payloads. A usage detail entry without a token count no longer drops the other valid entries. The sync openai-like embedding path now unpacks the response through the same typed fields as the async path

* fix(gemini): split cached text usage by input length and drop a provider item_usage key

Inputs that share a modality used to split Google's total evenly, so a short text batched with a long one came back from the cache with half the batch's text tokens. Text now splits by each input's local token count. Images, audio and video still split evenly, since Google reports one total per modality

EmbeddingResponse keeps item_usage only when it is a tuple of Usage objects, so a provider body with an item_usage key is dropped like other unknown keys and no longer breaks the per-input cache write

Adds a test that per-input usage stays off the logged hidden params, and types the cache test parameters as read-only

* fix(gemini): keep a video's text and audio tokens on the video and weight cached text shares by length

In a batch that mixes text with video or audio, text inputs keep their own local token count and the rest of Google's text count goes to the video and audio inputs, whose transcripts Google bills as text. Audio tokens with no audio input go to the video inputs that carry the audio track instead of to every input

Batches without video or audio split Google's text count by each input's character length, so a text-only batch no longer runs the tokenizer once per input on every request, even with caching off. A text-only batch without modality counts, such as gemini-embedding-001's local count, is split the same way

A file block passed as a read-only mapping keeps its own image, audio or video tokens instead of being split as text, and when Google's modality counts add up to more than promptTokenCount each input keeps its own counts instead of taking a negative share

* fix(gemini): weight cached text shares by UTF-8 bytes so non-Latin text keeps its share

A cache hit split Google's text count by character length, so an English and a Chinese text of the same length got equal shares even though the Chinese one takes far more tokens

UTF-8 byte length costs the same as character length and follows token counts across scripts, since a Chinese character takes three bytes where a Latin letter takes one

* fix(gemini): give a video's extracted audio tokens to the video on cache hits and keep a lone surrogate from raising

With audio_track_extraction on, Google's AUDIO count covers both the audio inputs and each video's audio track, so cache hits now split it across every audio and video input. Without it, AUDIO stays on the audio inputs as before

Text length now encodes with surrogatepass, so an input holding a lone surrogate gets its share instead of raising after Google already answered

New cases cover the text cap next to a video, a batch with no text input, an audio input, each text keeping its own count next to a video, the no-usageMetadata split, and a media batch without modality details

* fix(gemini): read every audio_track_extraction spelling Google accepts and decide it before the request

Google accepts embed_content_config and audio_track_extraction in snake_case, and treats "true" in any case and 1 as on. The flag reader only took a bool audioTrackExtraction under embedContentConfig, so a string or snake_case flag split a video's audio track onto an mp3 in the same batch on cache hits

The flag is now read from the request params before the call, so a config the reader cannot parse fails before Google answers and bills

A sync test covers the named param, string values, extra_body, the snake_case config and field, the named param overriding extra_body, and the flag unset

* fix(gemini): read every boolean form Google accepts for audio_track_extraction

Google's batchEmbedContents also reads "yes", "y", "t" in any case and the number 1.0 as on, so a cached audio input next to a video sent with one of those now gets its share of the video's audio track, the same as with true

A test also pins that an embedContentConfig the flag reader cannot parse fails before the request reaches Google, so it is never billed

* fix(gemini): send an embedContentConfig with non-string keys to Google as before

The audio_track_extraction reader validated the config as a string-keyed mapping, so a config with a non-string key raised a validation error before the request went out. It now reads any mapping, so Google answers that config with its own 400, the same as on main

The caching test's embed helper takes a Sequence so the test file adds no LIT001 hit

* fix(typing): validate the cached embedding dict before building the response

* test(gemini): cover text content blocks in batch embedding usage splits

* fix(gemini): bill a PDF-only batch embedding at the image rate when Google omits modality counts

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-23 04:02:39 +00:00
..
__init__.py ci: move caching, proxy-extras, gateway and enterprise tests into tests/unit and run them from litellm-tests (#42902) 2026-09-24 15:49:59 -07:00
_event_loop_reuse.py fix(caching): key cached async clients by a per-loop token instead of id(loop) (#48877) 2026-10-29 13:14:48 -07:00
redis_batch_double.py refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384) 2026-11-20 17:57:37 +09:00
test_azure_blob_cache.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_cache_preset_key.py ci: move caching, proxy-extras, gateway and enterprise tests into tests/unit and run them from litellm-tests (#42902) 2026-09-24 15:49:59 -07:00
test_caching.py fix(gemini): bill batch embeddings from usageMetadata so media inputs are not free (#49682) 2026-11-23 04:02:39 +00:00
test_caching_handler.py fix(caching): prevent duplicate success logging on cache hits (#52852) 2026-11-22 00:14:05 +00:00
test_check_and_fix_namespace_none_guard.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_dual_cache.py perf(caching): format dual cache debug logs lazily (#51902) 2026-11-15 06:50:04 +00:00
test_embedding_router.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
test_evicted_client_closer.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_gcs_cache.py test: migrate legacy unit dirs and unrun folders into tests/unit, tests/integration and tests/e2e (#45940) 2026-10-11 06:24:57 -07:00
test_in_memory_cache.py feat(router): add deployment rpd and tpd daily limits to enforce_model_rate_limits (#48875) 2026-10-28 15:52:44 -07:00
test_llm_caching_handler.py refactor(rust_bridge): route every Rust-to-Python call through host modules (#50822) 2026-11-11 01:57:54 +00:00
test_llm_client_cache_e2e.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_qdrant_semantic_cache.py perf(token_counter): memoize token counts of long texts per tokenizer (#51904) 2026-11-16 19:21:55 +00:00
test_redis_batch.py refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384) 2026-11-20 17:57:37 +09:00
test_redis_cache.py fix(responses): stop the background stream when a polling response is cancelled (#49384) 2026-11-18 14:33:39 +00:00
test_redis_cluster_cache.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_redis_cluster_node_isolation.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_redis_connection_pool.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_redis_semantic_cache.py perf(token_counter): memoize token counts of long texts per tokenizer (#51904) 2026-11-16 19:21:55 +00:00
test_request_redis_batch_post_call.py refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384) 2026-11-20 17:57:37 +09:00
test_request_redis_batch_pre_call.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_responses_stream_cache_keys.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00
test_s3_cache.py test: move routing, caching and callback tests out of local_testing (#45761) 2026-10-10 09:33:09 -07:00
test_unit_test_caching.py fix(caching): read the cache key's model group from the router's metadata for the call (#48917) 2026-10-29 05:35:23 -07:00
test_valkey_semantic_cache.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00