Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions litellm/llms/bedrock/base_aws_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,24 @@ def get_bedrock_model_id(
model_id = BaseAWSLLM.encode_model_id(model_id=model_id)
else:
model_id = model
# Strip LiteLLM routing prefixes (e.g. "bedrock/", "invoke/",
# "bedrock/invoke/", "bedrock/converse/") that are not part of the
# actual Bedrock model ID. The converse path already does this; the
# invoke path must do the same so that ARN models such as
# bedrock/arn:aws:bedrock:…:inference-profile/global.anthropic.…
# are not forwarded verbatim to the Bedrock API, which would produce
# a malformed URL and cause botocore's EventStreamBuffer to receive
# a JSON error body instead of a binary event-stream — surfaced as a
# misleading ChecksumMismatch (0x223a7b22 == ':{"').
# Use strip_bedrock_routing_prefix (no break) so compound prefixes
# like "bedrock/invoke/arn:..." are fully stripped in one call.
from litellm.llms.bedrock.common_utils import strip_bedrock_routing_prefix

model_id = strip_bedrock_routing_prefix(model_id)
Comment on lines +464 to +466

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The import of strip_bedrock_routing_prefix is placed inside the function body instead of at the module level. Deferred imports are typically used to break circular dependencies, but there's no comment explaining why that's necessary here. If this location truly is required to avoid a circular import, a short inline comment would clarify intent. Otherwise, moving it to the top of the file keeps the import section clear and avoids the per-call overhead of looking up an already-cached module entry.

Suggested change
from litellm.llms.bedrock.common_utils import strip_bedrock_routing_prefix
model_id = strip_bedrock_routing_prefix(model_id)
# Deferred to avoid circular import with common_utils.
from litellm.llms.bedrock.common_utils import strip_bedrock_routing_prefix
model_id = strip_bedrock_routing_prefix(model_id)

# URL-encode ARNs so colons and slashes are safe in the URL path.
if model_id.startswith("arn:"):
model_id = BaseAWSLLM.encode_model_id(model_id=model_id)
return model_id

model_id = model_id.replace("invoke/", "", 1)
if provider == "llama" and "llama/" in model_id:
Expand Down
99 changes: 99 additions & 0 deletions tests/test_litellm/llms/bedrock/test_base_aws_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,102 @@ def test_is_already_running_as_role_ssl_verify_passed():
mock_boto3_client.assert_called_once_with(
"sts", verify="/path/to/ca-bundle.crt"
)


# ---------------------------------------------------------------------------
# LIT-3274: get_bedrock_model_id must strip "bedrock/" prefix and URL-encode
# ARNs for the invoke path (invoke-with-response-stream). Without this fix
# the Bedrock API receives a malformed URL, returns a JSON error body, and
# botocore's EventStreamBuffer raises ChecksumMismatch instead of the real
# error. 0x223a7b22 == ':{\"' — the start of a JSON object.
Comment on lines +2120 to +2122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The class-level ARN constant contains 086734376398, a 12-digit number that looks like a real AWS account ID. Test fixtures should use an obviously synthetic ID (e.g. 123456789012, Amazon's documented placeholder) to avoid embedding what may be a real account ID in a public repository's history.

# ---------------------------------------------------------------------------


class TestGetBedrockModelIdArnHandling:
"""Unit tests for get_bedrock_model_id with inference-profile ARNs."""

ARN = "arn:aws:bedrock:us-east-1:086734376398:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0"

def _call(self, model: str, optional_params: dict | None = None) -> str:
from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM

provider = BaseAWSLLM.get_bedrock_invoke_provider(model)
return BaseAWSLLM.get_bedrock_model_id(
model=model,
provider=provider,
optional_params=optional_params or {},
)

def test_arn_with_bedrock_prefix_is_stripped_and_encoded(self):
"""bedrock/arn:... must not appear verbatim in the model_id."""
model_id = self._call(f"bedrock/{self.ARN}")
assert (
"bedrock/arn" not in model_id
), f"'bedrock/' prefix not stripped; got: {model_id}"
# Must be URL-encoded (colons → %3A)
assert "%3A" in model_id, f"ARN not URL-encoded; got: {model_id}"
assert "%2F" in model_id, f"ARN slashes not URL-encoded; got: {model_id}"

def test_arn_with_compound_bedrock_invoke_prefix_is_fully_stripped_and_encoded(
self,
):
"""bedrock/invoke/arn:... — compound prefix — must be fully stripped.

The old fix used ``break`` after the first matched prefix, so
``bedrock/invoke/arn:...`` would only strip ``bedrock/``, leaving
``invoke/arn:...``. The subsequent ``.replace('invoke/', '')`` call
then returned the bare unencoded ARN, reproducing the same
malformed-URL bug the fix aimed to prevent.

strip_bedrock_routing_prefix() has no break and handles this correctly.
"""
model_id = self._call(f"bedrock/invoke/{self.ARN}")
assert (
"invoke/" not in model_id
), f"'invoke/' prefix not stripped; got: {model_id}"
assert (
"bedrock/" not in model_id
), f"'bedrock/' prefix not stripped; got: {model_id}"
assert "%3A" in model_id, f"ARN not URL-encoded; got: {model_id}"
assert "%2F" in model_id, f"ARN slashes not URL-encoded; got: {model_id}"

def test_bare_arn_is_encoded(self):
"""Direct ARN without routing prefix must also be URL-encoded."""
model_id = self._call(self.ARN)
assert "%3A" in model_id, f"ARN not URL-encoded; got: {model_id}"
assert "%2F" in model_id, f"ARN slashes not URL-encoded; got: {model_id}"

def test_arn_url_matches_expected(self):
"""Full URL built from messages config must match expected encoded form."""
import urllib.parse
from litellm.llms.bedrock.messages.invoke_transformations.anthropic_claude3_transformation import (
AmazonAnthropicClaudeMessagesConfig,
)

config = AmazonAnthropicClaudeMessagesConfig()
url = config.get_complete_url(
api_base=None,
api_key=None,
model=f"bedrock/{self.ARN}",
optional_params={"aws_region_name": "us-east-1"},
litellm_params={},
stream=True,
)
encoded_arn = urllib.parse.quote(self.ARN, safe="")
expected = (
f"https://bedrock-runtime.us-east-1.amazonaws.com"
f"/model/{encoded_arn}/invoke-with-response-stream"
)
assert (
url == expected
), f"URL mismatch:\n got: {url}\n expected: {expected}"

def test_regular_model_id_unaffected(self):
"""Non-ARN model IDs must continue to work as before."""
model_id = self._call("anthropic.claude-3-sonnet-20240229-v1:0")
assert model_id == "anthropic.claude-3-sonnet-20240229-v1:0"

def test_invoke_prefixed_model_unaffected(self):
"""invoke/ prefix stripping still works after the fix."""
model_id = self._call("invoke/anthropic.claude-3-sonnet-20240229-v1:0")
assert model_id == "anthropic.claude-3-sonnet-20240229-v1:0"
Loading