From 74d05a986d600b159cb67ba17a7e8c0b26d773e0 Mon Sep 17 00:00:00 2001 From: Sebastian Turner Date: Sat, 17 Oct 2026 18:09:16 -0700 Subject: [PATCH] fix(bedrock): trim beta names when copying anthropic-beta into the body Signed-off-by: Sebastian Turner --- src/anthropic/lib/bedrock/_client.py | 5 +++-- tests/lib/test_bedrock.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/anthropic/lib/bedrock/_client.py b/src/anthropic/lib/bedrock/_client.py index f998d0881..74eccdb86 100644 --- a/src/anthropic/lib/bedrock/_client.py +++ b/src/anthropic/lib/bedrock/_client.py @@ -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): diff --git a/tests/lib/test_bedrock.py b/tests/lib/test_bedrock.py index bc97af03f..08e75d3c8 100644 --- a/tests/lib/test_bedrock.py +++ b/tests/lib/test_bedrock.py @@ -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