fix(vertex_ai): validate cached content metadata before billing

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Devin AI 2026-09-18 20:38:55 +00:00
parent 70cc1c1c59
commit 35a4536b14

View file

@ -9,6 +9,7 @@ from functools import partial
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, Union, cast
import httpx
from pydantic import TypeAdapter
import litellm
from litellm import verbose_logger
@ -113,6 +114,7 @@ else:
SUPPORTED_REASONING_EFFORTS: Final = ("minimal", "low", "medium", "high", "none", "disable")
_VERTEX_AI_CACHED_CONTENT_CREATION_ADAPTER: Final = TypeAdapter(VertexAICachedContentCreation)
def _unsupported_reasoning_effort(reasoning_effort: str) -> UnsupportedParamsError:
@ -2510,7 +2512,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
base_usage: Final = VertexGeminiConfig._calculate_usage(completion_response=completion_response)
cached_content_creation: Final = logging_obj.model_call_details.get(VERTEX_AI_CACHED_CONTENT_KEY)
usage: Final = (
_add_cache_creation_usage(base_usage, cast(VertexAICachedContentCreation, cached_content_creation))
_add_cache_creation_usage(
base_usage,
_VERTEX_AI_CACHED_CONTENT_CREATION_ADAPTER.validate_python(cached_content_creation),
)
if isinstance(cached_content_creation, dict) and "total_token_count" in cached_content_creation
else base_usage
)
@ -3272,7 +3277,10 @@ class ModelResponseIterator:
)
cached_content_creation: Final = self.logging_obj.model_call_details.get(VERTEX_AI_CACHED_CONTENT_KEY)
usage: Final = (
_add_cache_creation_usage(base_usage, cast(VertexAICachedContentCreation, cached_content_creation))
_add_cache_creation_usage(
base_usage,
_VERTEX_AI_CACHED_CONTENT_CREATION_ADAPTER.validate_python(cached_content_creation),
)
if isinstance(cached_content_creation, dict) and "total_token_count" in cached_content_creation
else base_usage
)