Python: fix per-run additional_beta_flags leaking into Anthropic request kwargs#7060
Conversation
…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
There was a problem hiding this comment.
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_flagsfrom therun_optionskwargs forwarded to the Anthropic SDK. - Add a regression test ensuring
additional_beta_flagsis consumed intobetasand 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. |
| # 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.
|
Good catch — fixed in 1860079. |
|
@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 |
|
@albatrossflyon-coder the command you issued was incorrect. Please try again. Examples are: and |
|
@microsoft-github-policy-service agree |
Summary
Fixes #5764.
_prepare_optionsinagent_framework_anthropic/_chat_client.pycopies every key from the caller-supplied options dict intorun_options, excluding onlyinstructionsandresponse_format. A per-runadditional_beta_flagsvalue is correctly folded into the finalbetasset by_prepare_betas, but the rawadditional_beta_flagskey itself was never excluded fromrun_options, so it survived and was forwarded straight through toAsyncMessages.create(**run_options):Fix
Add
additional_beta_flagsto 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
mainwith a regression test.Test plan
test_prepare_options_consumes_additional_beta_flags, which fails onmain(the key is forwarded and would raiseTypeErrorat the real API boundary) and passes with the fixpytest packages/anthropic/tests/test_anthropic_client.py -k "additional_beta_flags or prepare_options"— 24 passedpytest packages/anthropic/tests/(full package) — passes, no new failures