Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(gemini): drop ttl values whose expiry Google cannot store (past the year 9999)

mateo-berri5ad184783546
diff --git a/litellm/llms/vertex_ai/context_caching/transformation.py b/litellm/llms/vertex_ai/context_caching/transformation.py
index 79e435b790c..d5478920de0 100644
--- a/litellm/llms/vertex_ai/context_caching/transformation.py
+++ b/litellm/llms/vertex_ai/context_caching/transformation.py
@@ -6,6 +6,7 @@ Why separate file? Make it easy to see how transformation works
import re
from collections.abc import Sequence
+from datetime import datetime, timezone
from types import MappingProxyType
from typing import Final, Literal
@@ -89,7 +90,7 @@ def extract_ttl_from_cached_messages(messages: list[AllMessageValues]) -> str |
_TTL_PATTERN: Final = re.compile(r"^([0-9]*\.?[0-9]+)([smh])$")
_TTL_UNIT_SECONDS: Final = MappingProxyType({"s": 1, "m": 60, "h": 3600})
-_PROTOBUF_DURATION_MAX_SECONDS: Final = 315_576_000_000
+_LAST_EXPIRY_GOOGLE_ACCEPTS: Final = datetime(9999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
def _normalize_ttl_to_seconds(ttl: object) -> str | None:
@@ -99,7 +100,8 @@ def _normalize_ttl_to_seconds(ttl: object) -> str | None:
if match is None:
return None
seconds: Final = round(float(match.group(1)) * _TTL_UNIT_SECONDS[match.group(2)], 9)
- if not 0 < seconds <= _PROTOBUF_DURATION_MAX_SECONDS:
+ longest_ttl: Final = (_LAST_EXPIRY_GOOGLE_ACCEPTS - datetime.now(timezone.utc)).total_seconds()
+ if not 0 < seconds <= longest_ttl:
return None
return f"{seconds:.9f}".rstrip("0").rstrip(".") + "s"
diff --git a/tests/test_litellm/llms/vertex_ai/context_caching/test_context_caching_ttl.py b/tests/test_litellm/llms/vertex_ai/context_caching/test_context_caching_ttl.py
index 82f7d3dfc7d..44ce97b73ac 100644
--- a/tests/test_litellm/llms/vertex_ai/context_caching/test_context_caching_ttl.py
+++ b/tests/test_litellm/llms/vertex_ai/context_caching/test_context_caching_ttl.py
@@ -21,8 +21,7 @@ class TestTTLNormalization:
("1h", "3600s"),
("0.5h", "1800s"),
("48h", "172800s"),
- ("315576000000s", "315576000000s"),
- ("87660000h", "315576000000s"),
+ ("61320000h", "220752000000s"),
],
)
def test_normalizes_supported_units_to_seconds(self, ttl, expected):
@@ -45,8 +44,8 @@ class TestTTLNormalization:
"3600ss",
"1 h",
"0.0000000001s",
- "315576000001s",
- "87660001h",
+ "251700000000s",
+ "69920000h",
"9" * 400 + "h",
None,
123,