Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-prune-messages-caller-deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

Fix `pruneMessages` dropping tool calls referenced via provider-specific caller dependencies (e.g. Anthropic `code_execution` `caller.toolId`), causing "source tool not found" API errors.
109 changes: 109 additions & 0 deletions packages/ai/src/generate-text/prune-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,5 +716,114 @@ describe('pruneMessages', () => {
`);
});
});

describe('caller tool id dependencies', () => {
it('should keep referenced caller tool calls when pruning', () => {
const messages: ModelMessage[] = [
{
role: 'user',
content: [{ type: 'text', text: 'Use code execution.' }],
},
// server_tool_use (provider-executed)
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: 'srvtoolu_ABC',
toolName: 'code_execution',
input: {},
providerExecuted: true,
},
],
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: 'srvtoolu_ABC',
toolName: 'code_execution',
output: { type: 'text', value: 'executed' },
},
],
},
// programmatic tool_use referencing the server_tool_use via caller
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: 'toolu_XYZ',
toolName: 'lookup',
input: { ticker: 'AAPL' },
providerOptions: {
anthropic: {
caller: {
type: 'code_execution_20250825',
toolId: 'srvtoolu_ABC',
},
},
},
},
],
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: 'toolu_XYZ',
toolName: 'lookup',
output: { type: 'text', value: '185.42' },
},
],
},
// final assistant text
{
role: 'assistant',
content: [{ type: 'text', text: 'AAPL is $185.42.' }],
},
// follow-up turn
{
role: 'user',
content: [{ type: 'text', text: 'Now check MSFT.' }],
},
];

const result = pruneMessages({
messages,
toolCalls: 'before-last-5-messages',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
toolCalls: 'before-last-5-messages',
toolCalls: 'before-last-3-messages',

Test for "caller tool id dependencies" uses a window size (before-last-5-messages) that already includes the referenced caller tool's result in the initial scan, so the transitive closure code is never exercised and the test passes even without the new feature code.

Fix on Vercel

emptyMessages: 'remove',
});

// srvtoolu_ABC must be kept because toolu_XYZ references it via caller
const allToolCallIds = result.flatMap(m =>
typeof m.content === 'string'
? []
: m.content
.filter(
(p): p is { type: 'tool-call'; toolCallId: string } =>
p.type === 'tool-call',
)
.map(p => p.toolCallId),
);
const allToolResultIds = result.flatMap(m =>
typeof m.content === 'string'
? []
: m.content
.filter(
(p): p is { type: 'tool-result'; toolCallId: string } =>
p.type === 'tool-result',
)
.map(p => p.toolCallId),
);

expect(allToolCallIds).toContain('srvtoolu_ABC');
expect(allToolCallIds).toContain('toolu_XYZ');
expect(allToolResultIds).toContain('srvtoolu_ABC');
expect(allToolResultIds).toContain('toolu_XYZ');
});
});
});
});
29 changes: 29 additions & 0 deletions packages/ai/src/generate-text/prune-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ export function pruneMessages({
}
}
}

// Transitively keep tool calls referenced via provider-specific
// caller dependencies (e.g. Anthropic's code_execution caller.toolId).
let previousSize = 0;
while (keptToolCallIds.size !== previousSize) {
previousSize = keptToolCallIds.size;
for (const message of messages) {
if (
message.role === 'assistant' &&
typeof message.content !== 'string'
) {
for (const part of message.content) {
if (
part.type === 'tool-call' &&
keptToolCallIds.has(part.toolCallId)
) {
const callerToolId = (
part.providerOptions?.anthropic as
| { caller?: { toolId?: string } }
| undefined
)?.caller?.toolId;
if (callerToolId) {
keptToolCallIds.add(callerToolId);
}
}
}
}
}
}
}

messages = messages.map((message, messageIndex) => {
Expand Down
Loading