From ceff1831b08bb9fe0d1fa8c00e32b00abadf8b40 Mon Sep 17 00:00:00 2001 From: flex-myeonghyeon Date: Mon, 13 Apr 2026 14:28:46 +0900 Subject: [PATCH] fix: include tool_choice in vertex AI context caching key and request body tool_choice was not being popped from optional_params or passed to get_cache_key, causing cache misses when only tool_choice differed. Also assign tool_choice to the cached content request body alongside tools. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../context_caching/vertex_ai_context_caching.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py index b677cf3b1ec..9c38c09562f 100644 --- a/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py +++ b/litellm/llms/vertex_ai/context_caching/vertex_ai_context_caching.py @@ -338,6 +338,7 @@ class ContextCachingEndpoints(VertexBase): return messages, optional_params, None tools = optional_params.pop("tools", None) + tool_choice = optional_params.pop("tool_choice", None) ## AUTHORIZATION ## token, url = self._get_token_and_url_context_caching( @@ -370,7 +371,7 @@ class ContextCachingEndpoints(VertexBase): ## CHECK IF CACHED ALREADY generated_cache_key = local_cache_obj.get_cache_key( - messages=cached_messages, tools=tools, model=model + messages=cached_messages, tools=tools, tool_choice=tool_choice, model=model ) google_cache_name = self.check_cache( cache_key=generated_cache_key, @@ -401,6 +402,7 @@ class ContextCachingEndpoints(VertexBase): ) cached_content_request_body["tools"] = tools + cached_content_request_body["tool_choice"] = tool_choice ## LOGGING logging_obj.pre_call( @@ -486,6 +488,7 @@ class ContextCachingEndpoints(VertexBase): return messages, optional_params, None tools = optional_params.pop("tools", None) + tool_choice = optional_params.pop("tool_choice", None) ## AUTHORIZATION ## token, url = self._get_token_and_url_context_caching( @@ -515,7 +518,7 @@ class ContextCachingEndpoints(VertexBase): ## CHECK IF CACHED ALREADY generated_cache_key = local_cache_obj.get_cache_key( - messages=cached_messages, tools=tools, model=model + messages=cached_messages, tools=tools, tool_choice=tool_choice, model=model ) google_cache_name = await self.async_check_cache( cache_key=generated_cache_key, @@ -547,6 +550,7 @@ class ContextCachingEndpoints(VertexBase): ) cached_content_request_body["tools"] = tools + cached_content_request_body["tool_choice"] = tool_choice ## LOGGING logging_obj.pre_call(