Skip to content

Commit 085d559

Browse files
irfansofyanaclaude
andcommitted
fix(bedrock): Only forward anthropic_beta header to Claude models
Fix issue where anthropic_beta header was being sent to all Bedrock models via the /v1/messages endpoint, causing "unknown variant: anthropic_beta" errors on non-Claude models like Qwen and Nova. Changes: - Use BedrockLLM.get_bedrock_invoke_provider() for robust model detection - Only add anthropic_beta to request when provider is "anthropic" - Add comprehensive tests for Claude and non-Claude models - Update documentation explaining the filtering behavior This ensures Claude Code works seamlessly with non-Claude Bedrock models while preserving full anthropic_beta support for Claude models. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 1f6d8fc commit 085d559

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,16 @@ def transform_anthropic_messages_request(
128128
# 3. `model` is not allowed in request body for bedrock invoke
129129
if "model" in anthropic_messages_request:
130130
anthropic_messages_request.pop("model", None)
131-
132-
# 4. Handle anthropic_beta from user headers
133-
anthropic_beta_list = get_anthropic_beta_from_headers(headers)
134-
if anthropic_beta_list:
135-
anthropic_messages_request["anthropic_beta"] = anthropic_beta_list
131+
132+
# 4. Handle anthropic_beta from user headers - ONLY for Claude models
133+
# Use robust provider detection to avoid sending unsupported headers to non-Claude models
134+
from litellm import BedrockLLM
135+
136+
bedrock_provider = BedrockLLM.get_bedrock_invoke_provider(model=model)
137+
if bedrock_provider == "anthropic":
138+
anthropic_beta_list = get_anthropic_beta_from_headers(headers)
139+
if anthropic_beta_list:
140+
anthropic_messages_request["anthropic_beta"] = anthropic_beta_list
136141

137142
return anthropic_messages_request
138143

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
# /v1/messages
22

3-
This folder contains transformation logic for calling bedrock models in the Anthropic /v1/messages API spec.
3+
This folder contains transformation logic for calling bedrock models in the Anthropic /v1/messages API spec.
4+
5+
## Important Notes
6+
7+
- The `anthropic_beta` header is only forwarded to **Claude models** (models with provider "anthropic")
8+
- Non-Claude models (Qwen, Nova, etc.) will have this header stripped automatically
9+
- This prevents "unknown variant: anthropic_beta" errors on models that don't support Anthropic beta features
10+
- Provider detection is performed using `BedrockLLM.get_bedrock_invoke_provider()` for robust model identification

tests/test_litellm/llms/bedrock/test_anthropic_beta_support.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,48 @@ def test_anthropic_beta_all_supported_features(self):
163163
)
164164

165165
assert "anthropic_beta" in result
166-
assert result["anthropic_beta"] == supported_features
166+
assert result["anthropic_beta"] == supported_features
167+
168+
def test_anthropic_beta_excluded_for_non_claude_models(self):
169+
"""Test that anthropic_beta is NOT included for non-Claude models using provider detection."""
170+
config = AmazonAnthropicClaudeMessagesConfig()
171+
headers = {"anthropic-beta": "some-beta-feature"}
172+
173+
# Test with Qwen model (various formats)
174+
for qwen_model in ["qwen3-72b", "bedrock/qwen3-72b", "qwen.qwen3-72b-v1:0"]:
175+
result = config.transform_anthropic_messages_request(
176+
model=qwen_model,
177+
messages=[{"role": "user", "content": "test"}],
178+
anthropic_messages_optional_request_params={},
179+
litellm_params={},
180+
headers=headers,
181+
)
182+
# anthropic_beta should NOT be in the request for non-Claude models
183+
assert "anthropic_beta" not in result, f"anthropic_beta should not be present for {qwen_model}"
184+
185+
# Test with Nova model
186+
result_nova = config.transform_anthropic_messages_request(
187+
model="us.amazon.nova-pro-v1:0",
188+
messages=[{"role": "user", "content": "test"}],
189+
anthropic_messages_optional_request_params={},
190+
litellm_params={},
191+
headers=headers,
192+
)
193+
assert "anthropic_beta" not in result_nova
194+
195+
# Test with Claude models (various formats)
196+
for claude_model in [
197+
"anthropic.claude-3-5-sonnet-20240620-v1:0",
198+
"invoke/anthropic.claude-3-5-sonnet-20240620-v1:0",
199+
"bedrock/claude-3-5-sonnet",
200+
]:
201+
result_claude = config.transform_anthropic_messages_request(
202+
model=claude_model,
203+
messages=[{"role": "user", "content": "test"}],
204+
anthropic_messages_optional_request_params={},
205+
litellm_params={},
206+
headers=headers,
207+
)
208+
# anthropic_beta SHOULD be in the request for Claude models
209+
assert "anthropic_beta" in result_claude, f"anthropic_beta should be present for {claude_model}"
210+
assert result_claude["anthropic_beta"] == ["some-beta-feature"]

0 commit comments

Comments
 (0)