From 2c4db79e3863558ebdf4a1cb6e82b3e829550cfa Mon Sep 17 00:00:00 2001 From: nuernber Date: Fri, 11 Sep 2026 16:04:49 -0700 Subject: [PATCH] test(prompt-caching): add coverage for message-level cache_control on string content Add test verifying get_prompt_caching_ttl extracts TTL from cache_control when it appears as a sibling to a string content field rather than inside a content-block list. --- .../test_prompt_caching_deployment_check.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_litellm/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py b/tests/test_litellm/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py index cabd41c1943..08a5f037474 100644 --- a/tests/test_litellm/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py +++ b/tests/test_litellm/router_utils/pre_call_checks/test_prompt_caching_deployment_check.py @@ -119,6 +119,18 @@ def test_prompt_caching_affinity_ttl_matches_cache_control(ttl: str | None, expe assert PromptCachingCache.get_prompt_caching_ttl(messages) == expected_affinity_ttl +def test_message_level_cache_control_on_string_content_sets_affinity_ttl(): + """cache_control can sit as a sibling of a string `content` field rather than inside a + content-block list, e.g. {"role": "system", "content": "...", "cache_control": {...}}. + get_prompt_caching_ttl must still pick it up.""" + messages = cast( + List[AllMessageValues], + [{"role": "system", "content": "cached", "cache_control": {"type": "ephemeral", "ttl": "1h"}}], + ) + + assert PromptCachingCache.get_prompt_caching_ttl(messages) == 3600 + + def test_mixed_cache_ttls_use_the_shortest_affinity_ttl(): messages = cast( List[AllMessageValues],