Skip to content

fix(aws): percent-encode the model id in Bedrock and SageMaker URLs - #826

Open
beriki770-ship-it wants to merge 1 commit into
cohere-ai:mainfrom
beriki770-ship-it:fix/aws-model-id-url-encoding
Open

beriki770-ship-it wants to merge 1 commit into
cohere-ai:mainfrom
beriki770-ship-it:fix/aws-model-id-url-encoding

Conversation

@beriki770-ship-it

@beriki770-ship-it beriki770-ship-it commented Nov 4, 2026 •

Copy link
Copy Markdown

get_url() in src/cohere/aws_client.py puts the model id into the path as-is. Bedrock also takes an ARN as the model id (provisioned throughput, application inference profiles), and the / in an ARN splits it into two path segments:

BedrockClientV2(...).embed(model="arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/a1b2c3d4e5f6", ...)

main:  /model/arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/a1b2c3d4e5f6/invoke
boto3: /model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Aapplication-inference-profile%2Fa1b2c3d4e5f6/invoke

The fix percent-encodes the id as one segment (quote(model, safe="")) for Bedrock and SageMaker. With it the URL, and the canonical URI botocore's SigV4 signer computes, match what boto3.client("bedrock-runtime").invoke_model(modelId=...) produces for a plain id, a cross-region profile id with : and an ARN. I checked that offline with fake credentials (repro script: https://gist.github.com/beriki770-ship-it/61d651c04994d799967e5306c3e08bb1). I have no AWS account, so I did not run it against live Bedrock.

Tests: 3 mocked tests in tests/test_aws_client_unit.py (ARN becomes one encoded segment; plain ids unchanged; the request hook sends and signs the encoded URL). 2 fail on main, all pass with the fix. Full pytest -n 8 . without CO_API_KEY: the remaining failures are the live API tests in test_client.py, test_client_v2.py and test_async_client.py, same set on main. mypy . clean; ruff check . shows no new findings.


Note

Medium Risk
Changes URL construction and SigV4 signing for all AWS runtime calls; low behavioral risk for simple model ids but critical for ARN-based model ids.

Overview
Fixes broken Bedrock/SageMaker requests when the model id is an ARN (provisioned throughput or application inference profiles). Unencoded : and / in the id were treated as extra path segments, so URLs and SigV4 canonical paths did not match what boto3 sends.

get_url() now builds a single path segment with urllib.parse.quote(model, safe="") for both Bedrock and SageMaker; plain model names are unchanged. The request hook still strips model from the JSON body but now rewrites and signs the encoded URL.

Adds mocked unit tests for ARN encoding, unchanged plain ids, and hook URL/signing behavior.

Reviewed by Cursor Bugbot for commit f62335c. Bugbot is set up for automated code reviews on this repo. Configure here.

get_url() put the model id into the path as-is. Bedrock accepts ARNs
there (provisioned throughput, application inference profiles), and
the "/" in an ARN split it into two path segments. Encode the id as one
segment, the same URL boto3's bedrock-runtime client builds.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@beriki770-ship-it The encoded URL equals the one boto3 sends from invoke_model, which was the claim to check. Nothing here blocks a merge from my side.

# git archive f62335c (base a37a6cf), Python 3.12 venv + boto3, cohere.__file__ printed from the export
$ pytest -q tests/test_aws_client_unit.py
11 passed                                    # f62335c
2 failed, 9 passed                           # aws_client.py from a37a6cf, tests kept (the ARN and hook cases)

# boto3 bedrock-runtime invoke_model(modelId=...), URL captured in before-send, vs get_url() at f62335c
ARN with ":" and "/", "us.cohere.embed-v4:0", "cohere.embed-english-v3", "my model/x"   -> identical on all four

non-blocking: us.cohere.embed-v4:0 now goes out as us.cohere.embed-v4%3A0. boto3 sends the same, so I'd keep it, but test_plain_model_ids_are_unchanged only pins cohere.embed-english-v3. Could it also pin us.cohere.embed-v4:0 with its %3A form, so that change is on record?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants