Backport: fix(anthropic): propagate toModelOutput providerOption to anthropic tool results#15312
Merged
Merged
Conversation
…ool results (#15284) ## Background #15185 when a user used `tool.toModelOutput()` to return some providerOptions, the problem was that Anthropic never read them from that location. metadata existed at: `part.output.providerOptions` but the Anthropic converter only checked: `part.providerOptions`, `message.providerOptions` so `cache_control` was silently omitted from the outgoing anthropic tool_result block ## Summary converter now also checks provider options on the tool-result output itself, including inner content output parts ## Manual Verification verified by running the following repro: <details> <summary>repro:</summary> ```ts import { anthropic, type AnthropicLanguageModelOptions, } from '@ai-sdk/anthropic'; import { generateText, isStepCount, tool, type ModelMessage } from 'ai'; import fs from 'node:fs'; import { z } from 'zod'; import { run } from '../../lib/run'; const cachedText = fs.readFileSync('data/error-message.txt', 'utf8'); const anthropicOptions = { cacheControl: { type: 'ephemeral' }, } satisfies AnthropicLanguageModelOptions; const providerOptions = { anthropic: anthropicOptions, }; const controlMessages = [ { role: 'assistant', content: [ { type: 'text', text: cachedText, providerOptions, }, ], }, { role: 'user', content: 'Reply with "ok".', }, ] satisfies ModelMessage[]; const cachedTextTool = tool({ description: 'Return a stable cached text payload.', inputSchema: z.object({}), execute: async () => cachedText, toModelOutput: ({ output }) => ({ type: 'text', value: output, providerOptions, }), }); function hasCacheControl(body: unknown) { return JSON.stringify(body).includes('"cache_control"'); } run(async () => { const controlCold = await generateText({ model: anthropic('claude-opus-4-1'), messages: controlMessages, include: { requestBody: true }, }); console.log('=== control cold ==='); console.log(controlCold.finalStep.providerMetadata?.anthropic?.usage); console.log('request has cache_control:', hasCacheControl(controlCold.request.body)); const controlWarm = await generateText({ model: anthropic('claude-opus-4-1'), messages: controlMessages, include: { requestBody: true }, }); console.log('=== control warm ==='); console.log(controlWarm.finalStep.providerMetadata?.anthropic?.usage); console.log('request has cache_control:', hasCacheControl(controlWarm.request.body)); const toolCold = await generateText({ model: anthropic('claude-opus-4-1'), prompt: 'Call the cachedText tool, then reply with "ok".', tools: { cachedText: cachedTextTool }, toolChoice: { type: 'tool', toolName: 'cachedText' }, stopWhen: isStepCount(2), include: { requestBody: true }, }); console.log('=== tool result cold ==='); console.log(toolCold.finalStep.providerMetadata?.anthropic?.usage); console.log('request has cache_control:', hasCacheControl(toolCold.finalStep.request.body)); const toolWarm = await generateText({ model: anthropic('claude-opus-4-1'), messages: [ { role: 'user', content: 'Call the cachedText tool, then reply with "ok".' }, ...toolCold.responseMessages, { role: 'user', content: 'Reply with "ok" again.' }, ], tools: { cachedText: cachedTextTool }, include: { requestBody: true }, }); console.log('=== tool result warm ==='); console.log(toolWarm.finalStep.providerMetadata?.anthropic?.usage); console.log('request has cache_control:', hasCacheControl(toolWarm.request.body)); }); ``` </details> Observed behavior before the fix is that the last 2 tool-results will show `request has cache_control: false` ## Checklist - [x] All commits are signed (PRs with unsigned commits cannot be merged) - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Related Issues fixes #15186
aayush-kapoor
approved these changes
May 15, 2026
Contributor
Author
|
🚀 Published in:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an automated backport of #15284 to the release-v6.0 branch. FYI @aayush-kapoor