diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts index 0e0b9b243..d9ae2a77b 100644 --- a/core/llm/llms/Gemini.ts +++ b/core/llm/llms/Gemini.ts @@ -266,18 +266,35 @@ class Gemini extends BaseLLM { ? [{ text: msg.content }] : msg.content.map(this.continuePartToGeminiPart), }; - if (msg.toolCalls) { - msg.toolCalls.forEach((toolCall) => { + + if (msg.toolCalls && msg.toolCalls.length) { + msg.toolCalls.forEach((toolCall, index) => { if (toolCall.function?.name) { + const signatureForCall = (toolCall as any)?.extra_content + ?.google?.thought_signature; + + let thoughtSignature: string | undefined; + if (index === 0) { + if (typeof signatureForCall === "string") { + thoughtSignature = signatureForCall; + } else { + // Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures + // for histories that were not generated by Gemini or are missing signatures. + thoughtSignature = "skip_thought_signature_validator"; + } + } + assistantMsg.parts.push({ functionCall: { name: toolCall.function.name, args: safeParseToolCallArgs(toolCall), }, + ...(thoughtSignature && { thoughtSignature }), }); } }); } + return assistantMsg; } return { @@ -370,6 +387,7 @@ class Gemini extends BaseLLM { if ("text" in part) { textParts.push({ type: "text", text: part.text }); } else if ("functionCall" in part) { + const thoughtSignature = (part as any)?.thoughtSignature; toolCalls.push({ type: "function", id: part.functionCall.id ?? uuidv4(), @@ -380,6 +398,13 @@ class Gemini extends BaseLLM { ? part.functionCall.args : JSON.stringify(part.functionCall.args), }, + ...(thoughtSignature && { + extra_content: { + google: { + thought_signature: thoughtSignature, + }, + }, + }), }); } else { // Note: function responses shouldn't be streamed, images not supported diff --git a/packages/openai-adapters/src/apis/Gemini.ts b/packages/openai-adapters/src/apis/Gemini.ts index b0f32ad46..4f5b9837e 100644 --- a/packages/openai-adapters/src/apis/Gemini.ts +++ b/packages/openai-adapters/src/apis/Gemini.ts @@ -143,9 +143,25 @@ export class GeminiApi implements BaseLlmApi { return { role: "model" as const, - parts: msg.tool_calls.map((toolCall) => { - // Type guard for function tool calls + parts: msg.tool_calls.map((toolCall, index) => { if (toolCall.type === "function" && "function" in toolCall) { + let thoughtSignature: string | undefined; + if (index === 0) { + const rawSignature = (toolCall as any)?.extra_content?.google + ?.thought_signature; + + if ( + typeof rawSignature === "string" && + rawSignature.length > 0 + ) { + thoughtSignature = rawSignature; + } else { + // Fallback per https://ai.google.dev/gemini-api/docs/thought-signatures + // for histories that were not generated by Gemini or are missing signatures. + thoughtSignature = "skip_thought_signature_validator"; + } + } + return { functionCall: { id: includeToolCallIds ? toolCall.id : undefined, @@ -155,12 +171,12 @@ export class GeminiApi implements BaseLlmApi { `Call: ${toolCall.function.name} ${toolCall.id}`, ), }, + ...(thoughtSignature && { thoughtSignature }), }; - } else { - throw new Error( - `Unsupported tool call type in Gemini: ${toolCall.type}`, - ); } + throw new Error( + `Unsupported tool call type in Gemini: ${toolCall.type}`, + ); }), }; } @@ -328,11 +344,27 @@ export class GeminiApi implements BaseLlmApi { if (contentParts) { for (const part of contentParts) { if ("text" in part) { + const thoughtSignature = (part as any)?.thoughtSignature; + if (thoughtSignature) { + yield chatChunkFromDelta({ + model, + delta: { + role: "assistant", + extra_content: { + google: { + thought_signature: thoughtSignature, + }, + }, + } as any, + }); + } + yield chatChunk({ content: part.text, model, }); } else if ("functionCall" in part) { + const thoughtSignature = (part as any)?.thoughtSignature; yield chatChunkFromDelta({ model, delta: { @@ -345,6 +377,13 @@ export class GeminiApi implements BaseLlmApi { name: part.functionCall.name, arguments: JSON.stringify(part.functionCall.args), }, + ...(thoughtSignature && { + extra_content: { + google: { + thought_signature: thoughtSignature, + }, + }, + }), }, ], },