From c7ba7948bc79606f96382f4f334a528ba12ad28a Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Sat, 14 Mar 2026 10:41:25 +0500 Subject: [PATCH] =?UTF-8?q?PR=20#22867=20added=20=5Fremove=5Fscope=5Ffrom?= =?UTF-8?q?=5Fcache=5Fcontrol=20for=20Bedrock=20and=20Azur=E2=80=A6=20(#23?= =?UTF-8?q?183)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * PR #22867 added _remove_scope_from_cache_control for Bedrock and Azure AI but omitted Vertex AI. This applies the same pattern to VertexAIPartnerModelsAnthropicMessagesConfig." * PR #22867 added _remove_scope_from_cache_control for Bedrock and Azure AI but omitted Vertex AI. This applies the same pattern to VertexAIPartnerModelsAnthropicMessagesConfig." * PR #22867 added _remove_scope_from_cache_control to AzureAnthropicMessagesConfig but missed VertexAIPartnerModelsAnthropicMessagesConfi Rather than duplicating the method again, moved it up to the base AnthropicMessagesConfig so all providers inherit it, and removed the now-redundant copy from the Azure AI subclass. * PR #22867 added _remove_scope_from_cache_control to AzureAnthropicMessagesConfig but missed VertexAIPartnerModelsAnthropicMessagesConfi Rather than duplicating the method again, moved it up to the base AnthropicMessagesConfig so all providers inherit it, and removed the now-redundant copy from the Azure AI subclass. --------- Co-authored-by: Krish Dholakia --- .../messages/transformation.py | 32 +++++++++++++ .../transformation.py | 2 + ...artner_models_anthropic_messages_config.py | 45 +++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py index 4d0a2cd829b..e9ceea48220 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py @@ -50,6 +50,38 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): # "metadata", ] + def _remove_scope_from_cache_control( + self, anthropic_messages_request: Dict + ) -> None: + """ + Remove `scope` field from cache_control blocks. + + Some providers (Vertex AI, Azure AI Foundry) do not support the `scope` + field in cache_control (e.g. "global" for cross-request caching). + Processes both `system` and `messages` content blocks. + """ + + def _sanitize(cache_control: Any) -> None: + if isinstance(cache_control, dict): + cache_control.pop("scope", None) + + def _process_content_list(content: list) -> None: + for item in content: + if isinstance(item, dict) and "cache_control" in item: + _sanitize(item["cache_control"]) + + if "system" in anthropic_messages_request: + system = anthropic_messages_request["system"] + if isinstance(system, list): + _process_content_list(system) + + if "messages" in anthropic_messages_request: + for message in anthropic_messages_request["messages"]: + if isinstance(message, dict) and "content" in message: + content = message["content"] + if isinstance(content, list): + _process_content_list(content) + @staticmethod def _filter_billing_headers_from_system(system_param): """ diff --git a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/experimental_pass_through/transformation.py b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/experimental_pass_through/transformation.py index d3b0217d044..5c3bbf61ee2 100644 --- a/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/experimental_pass_through/transformation.py +++ b/litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/experimental_pass_through/transformation.py @@ -150,6 +150,8 @@ class VertexAIPartnerModelsAnthropicMessagesConfig(AnthropicMessagesConfig, Vert headers=headers, ) + self._remove_scope_from_cache_control(anthropic_messages_request) + anthropic_messages_request["anthropic_version"] = "vertex-2023-10-16" anthropic_messages_request.pop( diff --git a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py index b5f076262d7..391daa24f47 100644 --- a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py +++ b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_messages_config.py @@ -5,6 +5,7 @@ import pytest from litellm.llms.vertex_ai.vertex_ai_partner_models.anthropic.experimental_pass_through.transformation import ( VertexAIPartnerModelsAnthropicMessagesConfig, ) +from litellm.types.router import GenericLiteLLMParams def test_validate_environment_uses_vertex_ai_location(): @@ -248,3 +249,47 @@ def test_validate_environment_with_authorization_header_calculates_api_base(): # Verify Authorization header is still present assert "Authorization" in updated_headers, \ "Authorization header should be preserved" + + +def test_transform_anthropic_messages_request_removes_scope_from_cache_control(): + """Ensure scope field is removed from cache_control for Vertex AI (not supported).""" + config = VertexAIPartnerModelsAnthropicMessagesConfig() + + messages = [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "Hello", + "cache_control": {"type": "ephemeral", "scope": "global"}, + } + ], + } + ] + anthropic_messages_optional_request_params = { + "max_tokens": 1024, + "system": [ + { + "type": "text", + "text": "You are an AI assistant.", + "cache_control": {"type": "ephemeral", "scope": "global"}, + } + ], + } + + result = config.transform_anthropic_messages_request( + model="claude-sonnet-4-6", + messages=messages, + anthropic_messages_optional_request_params=anthropic_messages_optional_request_params, + litellm_params=GenericLiteLLMParams(), + headers={}, + ) + + # scope removed from system + assert "scope" not in result["system"][0]["cache_control"] + assert result["system"][0]["cache_control"]["type"] == "ephemeral" + + # scope removed from message content + assert "scope" not in result["messages"][0]["content"][0]["cache_control"] + assert result["messages"][0]["content"][0]["cache_control"]["type"] == "ephemeral"