From 7a5e49a3288f7a00f635563e6dff009e0a103f92 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Tue, 16 Dec 2025 11:56:46 +0530 Subject: [PATCH] fix: skip adding beta headers for vertex ai as it is not suppported --- litellm/llms/anthropic/chat/handler.py | 2 +- litellm/llms/anthropic/chat/transformation.py | 6 +++ ...partner_models_anthropic_transformation.py | 43 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.py index 90c8c30eed..7649c276b3 100644 --- a/litellm/llms/anthropic/chat/handler.py +++ b/litellm/llms/anthropic/chat/handler.py @@ -340,7 +340,7 @@ class AnthropicChatCompletion(BaseLLM): data = config.transform_request( model=model, messages=messages, - optional_params=optional_params, + optional_params={**optional_params, "is_vertex_request": is_vertex_request}, litellm_params=litellm_params, headers=headers, ) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 628121ab11..86f4832c2a 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -942,6 +942,12 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): self, headers: dict, optional_params: dict ) -> dict: """Update headers with optional anthropic beta.""" + + # Skip adding beta headers for Vertex requests + # Vertex AI handles these headers differently + is_vertex_request = optional_params.get("is_vertex_request", False) + if is_vertex_request: + return headers _tools = optional_params.get("tools", []) for tool in _tools: diff --git a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py index b129b7bab7..5f2dd387b9 100644 --- a/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py +++ b/tests/test_litellm/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py @@ -72,3 +72,46 @@ def test_vertex_ai_anthropic_web_search_header_in_completion(): # because Anthropic doesn't require it assert "anthropic-beta" not in headers_non_vertex or "web-search" not in headers_non_vertex.get("anthropic-beta", ""), \ "anthropic-beta with web-search should not be present for non-Vertex requests" + + +def test_vertex_ai_anthropic_structured_output_header_not_added(): + """Test that structured output beta headers are NOT added for Vertex AI requests""" + from litellm.llms.anthropic.chat.transformation import AnthropicConfig + + config = AnthropicConfig() + + # Test case 1: Vertex request with output_format should NOT add beta header + headers_vertex = {} + optional_params_vertex = { + 'output_format': { + 'type': 'json_schema', + 'json_schema': { + 'name': 'MathResult', + 'schema': {'properties': {'result': {'type': 'integer'}}} + } + }, + 'is_vertex_request': True + } + result_vertex = config.update_headers_with_optional_anthropic_beta(headers_vertex, optional_params_vertex) + + assert "anthropic-beta" not in result_vertex, \ + f"Vertex request should NOT have anthropic-beta header for structured output, got: {result_vertex.get('anthropic-beta')}" + + # Test case 2: Non-Vertex request with output_format SHOULD add beta header + headers_non_vertex = {} + optional_params_non_vertex = { + 'output_format': { + 'type': 'json_schema', + 'json_schema': { + 'name': 'MathResult', + 'schema': {'properties': {'result': {'type': 'integer'}}} + } + }, + 'is_vertex_request': False + } + result_non_vertex = config.update_headers_with_optional_anthropic_beta(headers_non_vertex, optional_params_non_vertex) + + assert "anthropic-beta" in result_non_vertex, \ + "Non-Vertex request SHOULD have anthropic-beta header for structured output" + assert result_non_vertex["anthropic-beta"] == "structured-outputs-2025-11-13", \ + f"Expected 'structured-outputs-2025-11-13', got: {result_non_vertex.get('anthropic-beta')}"