Skip to content

Commit

Permalink
fix(bedrock): correct URL encoding for model params (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritalous authored Nov 27, 2024
1 parent b9ffefe commit be4e73a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/anthropic/lib/bedrock/_client.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit be4e73a

Please sign in to comment.