mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
fix(vertex_ai): keep context-1m beta out of the Vertex anthropic-beta header
Vertex's rawPredict validates the anthropic-beta HTTP header against an allowed
set and returns a 400 ("Unexpected value(s) context-1m-2025-08-07 for the
anthropic-beta header") when context-1m-2025-08-07 is sent there. Anthropic betas
on Vertex are read from the anthropic_beta request body, so that beta has to travel
in the body only.
VertexAIAnthropicConfig.transform_request was forwarding every beta into both the
body and the header (#22321). That is correct for betas Vertex wants in the header
(tool-search, context_management) but it breaks 1M context: any request asking for
context-1m on a Vertex Claude deployment fails with a 400, and a fallback that lands
on Vertex carrying the [1m] beta dies the same way.
This keeps body-only betas (currently context-1m-2025-08-07) in the anthropic_beta
body field while excluding them from the anthropic-beta header
This commit is contained in:
parent
8e30cfbeb1
commit
15944c3f84
2 changed files with 63 additions and 1 deletions
|
|
@ -12,6 +12,10 @@ from litellm.types.utils import ModelResponse
|
|||
from ....anthropic.chat.transformation import AnthropicConfig
|
||||
from .output_params_utils import sanitize_vertex_anthropic_output_params
|
||||
|
||||
# Betas that Vertex's rawPredict rejects (400) when sent in the anthropic-beta HTTP
|
||||
# header; they are only accepted in the anthropic_beta request body (e.g. context-1m).
|
||||
VERTEX_BODY_ONLY_ANTHROPIC_BETAS = frozenset({"context-1m-2025-08-07"})
|
||||
|
||||
|
||||
class VertexAIError(Exception):
|
||||
def __init__(self, status_code, message):
|
||||
|
|
@ -137,7 +141,9 @@ class VertexAIAnthropicConfig(AnthropicConfig):
|
|||
|
||||
if beta_set:
|
||||
data["anthropic_beta"] = list(beta_set)
|
||||
headers["anthropic-beta"] = ",".join(beta_set)
|
||||
header_betas = beta_set - VERTEX_BODY_ONLY_ANTHROPIC_BETAS
|
||||
if header_betas:
|
||||
headers["anthropic-beta"] = ",".join(header_betas)
|
||||
|
||||
return data
|
||||
|
||||
|
|
|
|||
|
|
@ -463,6 +463,62 @@ def test_vertex_ai_anthropic_no_extra_headers_unchanged():
|
|||
assert "anthropic-beta" not in headers
|
||||
|
||||
|
||||
def test_vertex_ai_anthropic_context_1m_beta_in_body_not_header():
|
||||
"""context-1m-2025-08-07 must go in the anthropic_beta body field only.
|
||||
|
||||
Vertex's rawPredict 400s ("Unexpected value(s) ... for the anthropic-beta header")
|
||||
when this beta is sent in the HTTP header, so it must not be forwarded there.
|
||||
"""
|
||||
config = VertexAIAnthropicConfig()
|
||||
|
||||
headers = {}
|
||||
optional_params = {
|
||||
"max_tokens": 100,
|
||||
"is_vertex_request": True,
|
||||
"extra_headers": {"anthropic-beta": "context-1m-2025-08-07"},
|
||||
}
|
||||
|
||||
result = config.transform_request(
|
||||
model="claude-sonnet-4-5@20250929",
|
||||
messages=[{"role": "user", "content": "Hello"}],
|
||||
optional_params=optional_params,
|
||||
litellm_params={},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert "context-1m-2025-08-07" in result["anthropic_beta"]
|
||||
assert "anthropic-beta" not in headers
|
||||
|
||||
|
||||
def test_vertex_ai_anthropic_context_1m_excluded_from_header_but_others_kept():
|
||||
"""When context-1m is combined with a header-compatible beta, only context-1m is
|
||||
stripped from the HTTP header; both still travel in the body."""
|
||||
config = VertexAIAnthropicConfig()
|
||||
|
||||
headers = {}
|
||||
optional_params = {
|
||||
"max_tokens": 100,
|
||||
"is_vertex_request": True,
|
||||
"extra_headers": {
|
||||
"anthropic-beta": "context-1m-2025-08-07,interleaved-thinking-2025-05-14",
|
||||
},
|
||||
}
|
||||
|
||||
result = config.transform_request(
|
||||
model="claude-sonnet-4-5@20250929",
|
||||
messages=[{"role": "user", "content": "Hello"}],
|
||||
optional_params=optional_params,
|
||||
litellm_params={},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
assert "context-1m-2025-08-07" in result["anthropic_beta"]
|
||||
assert "interleaved-thinking-2025-05-14" in result["anthropic_beta"]
|
||||
|
||||
assert "interleaved-thinking-2025-05-14" in headers["anthropic-beta"]
|
||||
assert "context-1m-2025-08-07" not in headers["anthropic-beta"]
|
||||
|
||||
|
||||
def test_vertex_ai_partner_models_anthropic_remove_prompt_caching_scope_beta_header():
|
||||
"""
|
||||
Test that remove_unsupported_beta correctly filters out prompt-caching-scope-2026-01-05
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue