fix(agent): switch to auto tool choice when the provider downgrades a forced one
Merged
NA
2mo ago+219+219+012345678901234567890123456789-22-22-01234567890123456789
@ai-sdk/anthropic does not send a forced tool choice to Claude models that reject forced tool use (Claude Sonnet 5.5, for one). It rewrites toolChoice: 'required' (and { type: 'tool' }) to auto on the client and reports that only as a call warning, { type: 'unsupported', feature: 'toolChoice' }. The request succeeds, so the HTTP 400 path in freeToolChoiceRetry never runs.
On main, with that provider:
- The loop keeps asking for
required, and the SDK logsAI SDK Warning ... The feature "toolChoice" is not supportedon every turn of every step. - The tool-calls-only rule never reaches the instructions.
- A turn the model answers in prose throws
ToolChoiceViolationErrorinsidegenerateText(ai 7.0.88+), which the loop reports asMODEL_PROVIDER_FAILED.
What changes:
noticeToolChoiceDowngradereadsstep.warningsafter each turn. AtoolChoicewarning switches the step toautoand remembers the model inFREE_TOOL_CHOICE_MODELS, as a refusal does.prepareTurnreturnsinstructionswhen the mode changed inside a generate call, so the remaining turns carry the rule. The Anthropic cache breakpoint rides along throughhints.instructions.freeToolChoiceRetryalso takes a forced-choice turn that came back as a finished reply without a tool call: matched by error nameAI_ToolChoiceViolationError(older 7.x releases export no class) andfinishReason === 'stop'(alengthorcontent-filterstop says nothing about the provider). That turn was answered and billed, so it is counted as a model call and a turn before the retry.- Test fakes:
scriptedResultandLoopResponderanswers can carry providerwarnings.
flowchart LR
T[turn asks required] --> P{provider}
P -- HTTP 400 tool_choice --> R[resend with auto + rule]
P -- sends auto, warns --> W[step.warnings: toolChoice] --> A[rest of step and later steps: auto + rule]
P -- sends auto, model writes prose --> V[AI_ToolChoiceViolationError, finishReason stop] --> R
Verified
Ran it locally: yes
- A NativeScript app's suite on
@e2e-dev/mobile(iPhone Duo simulator), agentanthropic('claude-sonnet-5-5')from@ai-sdk/anthropic4.0.71,ai7.0.126:e2e run tests/bookmarks.e2e.ts tests/search.e2e.ts --no-cache, with the AI SDK's warning log left on. Main is the publishede2e0.15.1 (nosrc/agentchanges since that tag); the branch is this build'spackages/e2e/distin its place.
| main | this branch | |
|---|---|---|
AI SDK Warning ... toolChoice lines | 7, one per model call | 1, the first turn |
| loop note in the report | none | [loop] the provider sent auto in place of a forced tool choice on turn 1: continuing with auto |
| result | 2 passed, 7 model calls | 2 passed, 7 model calls |
- The prose-reply path did not come up in live runs; it is covered by
retries with auto when a downgraded turn answers in prose, which scripts a provider that downgrades and answers in prose. - Three of the four new tests in
tool-loop-tool-choice.test.tsfail against main'stool-loop.ts; the fourth guards against acting on unrelated warnings.