Skip to content

Commit b018630

Browse files
PierrunoYTclaude
andcommitted
fix(litellm): don't truncate tool call ids that carry a thought signature
LiteLLM encodes a Gemini thought signature into the tool call id after __thought__. sanitizeOpenAiCallId cut such ids to 64 characters and appended a hash, so Gemini received a corrupted signature (400 Base64 decoding failed). Only character-sanitize ids containing the marker. Fixes #2064 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 16425bc commit b018630

4 files changed

Lines changed: 97 additions & 4 deletions

File tree

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
2+
import type OpenAI from "openai"
23

34
import { LiteLLMHandler } from "../lite-llm"
5+
import type { ApiHandlerCreateMessageMetadata } from "../../index"
46
import { ApiHandlerOptions } from "../../../shared/api"
57
import { litellmDefaultModelId, litellmDefaultModelInfo } from "@roo-code/types"
68
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
@@ -754,6 +756,47 @@ describe("LiteLLMHandler", () => {
754756
expect(assistantMessage.tool_calls[0].provider_specific_fields.thought_signature).toBe(dummySignature)
755757
})
756758

759+
it("should send tool call ids that carry a thought signature unchanged", async () => {
760+
// LiteLLM encodes the Gemini signature into the id; truncating it breaks the signature.
761+
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i_aDwY5o-0123456789abcdef"
762+
const toolCallId = `call_864944__thought__${signature}`
763+
expect(toolCallId.length).toBeGreaterThan(64)
764+
765+
const messages: Anthropic.Messages.MessageParam[] = [
766+
{ role: "user", content: "Hello" },
767+
{
768+
role: "assistant",
769+
content: [{ type: "tool_use", id: toolCallId, name: "read_file", input: { path: "test.txt" } }],
770+
},
771+
{
772+
role: "user",
773+
content: [{ type: "tool_result", tool_use_id: toolCallId, content: "file contents" }],
774+
},
775+
]
776+
mockCreate.mockReturnValue({
777+
withResponse: vi.fn().mockResolvedValue({
778+
data: asyncStreamFrom([{ choices: [{ delta: { content: "ok" } }] }]),
779+
}),
780+
})
781+
const metadata: ApiHandlerCreateMessageMetadata = {
782+
taskId: "task-1",
783+
tools: [
784+
{
785+
type: "function",
786+
function: { name: "read_file", description: "Read a file", parameters: {} },
787+
},
788+
],
789+
}
790+
791+
await collectStream(handler.createMessage("You are a helpful assistant", messages, metadata))
792+
793+
const sent: OpenAI.Chat.ChatCompletionMessageParam[] = mockCreate.mock.calls[0][0].messages
794+
const assistantMessage = sent.find((msg) => msg.role === "assistant" && msg.tool_calls?.length)
795+
const toolMessage = sent.find((msg) => msg.role === "tool")
796+
expect(assistantMessage?.role === "assistant" && assistantMessage.tool_calls?.[0].id).toBe(toolCallId)
797+
expect(toolMessage?.role === "tool" && toolMessage.tool_call_id).toBe(toolCallId)
798+
})
799+
757800
it("should not inject thought signatures for non-Gemini models", async () => {
758801
const optionsWithGPT4: ApiHandlerOptions = {
759802
...mockOptions,

‎src/api/providers/lite-llm.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
1818
import { convertToOpenAiMessages } from "../transform/openai-format"
1919
import { convertToR1Format } from "../transform/r1-format"
2020
import { GEMINI_THOUGHT_SIGNATURE_BYPASS } from "../transform/gemini-format"
21-
import { sanitizeOpenAiCallId } from "../../utils/tool-id"
21+
import { sanitizeLiteLlmCallId } from "../../utils/tool-id"
2222

2323
import type {
2424
SingleCompletionHandler,
@@ -158,11 +158,11 @@ export class LiteLLMHandler extends RouterProvider implements SingleCompletionHa
158158
// never gets inserted mid-turn and causes the model to drop prior reasoning_content.
159159
const openAiMessages = info.preserveReasoning
160160
? convertToR1Format(messages, {
161-
normalizeToolCallId: sanitizeOpenAiCallId,
161+
normalizeToolCallId: sanitizeLiteLlmCallId,
162162
mergeToolResultText: true,
163163
})
164164
: convertToOpenAiMessages(messages, {
165-
normalizeToolCallId: sanitizeOpenAiCallId,
165+
normalizeToolCallId: sanitizeLiteLlmCallId,
166166
})
167167

168168
// Prepare messages with cache control if enabled and supported

‎src/utils/__tests__/tool-id.spec.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { sanitizeToolUseId, truncateOpenAiCallId, sanitizeOpenAiCallId, OPENAI_CALL_ID_MAX_LENGTH } from "../tool-id"
1+
import {
2+
sanitizeToolUseId,
3+
truncateOpenAiCallId,
4+
sanitizeOpenAiCallId,
5+
sanitizeLiteLlmCallId,
6+
OPENAI_CALL_ID_MAX_LENGTH,
7+
} from "../tool-id"
28

39
describe("sanitizeToolUseId", () => {
410
describe("valid IDs pass through unchanged", () => {
@@ -184,3 +190,29 @@ describe("OPENAI_CALL_ID_MAX_LENGTH constant", () => {
184190
expect(OPENAI_CALL_ID_MAX_LENGTH).toBe(64)
185191
})
186192
})
193+
194+
describe("sanitizeLiteLlmCallId", () => {
195+
const signature = "AY89a18ErUKBAfkTYnDbzq6mGnRG5QS8VFogncpDzYVXzC0i_aDwY5o-0123456789abcdef"
196+
197+
it("keeps an id that carries a thought signature intact, however long", () => {
198+
const id = `call_864944__thought__${signature}`
199+
200+
expect(id.length).toBeGreaterThan(OPENAI_CALL_ID_MAX_LENGTH)
201+
expect(sanitizeLiteLlmCallId(id)).toBe(id)
202+
})
203+
204+
it("still replaces invalid characters in an id that carries a thought signature", () => {
205+
expect(sanitizeLiteLlmCallId("call_1__thought__ab+c/d=")).toBe("call_1__thought__ab_c_d_")
206+
})
207+
208+
it("truncates other long ids like sanitizeOpenAiCallId", () => {
209+
const id = "call_" + "x".repeat(100)
210+
211+
expect(sanitizeLiteLlmCallId(id)).toBe(sanitizeOpenAiCallId(id))
212+
expect(sanitizeLiteLlmCallId(id).length).toBe(OPENAI_CALL_ID_MAX_LENGTH)
213+
})
214+
215+
it("leaves short ids unchanged", () => {
216+
expect(sanitizeLiteLlmCallId("toolu_123")).toBe("toolu_123")
217+
})
218+
})

‎src/utils/tool-id.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,21 @@ export function sanitizeOpenAiCallId(id: string, maxLength: number = OPENAI_CALL
5454
const sanitized = sanitizeToolUseId(id)
5555
return truncateOpenAiCallId(sanitized, maxLength)
5656
}
57+
58+
/**
59+
* LiteLLM carries a Gemini thought signature inside the tool call id, as
60+
* `<call id>__thought__<signature>`. Truncating such an id corrupts the signature.
61+
*/
62+
export const LITELLM_THOUGHT_SIGNATURE_MARKER = "__thought__"
63+
64+
/**
65+
* Normalize a tool call id for LiteLLM. Ids that carry a thought signature are only
66+
* character-sanitized, because the signature must reach Gemini intact; every other id
67+
* gets the usual OpenAI 64-character treatment.
68+
*
69+
* @param id - The original call_id
70+
* @returns The id to send to LiteLLM
71+
*/
72+
export function sanitizeLiteLlmCallId(id: string): string {
73+
return id.includes(LITELLM_THOUGHT_SIGNATURE_MARKER) ? sanitizeToolUseId(id) : sanitizeOpenAiCallId(id)
74+
}

0 commit comments

Comments
 (0)