Skip to content

Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs#7060

Open
albatrossflyon-coder wants to merge 2 commits into
microsoft:mainfrom
albatrossflyon-coder:fix/anthropic-additional-beta-flags-leak
Open

Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs#7060
albatrossflyon-coder wants to merge 2 commits into
microsoft:mainfrom
albatrossflyon-coder:fix/anthropic-additional-beta-flags-leak

Conversation

@albatrossflyon-coder

Copy link
Copy Markdown

Summary

Fixes #5764. _prepare_options in agent_framework_anthropic/_chat_client.py copies every key from the caller-supplied options dict into run_options, excluding only instructions and response_format. A per-run additional_beta_flags value is correctly folded into the final betas set by _prepare_betas, but the raw additional_beta_flags key itself was never excluded from run_options, so it survived and was forwarded straight through to AsyncMessages.create(**run_options):

TypeError: AsyncMessages.create() got an unexpected keyword argument 'additional_beta_flags'

Fix

Add additional_beta_flags to the exclusion set alongside the other framework-level keys already handled separately (instructions, response_format).

Note on prior attempt

#5772 attempted the same fix but was closed after CI failures on that branch (per the maintainer comments, it wasn't closed on the merits of the approach — the contributor just never rebased/reopened). This PR re-implements the same direction cleanly against current main with a regression test.

Test plan

  • Added test_prepare_options_consumes_additional_beta_flags, which fails on main (the key is forwarded and would raise TypeError at the real API boundary) and passes with the fix
  • pytest packages/anthropic/tests/test_anthropic_client.py -k "additional_beta_flags or prepare_options" — 24 passed
  • pytest packages/anthropic/tests/ (full package) — passes, no new failures

…est 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 microsoft#5764
Copilot AI review requested due to automatic review settings July 10, 2026 18:58
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an Anthropic client integration bug where per-run additional_beta_flags could leak into the raw request kwargs passed to AsyncMessages.create(...), causing a TypeError. The change keeps additional_beta_flags framework-scoped (consumed into betas) rather than forwarding it as an unexpected parameter.

Changes:

  • Exclude additional_beta_flags from the run_options kwargs forwarded to the Anthropic SDK.
  • Add a regression test ensuring additional_beta_flags is consumed into betas and not forwarded raw.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/anthropic/agent_framework_anthropic/_chat_client.py Filters additional_beta_flags out of forwarded request kwargs when building run_options.
python/packages/anthropic/tests/test_anthropic_client.py Adds a regression test asserting additional_beta_flags is not forwarded and is included in betas.

Comment on lines 580 to 585
# 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"}
}
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.
@albatrossflyon-coder

Copy link
Copy Markdown
Author

Good catch — fixed in 1860079. additional_beta_flags is now also excluded from filtered_kwargs, so it's dropped consistently whether it arrives via the options dict or as a direct kwarg. Added a regression test for the kwarg path (test_prepare_options_drops_additional_beta_flags_passed_as_kwarg).

@albatrossflyon-coder

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree, send. I suppose this is the way for the CLA because I couldn't find a button up there I let me know thank you it's a pleasure

@microsoft-github-policy-service

Copy link
Copy Markdown

@albatrossflyon-coder the command you issued was incorrect. Please try again.

Examples are:

@microsoft-github-policy-service agree

and

@microsoft-github-policy-service agree company="your company"

@albatrossflyon-coder

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Anthropic: per-run additional_beta_flags leaks into request kwargs

4 participants