Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,10 @@ describe('AnthropicContentConverter', () => {
});

it('cleans orphaned tool_use blocks without matching tool_result', () => {
// A genuine orphan requires a subsequent message that was actually
// scanned and found lacking a matching tool_result -- not merely the
// absence of any subsequent message (see the "trailing tool_use"
// test below for that case).
const { messages } = converter.convertGeminiRequestToAnthropic({
model: 'models/test',
contents: [
Expand All @@ -1078,20 +1082,69 @@ describe('AnthropicContentConverter', () => {
{ functionCall: { id: 'orphan', name: 'tool', args: {} } },
],
},
{ role: 'user', parts: [{ text: 'never mind' }] },
],
});

expect(messages).toEqual([
{
role: 'user',
content: [
{ type: 'text', text: 'Hi', cache_control: { type: 'ephemeral' } },
],
content: [{ type: 'text', text: 'Hi' }],
},
{
role: 'assistant',
content: [{ type: 'text', text: 'Let me help' }],
},
{
role: 'user',
content: [
{
type: 'text',
text: 'never mind',
cache_control: { type: 'ephemeral' },
},
],
},
]);
});

it('does not strip a trailing tool_use that has no subsequent message yet (unresolved, not orphaned)', () => {
// "History ends on a pending tool_use" is not evidence the call is
// orphaned -- the tool may simply not have finished executing yet,
// or this conversion may be happening for a reason other than
// sending the completed turn to Anthropic (token counting, a
// resumed/replayed session snapshot, ...). Regression test for the
// bug where this exact shape had its tool_use silently deleted.
const { messages } = converter.convertGeminiRequestToAnthropic({
model: 'models/test',
contents: [
{ role: 'user', parts: [{ text: 'What is the weather in Paris?' }] },
{
role: 'model',
parts: [
{ text: 'Let me check the weather.' },
{
functionCall: {
id: 'toolu_pending',
name: 'get_weather',
args: { city: 'Paris' },
},
},
],
},
],
});

const lastMsg = messages[messages.length - 1];
expect(lastMsg.role).toBe('assistant');
expect(lastMsg.content).toEqual([
{ type: 'text', text: 'Let me check the weather.' },
{
type: 'tool_use',
id: 'toolu_pending',
name: 'get_weather',
input: { city: 'Paris' },
},
]);
});

Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/core/anthropicContentGenerator/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,15 @@ function mergeConsecutiveAssistantMessages(
* immediately following user message, and remove tool_result blocks that
* have no matching tool_use in the immediately preceding assistant message.
*
* A `tool_use` in the very last message (no message follows it at all) is
* never condemned as orphaned here -- "no result yet" isn't the same as
* "no result ever": the tool may simply not have finished executing yet,
* or this conversion may not be building the completed turn to send to
* Anthropic at all (token counting, a resumed/replayed session snapshot,
* a retry issued before tool execution completes, ...). Only a `tool_use`
* whose subsequent message was actually scanned and found lacking a
* matching `tool_result` is a genuine orphan.
*
* Empty messages produced by the cleanup are dropped entirely. A subsequent
* mergeConsecutiveAssistantMessages call fixes any alternation issues
* created by dropped messages.
Expand Down Expand Up @@ -1107,6 +1116,20 @@ function cleanOrphanedToolCalls(
}
if (toolUseBlocks.size === 0) continue;

// No message follows this assistant turn at all -- these tool_use
// blocks are unresolved (the tool hasn't finished executing yet, or
// this conversion isn't building the completed turn for Anthropic at
// all, e.g. a token-count pass or a mid-tool-call snapshot), not
// orphaned. Protect them from the filter below. A genuine orphan
// requires a subsequent message that was actually scanned and found
// to lack a matching tool_result -- "history ends here" is not that.
if (i === messages.length - 1) {
for (const block of toolUseBlocks.values()) {
validToolUseBlocks.add(block as object);
}
continue;
}

for (let j = i + 1; j < messages.length; j++) {
const nextMessage = messages[j];
if (
Expand Down
Loading