`providerOptions` on an approval-only tool message are dropped since #17577
Since https://github.com/vercel/ai/pull/17577 (ai 7.0.33), providerOptions on a tool message whose only content is tool-approval-response parts are lost when the approved tool runs.
Approval responses for locally executed tools are filtered out during conversion, so that message reaches the "combine consecutive tool messages" loop in convert-to-language-model-prompt.ts with content: []. generateText / streamText then append the executed result as a separate tool message with no providerOptions. When the two are combined, the previous message's options can only move onto its last content part, and there is none. Then lastCombinedMessage.providerOptions = message.providerOptions overwrites them with undefined.
Before #17577 the combined message kept the first message's options, so this worked on 7.0.28.
In practice, an Anthropic cacheControl breakpoint on the latest message disappears whenever a conversation continues after an approval. Nothing errors.
Repro
import { generateText, tool } from "ai";
import { MockLanguageModelV4 } from "ai/test";
import { z } from "zod";
const model = new MockLanguageModelV4({
doGenerate: async () => ({
content: [{ type: "text", text: "ok" }],
finishReason: { unified: "stop", raw: "stop" },
usage: { inputTokens: { total: 1 }, outputTokens: { total: 1 } },
warnings: [],
}),
});
await generateText({
model,
tools: {
echo: tool({
inputSchema: z.object({ t: z.string() }),
needsApproval: true,
execute: async ({ t }) => t,
}),
},
messages: [
{ role: "user", content: "go" },
{
role: "assistant",
content: [
{ type: "tool-call", toolCallId: "c1", toolName: "echo", input: { t: "x" } },
{ type: "tool-approval-request", approvalId: "a1", toolCallId: "c1" },
],
},
{
role: "tool",
content: [{ type: "tool-approval-response", approvalId: "a1", approved: true }],
providerOptions: { anthropic: { cacheControl: { type: "ephemeral" } } },
},
],
});
console.log(JSON.stringify(model.doGenerateCalls[0].prompt.at(-1)));
7.0.28 {"role":"tool","content":[{"type":"tool-result",...}],"providerOptions":{"anthropic":{"cacheControl":{"type":"ephemeral"}}}}
7.0.119 {"role":"tool","content":[{"type":"tool-result",...}]}
Possible fix
In the combine loop, when the previous combined message has no content part to carry its options, keep them on the message instead of overwriting:
lastCombinedMessage.content.push(...message.content);
lastCombinedMessage.providerOptions =
lastContentPart == null
? mergeObjects(lastCombinedMessage.providerOptions, message.providerOptions)
: message.providerOptions;
Related: https://github.com/vercel/ai/issues/17507
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete regression in existing AI SDK behavior with reproducible incorrect output, so it is a bug despite also proposing a fix.
Evidence
- The report identifies a regression after PR #17577:
providerOptionsare dropped from approval-only tool messages when the approved tool executes. - A concrete reproduction compares working behavior in 7.0.28 with incorrect behavior in 7.0.119.
- The missing Anthropic
cacheControloption causes current output to differ from prior behavior.
Bug Reproduction - AI SDK 7
Bug was reproduced.
Branch: main
PR: successful reproduction pull request
Details
- AI SDK
7.0.128reproduces the regression: bothgenerateTextandstreamTextexecute the approved local tool but omit the approval message'sproviderOptionsfrom the resulting tool message and tool-result part. packages/ai/src/prompt/convert-to-language-model-prompt.tsfilters local approval responses, then overwrites the empty tool message's options with the appended result message's undefined options.- The missing Anthropic
cacheControloption silently removes the intended prompt-cache breakpoint from the latest tool result. - Added
examples/ai-functions/src/reproduction/issue-22173-approval-provider-options.ts; it expects the options to survive at either message or part level and exits 1 after observing them dropped by both APIs.
Relevant Documentation
- https://ai-sdk.dev/docs/foundations/prompts
- https://platform.claude.com/docs/en/build-with-claude/prompt-caching
- https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-runner
Bugfix - AI SDK 7
Created bugfix and opened PR: #22178
Root Cause
Approval responses were filtered into an empty tool message, and the consecutive-tool-message combiner then replaced that message's providerOptions with the appended result message's undefined options. The original reproduction confirmed this affected both generateText and streamText.
Summary
The tool-message combiner now merges providerOptions when the preceding tool message has no content, retaining approval-message options on the resulting tool message. Reproduction-only artifacts were removed and a patch changeset was added.
Testing
Added a regression test verifying that providerOptions survive combining an empty approval-response tool message with a subsequent tool-result message.
Bug Reproduction - AI SDK 6
Bug was reproduced.
Branch: release-v6.0
PR: successful reproduction pull request
Details
- On
release-v6.0with[email protected], approved local tools execute successfully, but the approval-only message's AnthropiccacheControloptions disappear from the resulting tool prompt. - The primary failure occurs in both
generateTextandstreamText, silently removing the intended prompt-cache breakpoint. packages/ai/src/prompt/convert-to-language-model-prompt.tsfilters the local approval response to empty content and then overwrites itsproviderOptionswith the subsequent tool-result message's undefined options.- Added
examples/ai-functions/src/reproduction/issue-22173-approval-provider-options.ts; it expects the options to survive at either message or tool-result-part level and observed them missing for both APIs.
Relevant Documentation
- https://ai-sdk.dev/docs/foundations/prompts
- https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling
- https://platform.claude.com/docs/en/build-with-claude/prompt-caching
Backport to v6.0
created: #23238 (adapted).