ai icon indicating copy to clipboard operation
ai copied to clipboard

`providerOptions` on an approval-only tool message are dropped since #17577

Open robelest opened this issue 1 month ago • 5 comments

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

robelest avatar Oct 06 '26 18:10 robelest

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: providerOptions are 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 cacheControl option causes current output to differ from prior behavior.

ai-sdk-factory[bot] avatar Oct 06 '26 18:10 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 7

Bug was reproduced.

Branch: main PR: successful reproduction pull request

Details

  • AI SDK 7.0.128 reproduces the regression: both generateText and streamText execute the approved local tool but omit the approval message's providerOptions from the resulting tool message and tool-result part.
  • packages/ai/src/prompt/convert-to-language-model-prompt.ts filters local approval responses, then overwrites the empty tool message's options with the appended result message's undefined options.
  • The missing Anthropic cacheControl option 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

ai-sdk-factory[bot] avatar Oct 06 '26 18:10 ai-sdk-factory[bot]

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.

ai-sdk-factory[bot] avatar Oct 06 '26 19:10 ai-sdk-factory[bot]

Bug Reproduction - AI SDK 6

Bug was reproduced.

Branch: release-v6.0 PR: successful reproduction pull request

Details

  • On release-v6.0 with [email protected], approved local tools execute successfully, but the approval-only message's Anthropic cacheControl options disappear from the resulting tool prompt.
  • The primary failure occurs in both generateText and streamText, silently removing the intended prompt-cache breakpoint.
  • packages/ai/src/prompt/convert-to-language-model-prompt.ts filters the local approval response to empty content and then overwrites its providerOptions with 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

ai-sdk-factory[bot] avatar Oct 13 '26 04:10 ai-sdk-factory[bot]

Backport to v6.0

created: #23238 (adapted).

ai-sdk-factory[bot] avatar Oct 13 '26 04:10 ai-sdk-factory[bot]