Skip to content

fix(bedrock): strip bedrock/ prefix and URL-encode ARNs in get_bedrock_model_id (LIT-3274) - #28551

Closed
krrish-berri-2 wants to merge 2 commits into
litellm_internal_stagingfrom
krrishdholakia1/lit-3274-v1messages-with-bedrock-inference-profile-arns-returns
Closed

krrish-berri-2 wants to merge 2 commits into
litellm_internal_stagingfrom
krrishdholakia1/lit-3274-v1messages-with-bedrock-inference-profile-arns-returns

Conversation

@krrish-berri-2

Copy link
Copy Markdown
Contributor

Problem

/v1/messages with Bedrock inference profile ARNs fails with ChecksumMismatch (HTTP 500).

Root cause: get_bedrock_model_id() in base_aws_llm.py falls back to the raw model string when model_id is not set in optional_params. It did not strip the bedrock/ routing prefix or URL-encode ARNs.

For bedrock/arn:aws:bedrock:us-east-1:<ACCT>:inference-profile/global.anthropic... the built URL was:

/model/bedrock/arn:aws:bedrock:…/invoke-with-response-stream  ❌

Bedrock returned a JSON error body. AWSEventStreamDecoder passed those bytes into botocore's EventStreamBuffer which expects binary event-stream framing. Checksum validation failed on the JSON prelude (0x223a7b22 == ':{"') — producing a misleading ChecksumMismatch instead of the real error.

Fix

Strip bedrock/ (and invoke/) routing prefix, then URL-encode if the result is an ARN — matching what the converse path already does in converse_handler.py.

After fix:

/model/arn%3Aaws%3Abedrock%3A…%3Ainference-profile%2Fglobal.anthropic…/invoke-with-response-stream  ✅

Real AWS call returns HTTP 403 (IAM permission error) instead of ChecksumMismatch.

Tests

5 new unit tests in TestGetBedrockModelIdArnHandling:

  • ARN with bedrock/ prefix → stripped + encoded
  • Bare ARN → encoded
  • Full URL matches expected encoded form
  • Regular model IDs unaffected
  • invoke/ prefix stripping still works

121/121 pass in bedrock test suite.

Fixes: LIT-3274

…k_model_id for invoke path

The invoke path (used by /v1/messages → Anthropic SDK / Claude Code) called
get_bedrock_model_id() which, when falling back to the raw model string, did
not strip the 'bedrock/' routing prefix and did not URL-encode ARNs.

For a model like:
  bedrock/arn:aws:bedrock:us-east-1:<ACCOUNT>:inference-profile/global.anthropic...

the URL built was:
  /model/bedrock/arn:aws:bedrock:…/invoke-with-response-stream  ❌

Bedrock returned a JSON error body.  LiteLLM's AWSEventStreamDecoder passed
those bytes into botocore's EventStreamBuffer which expects binary event-stream
framing.  Checksum validation failed on the JSON prelude (0x223a7b22 == ':{"')
producing a misleading botocore.eventstream.ChecksumMismatch instead of the
actual Bedrock error.

Fix: strip 'bedrock/' (and 'invoke/') routing prefix from model string, then
URL-encode if the result is an ARN — matching what the converse path already
does in converse_handler.py.

Fixes: LIT-3274
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing krrishdholakia1/lit-3274-v1messages-with-bedrock-inference-profile-arns-returns (ac6e1e1) with main (79b4578)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a ChecksumMismatch error on the Bedrock invoke path when an inference-profile ARN is passed with a bedrock/ routing prefix, by stripping routing prefixes via the existing strip_bedrock_routing_prefix utility and then URL-encoding the resulting ARN before building the request URL.

  • base_aws_llm.py: Adds prefix-stripping and ARN URL-encoding to get_bedrock_model_id's else-branch, matching what the converse path already does. Uses strip_bedrock_routing_prefix (which iterates the full prefix list without a break) to correctly handle compound prefixes like bedrock/invoke/arn:….
  • test_base_aws_llm.py: Adds 6 unit tests covering bare ARN, bedrock/ prefix, bedrock/invoke/ compound prefix, full URL construction, and regression tests for regular model IDs and invoke/ prefix stripping. No real network calls are made.

Confidence Score: 5/5

The change is narrowly scoped to the else-branch of get_bedrock_model_id, and the new ARN early-return path does not affect non-ARN models or the model_id-from-optional-params path.

The fix correctly delegates prefix stripping to the existing strip_bedrock_routing_prefix utility, which already handles compound prefixes in one pass. The early return for ARN models bypasses the old invoke/ replace, but that replace was always a no-op for ARNs. The new tests exercise every documented edge case without making real network calls.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/bedrock/base_aws_llm.py Adds routing-prefix stripping and ARN URL-encoding to get_bedrock_model_id; correctly reuses strip_bedrock_routing_prefix to handle compound prefixes, and early-returns before the now-redundant invoke/ replace for ARN models.
tests/test_litellm/llms/bedrock/test_base_aws_llm.py Six new unit tests covering the ARN handling fix; all are pure string/URL construction tests with no network calls, satisfying the mock-only test policy.

Reviews (4): Last reviewed commit: "fix(bedrock): use strip_bedrock_routing_..." | Re-trigger Greptile

Comment thread litellm/llms/bedrock/base_aws_llm.py Resolved
@codecov

codecov Bot commented May 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@krrish-berri-2

Copy link
Copy Markdown
Contributor Author

@greptile review

@krrish-berri-2

Copy link
Copy Markdown
Contributor Author

Real Bedrock API call results (reproduction + fix verification)

BEFORE fix — broken URL

URL: .../model/bedrock/arn:aws:bedrock:us-east-1:086734376398:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0/invoke-with-response-stream

HTTP 200  body: {"Output":{"__type":"com.amazon.coral.service#UnknownOperationException"},"Version":"1.0"}

Feeding HTTP body into botocore EventStreamBuffer (what litellm does):
  EXCEPTION raised: InvalidHeadersLength: Header length of 1953527156 exceeded the maximum of 131072

Root cause confirmed: Bedrock receives malformed URL (extra bedrock/ segment + unencoded ARN colons/slashes), treats it as unknown operation, returns HTTP 200 with JSON error body. LiteLLM sees HTTP 200 and forwards JSON bytes into botocore.eventstream.EventStreamBuffer, which expects binary event-stream framing → botocore internal exception (InvalidHeadersLength or ChecksumMismatch depending on botocore version).

AFTER fix — correct URL

URL: .../model/arn%3Aaws%3Abedrock%3Aus-east-1%3A086734376398%3Ainference-profile%2Fglobal.anthropic.claude-sonnet-4-5-20250929-v1%3A0/invoke-with-response-stream

HTTP 403  body: {"Message":"User: arn:aws:iam::888602223428:root is not authorized to perform: bedrock:InvokeModelWithResponseStream on resource: arn:aws:bedrock:us-east-1:086734376398:inference-profile/..."}

Fix confirmed: Correct URL → Bedrock returns HTTP 403 (cross-account IAM, expected for test creds). LiteLLM HTTP error handler catches 4xx before touching EventStreamBuffer. Real error message surfaced to caller instead of opaque botocore crash.

Note: HTTP 403 is expected — test creds belong to account 888602223428, inference profile is in 086734376398. On accounts with proper IAM the call succeeds and streaming works.

Unit test results

tests/test_litellm/llms/bedrock/test_base_aws_llm.py::TestGetBedrockModelIdArnHandling::test_arn_url_matches_expected PASSED
tests/test_litellm/llms/bedrock/test_base_aws_llm.py::TestGetBedrockModelIdArnHandling::test_arn_with_bedrock_prefix_is_stripped_and_encoded PASSED
tests/test_litellm/llms/bedrock/test_base_aws_llm.py::TestGetBedrockModelIdArnHandling::test_bare_arn_is_encoded PASSED
tests/test_litellm/llms/bedrock/test_base_aws_llm.py::TestGetBedrockModelIdArnHandling::test_invoke_prefixed_model_unaffected PASSED
tests/test_litellm/llms/bedrock/test_base_aws_llm.py::TestGetBedrockModelIdArnHandling::test_regular_model_id_unaffected PASSED

121 passed in 1.81s

…fixes

Address greptile review: the original fix used a loop with break, so
bedrock/invoke/arn:... only stripped bedrock/ leaving invoke/arn:...
which is not an ARN → fell through to .replace('invoke/','',1) →
bare unencoded ARN → same malformed-URL bug.

strip_bedrock_routing_prefix() iterates without break, correctly
stripping bedrock/ then invoke/ in sequence. Also adds test case
for the compound-prefix scenario.
@krrish-berri-2

Copy link
Copy Markdown
Contributor Author

@greptile review

1 similar comment
@krrish-berri-2

Copy link
Copy Markdown
Contributor Author

@greptile review

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions Bot added the stale label Aug 21, 2026
@github-actions github-actions Bot closed this Aug 28, 2026
@devin-ai-integration
devin-ai-integration Bot deleted the krrishdholakia1/lit-3274-v1messages-with-bedrock-inference-profile-arns-returns branch September 10, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants