fix(agent): switch to auto tool choice when the provider downgrades a forced one

Merged
NA
2mo ago
+219-22

@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 logs AI SDK Warning ... The feature "toolChoice" is not supported on every turn of every step.
  • The tool-calls-only rule never reaches the instructions.
  • A turn the model answers in prose throws ToolChoiceViolationError inside generateText (ai 7.0.88+), which the loop reports as MODEL_PROVIDER_FAILED.

What changes:

  • noticeToolChoiceDowngrade reads step.warnings after each turn. A toolChoice warning switches the step to auto and remembers the model in FREE_TOOL_CHOICE_MODELS, as a refusal does.
  • prepareTurn returns instructions when the mode changed inside a generate call, so the remaining turns carry the rule. The Anthropic cache breakpoint rides along through hints.instructions.
  • freeToolChoiceRetry also takes a forced-choice turn that came back as a finished reply without a tool call: matched by error name AI_ToolChoiceViolationError (older 7.x releases export no class) and finishReason === 'stop' (a length or content-filter stop 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: scriptedResult and LoopResponder answers can carry provider warnings.
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), agent anthropic('claude-sonnet-5-5') from @ai-sdk/anthropic 4.0.71, ai 7.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 published e2e 0.15.1 (no src/agent changes since that tag); the branch is this build's packages/e2e/dist in its place.
mainthis branch
AI SDK Warning ... toolChoice lines7, one per model call1, the first turn
loop note in the reportnone[loop] the provider sent auto in place of a forced tool choice on turn 1: continuing with auto
result2 passed, 7 model calls2 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.ts fail against main's tool-loop.ts; the fourth guards against acting on unrelated warnings.