From 091be73991e125488d5b93b3c5b25a699eb3a28a Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 10 Jul 2026 13:57:57 -0500 Subject: [PATCH 1/2] Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs _prepare_options copied every key from the caller-supplied options dict into run_options except "instructions" and "response_format". A per-run additional_beta_flags value is correctly folded into the betas set by _prepare_betas, but the raw key was never excluded, so it survived into run_options and was forwarded straight through to AsyncMessages.create(), which rejects it with TypeError: got an unexpected keyword argument 'additional_beta_flags'. Add it to the exclusion set alongside the other framework-level keys. Fixes #5764 --- .../agent_framework_anthropic/_chat_client.py | 4 +++- .../anthropic/tests/test_anthropic_client.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py index 3ec953b5bb1..ca4328ff524 100644 --- a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py +++ b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py @@ -579,7 +579,9 @@ def _prepare_options( """ # Start with a copy of options, excluding keys we handle separately run_options: dict[str, Any] = { - k: v for k, v in options.items() if v is not None and k not in {"instructions", "response_format"} + k: v + for k, v in options.items() + if v is not None and k not in {"instructions", "response_format", "additional_beta_flags"} } # Framework-level options handled elsewhere; do not forward as raw Anthropic request kwargs. run_options.pop("allow_multiple_tool_calls", None) diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index 0992c0ed91f..4c9117ff421 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -1270,6 +1270,27 @@ async def test_prepare_options_excludes_stream_option( assert "stream" not in run_options +async def test_prepare_options_consumes_additional_beta_flags( + mock_anthropic_client: MagicMock, +) -> None: + """Per-run additional_beta_flags must be folded into betas, not forwarded raw. + + Regression test for https://github.com/microsoft/agent-framework/issues/5764: + the key survived into run_options and was passed straight through to + ``AsyncMessages.create()``, which rejects it with + ``TypeError: got an unexpected keyword argument 'additional_beta_flags'``. + """ + client = create_test_anthropic_client(mock_anthropic_client) + + messages = [Message(role="user", contents=["Hello"])] + chat_options: dict[str, Any] = {"additional_beta_flags": ["extended-cache-ttl-2025-04-11"]} + + run_options = client._prepare_options(messages, chat_options) + + assert "additional_beta_flags" not in run_options + assert "extended-cache-ttl-2025-04-11" in run_options["betas"] + + async def test_prepare_options_filters_internal_kwargs( mock_anthropic_client: MagicMock, ) -> None: From 186007984ae08a0d9de028fd3f8c92545a1161bb Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Fri, 10 Jul 2026 15:24:29 -0500 Subject: [PATCH 2/2] Exclude additional_beta_flags from filtered_kwargs too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copilot's review on the original fix pointed out the exclusion only covered the options-dict copy, not kwargs passed directly to _prepare_options — so additional_beta_flags supplied as a raw kwarg would still leak through and reproduce the same TypeError. Add the same exclusion to filtered_kwargs for consistency, with a regression test covering the kwarg path. --- .../agent_framework_anthropic/_chat_client.py | 4 +++- .../anthropic/tests/test_anthropic_client.py | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py index ca4328ff524..367362c7032 100644 --- a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py +++ b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py @@ -594,7 +594,9 @@ def _prepare_options( # This includes underscore-prefixed internal objects (like _function_middleware_pipeline) # and framework kwargs like 'thread' and 'middleware'. filtered_kwargs = { - k: v for k, v in kwargs.items() if not k.startswith("_") and k not in {"thread", "middleware"} + k: v + for k, v in kwargs.items() + if not k.startswith("_") and k not in {"thread", "middleware", "additional_beta_flags"} } _apply_option_translations(filtered_kwargs) run_options.update(filtered_kwargs) diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index 4c9117ff421..8876823cc61 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -1291,6 +1291,27 @@ async def test_prepare_options_consumes_additional_beta_flags( assert "extended-cache-ttl-2025-04-11" in run_options["betas"] +async def test_prepare_options_drops_additional_beta_flags_passed_as_kwarg( + mock_anthropic_client: MagicMock, +) -> None: + """additional_beta_flags must also be excluded when passed as a raw kwarg, + not just via the options dict. + + Flagged in code review on the fix for #5764: the initial fix only excluded + the key from the options-dict copy, but filtered_kwargs (built from + **kwargs at the call site) had no equivalent exclusion, so + ``_prepare_options(messages, {}, additional_beta_flags=[...])`` would still + forward the raw key and reproduce the same TypeError. + """ + client = create_test_anthropic_client(mock_anthropic_client) + + messages = [Message(role="user", contents=["Hello"])] + + run_options = client._prepare_options(messages, {}, additional_beta_flags=["extended-cache-ttl-2025-04-11"]) + + assert "additional_beta_flags" not in run_options + + async def test_prepare_options_filters_internal_kwargs( mock_anthropic_client: MagicMock, ) -> None: