diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index 70d1561a..89c601d9 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import urllib.parse from typing import Any, Union, Mapping, TypeVar from typing_extensions import Self, override @@ -47,6 +48,7 @@ def _prepare_options(input_options: FinalRequestOptions) -> FinalRequestOptions: raise RuntimeError("Expected dictionary json_data for post /completions endpoint") model = options.json_data.pop("model", None) + model = urllib.parse.quote(str(model), safe=":") stream = options.json_data.pop("stream", False) if stream: options.url = f"/model/{model}/invoke-with-response-stream" diff --git a/tests/lib/test_bedrock.py b/tests/lib/test_bedrock.py index c3af6875..bb29f106 100644 --- a/tests/lib/test_bedrock.py +++ b/tests/lib/test_bedrock.py @@ -91,3 +91,36 @@ async def test_messages_retries_async(respx_mock: MockRouter) -> None: calls[1].request.url == "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-sonnet-20240229-v1:0/invoke" ) + +@pytest.mark.respx() +def test_application_inference_profile(respx_mock: MockRouter) -> None: + respx_mock.post(re.compile(r"https://bedrock-runtime\.us-east-1\.amazonaws\.com/model/.*/invoke")).mock( + side_effect=[ + httpx.Response(500, json={"error": "server error"}, headers={"retry-after-ms": "10"}), + httpx.Response(200, json={"foo": "bar"}), + ] + ) + + sync_client.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Say hello there!", + } + ], + model="arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/jf2sje1c0jnb", + ) + + calls = cast("list[MockRequestCall]", respx_mock.calls) + + assert len(calls) == 2 + + assert ( + calls[0].request.url + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn:aws:bedrock:us-east-1:123456789012:application-inference-profile%2Fjf2sje1c0jnb/invoke" + ) + assert ( + calls[1].request.url + == "https://bedrock-runtime.us-east-1.amazonaws.com/model/arn:aws:bedrock:us-east-1:123456789012:application-inference-profile%2Fjf2sje1c0jnb/invoke" + ) \ No newline at end of file