Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/anthropic/lib/bedrock/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ def _prepare_options(input_options: FinalRequestOptions) -> FinalRequestOptions:
options.json_data.setdefault("anthropic_version", DEFAULT_VERSION)

if is_given(options.headers):
betas = build_headers(options.headers)[0].get("anthropic-beta")
beta_header = build_headers(options.headers)[0].get("anthropic-beta") or ""
betas = [beta.strip() for beta in beta_header.split(",") if beta.strip()]
if betas:
options.json_data.setdefault("anthropic_beta", betas.split(","))
options.json_data.setdefault("anthropic_beta", betas)

if options.url in {"/v1/complete", "/v1/messages", "/v1/messages?beta=true"} and options.method == "post":
if not is_dict(options.json_data):
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,22 @@ def test_prepare_options_lifts_anthropic_beta_header_case_insensitively() -> Non
]


def test_prepare_options_strips_whitespace_from_anthropic_beta_header() -> None:
options = _prepare_options(
FinalRequestOptions(
method="post",
url="/v1/messages",
json_data={"model": "anthropic.claude-sonnet-4-5"},
headers={"anthropic-beta": "context-1m-2025-08-07, other-beta,"},
)
)

assert cast("dict[str, object]", options.json_data).get("anthropic_beta") == [
"context-1m-2025-08-07",
"other-beta",
]


@pytest.mark.respx()
def test_betas_param_and_extra_headers_case_variant(respx_mock: MockRouter) -> None:
# `extra_headers` overrides the header written by `betas`, on the wire and in the body
Expand Down