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/strong-hoops-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents-plugin-google': minor
---

preserve thought signatures for Gemini 3+ function calling
30 changes: 28 additions & 2 deletions agents/src/llm/chat_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,35 @@ export class FunctionCall {

createdAt: number;

/**
* Opaque signature for Gemini thinking mode.
* When using Gemini 3+ models with thinking enabled, this signature must be
* preserved and returned with function responses to maintain thought context.
*/
thoughtSignature?: string;

constructor(params: {
callId: string;
name: string;
args: string;
id?: string;
createdAt?: number;
thoughtSignature?: string;
}) {
const { callId, name, args, id = shortuuid('item_'), createdAt = Date.now() } = params;
const {
callId,
name,
args,
id = shortuuid('item_'),
createdAt = Date.now(),
thoughtSignature,
} = params;
this.id = id;
this.callId = callId;
this.args = args;
this.name = name;
this.createdAt = createdAt;
this.thoughtSignature = thoughtSignature;
}

static create(params: {
Expand All @@ -210,6 +226,7 @@ export class FunctionCall {
args: string;
id?: string;
createdAt?: number;
thoughtSignature?: string;
}) {
return new FunctionCall(params);
}
Expand All @@ -224,6 +241,10 @@ export class FunctionCall {
args: this.args,
};

if (this.thoughtSignature) {
result.thoughtSignature = this.thoughtSignature;
}

if (!excludeTimestamp) {
result.createdAt = this.createdAt;
}
Expand Down Expand Up @@ -602,7 +623,12 @@ export class ChatContext {
return false;
}
} else if (a.type === 'function_call' && b.type === 'function_call') {
if (a.name !== b.name || a.callId !== b.callId || a.args !== b.args) {
if (
a.name !== b.name ||
a.callId !== b.callId ||
a.args !== b.args ||
a.thoughtSignature !== b.thoughtSignature
) {
return false;
}
} else if (a.type === 'function_call_output' && b.type === 'function_call_output') {
Expand Down
8 changes: 6 additions & 2 deletions agents/src/llm/provider_format/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ export async function toChatCtx(
}
}
} else if (msg.type === 'function_call') {
parts.push({
const functionCallPart: Record<string, unknown> = {
functionCall: {
id: msg.callId,
name: msg.name,
args: JSON.parse(msg.args || '{}'),
},
});
};
if (msg.thoughtSignature) {
functionCallPart.thoughtSignature = msg.thoughtSignature;
}
parts.push(functionCallPart);
} else if (msg.type === 'function_call_output') {
const response = msg.isError ? { error: msg.output } : { output: msg.output };
parts.push({
Expand Down
2 changes: 2 additions & 0 deletions agents/src/voice/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ export function performLLMInference(
callId: `${data.id}/fnc_${data.generatedToolCalls.length}`,
name: tool.name,
args: tool.args,
// Preserve thought signature for Gemini 3+ thinking mode
thoughtSignature: tool.thoughtSignature,
});

data.generatedToolCalls.push(toolCall);
Expand Down
2 changes: 1 addition & 1 deletion plugins/google/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@google/genai": "^1.13.0",
"@google/genai": "^1.34.0",
"@livekit/mutex": "^1.1.1",
"@types/json-schema": "^7.0.15",
"json-schema": "^0.4.0"
Expand Down
2 changes: 2 additions & 0 deletions plugins/google/src/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ export class LLMStream extends llm.LLMStream {
callId: part.functionCall.id || shortuuid('function_call_'),
name: part.functionCall.name!,
args: JSON.stringify(part.functionCall.args!),
// Preserve thought signature for Gemini 3+ thinking mode
thoughtSignature: part.thoughtSignature,
}),
],
},
Expand Down
Loading