Skip to content

Commit 45b920a

Browse files
Wen-Yong LimWen-Yong Lim
authored andcommitted
fix(venice): downgrade unsigned Gemini tool history
1 parent b01fcd8 commit 45b920a

2 files changed

Lines changed: 102 additions & 31 deletions

File tree

‎extensions/venice/index.test.ts‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe("venice provider plugin", () => {
116116
]);
117117
});
118118

119-
it("replays Gemini signatures and marks foreign history in Venice's top-level field", async () => {
119+
it("replays Gemini signatures and downgrades foreign tool history to text", async () => {
120120
const provider = await registerSingleProviderPlugin(plugin);
121121
const capturedPayloads: Record<string, unknown>[] = [];
122122
const baseStreamFn = (_model: unknown, _context: unknown, options: unknown) => {
@@ -227,17 +227,20 @@ describe("venice provider plugin", () => {
227227
} as never,
228228
{},
229229
);
230-
const crossRouteToolCall = (
231-
(capturedPayloads[1]!.messages as Array<Record<string, unknown>>)[1]!.tool_calls as Array<
232-
Record<string, unknown>
233-
>
234-
)[0];
235-
expect(crossRouteToolCall).toMatchObject({
236-
thought_signature: "skip_thought_signature_validator",
230+
const crossRouteMessages = capturedPayloads[1]!.messages as Array<Record<string, unknown>>;
231+
expect(crossRouteMessages[1]).toMatchObject({
232+
role: "assistant",
233+
content: expect.stringContaining("[Historical tool call: echo_value("),
237234
});
235+
expect(crossRouteMessages[1]).not.toHaveProperty("tool_calls");
236+
expect(crossRouteMessages[2]).toMatchObject({
237+
role: "user",
238+
content: "[Historical tool result for echo_value:\nok]",
239+
});
240+
expect(crossRouteMessages[2]).not.toHaveProperty("tool_call_id");
238241
});
239242

240-
it("marks unsigned mixed-model and parallel Gemini history for validation bypass", async () => {
243+
it("downgrades mixed signed and unsigned Gemini tool batches to text history", async () => {
241244
const provider = await registerSingleProviderPlugin(plugin);
242245
let capturedPayload: Record<string, unknown> | undefined;
243246
const baseStreamFn = (_model: unknown, _context: unknown, options: unknown) => {
@@ -252,6 +255,10 @@ describe("venice provider plugin", () => {
252255
{ id: "signed_call", type: "function", function: { name: "read" } },
253256
],
254257
},
258+
{ role: "tool", tool_call_id: "foreign_call", content: "foreign result" },
259+
{ role: "tool", tool_call_id: "legacy_call", content: "legacy result" },
260+
{ role: "tool", tool_call_id: "signed_call", content: "signed result" },
261+
{ role: "user", content: "current prompt" },
255262
],
256263
};
257264
(options as { onPayload?: (payload: Record<string, unknown>) => void })?.onPayload?.(payload);
@@ -297,16 +304,21 @@ describe("venice provider plugin", () => {
297304
{},
298305
);
299306

300-
const toolCalls = (capturedPayload!.messages as Array<Record<string, unknown>>)[0]!
301-
.tool_calls as Array<Record<string, unknown>>;
302-
expect(toolCalls.map((toolCall) => toolCall.thought_signature)).toEqual([
303-
"skip_thought_signature_validator",
304-
"skip_thought_signature_validator",
305-
"SIG-EXACT-SAME-ROUTE==",
307+
const messages = capturedPayload!.messages as Array<Record<string, unknown>>;
308+
expect(messages[0]).toMatchObject({
309+
role: "assistant",
310+
content: expect.stringContaining("[Historical tool call: web_fetch("),
311+
});
312+
expect(messages[0]).not.toHaveProperty("tool_calls");
313+
expect(messages.slice(1, 4)).toEqual([
314+
{ role: "user", content: "[Historical tool result for web_fetch:\nforeign result]" },
315+
{ role: "user", content: "[Historical tool result for read:\nlegacy result]" },
316+
{ role: "user", content: "[Historical tool result for read:\nsigned result]" },
306317
]);
318+
expect(messages[4]).toEqual({ role: "user", content: "current prompt" });
307319
});
308320

309-
it("does not add the Gemini 3 validator bypass to unsigned Gemini 2.5 history", async () => {
321+
it("leaves unsigned Gemini 2.5 history unchanged", async () => {
310322
const provider = await registerSingleProviderPlugin(plugin);
311323
let capturedPayload: Record<string, unknown> | undefined;
312324
const baseStreamFn = (_model: unknown, _context: unknown, options: unknown) => {

‎extensions/venice/stream.ts‎

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,39 @@ function isVeniceGemini3ModelId(modelId: unknown): boolean {
1717
return typeof modelId === "string" && /^gemini-3(?:[.-]|$)/.test(modelId.trim().toLowerCase());
1818
}
1919

20-
const GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP = "skip_thought_signature_validator";
20+
function stringifyHistoricalValue(value: unknown): string {
21+
if (typeof value === "string") {
22+
return value;
23+
}
24+
if (value === null || value === undefined) {
25+
return "";
26+
}
27+
try {
28+
return JSON.stringify(value);
29+
} catch {
30+
return String(value);
31+
}
32+
}
33+
34+
function describeHistoricalToolCall(toolCall: Record<string, unknown>): {
35+
id?: string;
36+
name: string;
37+
text: string;
38+
} {
39+
const fn =
40+
toolCall.function && typeof toolCall.function === "object"
41+
? (toolCall.function as Record<string, unknown>)
42+
: {};
43+
const name = typeof fn.name === "string" && fn.name.length > 0 ? fn.name : "unknown_tool";
44+
const args = stringifyHistoricalValue(fn.arguments) || "{}";
45+
return {
46+
...(typeof toolCall.id === "string" ? { id: toolCall.id } : {}),
47+
name,
48+
text: `[Historical tool call: ${name}(${args})]`,
49+
};
50+
}
2151

22-
function applyVeniceGeminiThoughtSignatures(
52+
function applyVeniceGeminiToolHistoryCompatibility(
2353
payload: Record<string, unknown>,
2454
context: Parameters<NonNullable<ProviderWrapStreamFnContext["streamFn"]>>[1],
2555
model: Parameters<NonNullable<ProviderWrapStreamFnContext["streamFn"]>>[0],
@@ -28,9 +58,7 @@ function applyVeniceGeminiThoughtSignatures(
2858
return;
2959
}
3060
const signaturesByToolCallId = new Map<string, string>();
31-
const fallbackSignature = isVeniceGemini3ModelId(model.id)
32-
? GEMINI_THOUGHT_SIGNATURE_VALIDATOR_SKIP
33-
: undefined;
61+
const requiresUnsignedCallFallback = isVeniceGemini3ModelId(model.id);
3462
for (const message of context.messages ?? []) {
3563
if (message.role !== "assistant") {
3664
continue;
@@ -54,11 +82,12 @@ function applyVeniceGeminiThoughtSignatures(
5482
}
5583
}
5684
if (
57-
(signaturesByToolCallId.size === 0 && !fallbackSignature) ||
85+
(signaturesByToolCallId.size === 0 && !requiresUnsignedCallFallback) ||
5886
!Array.isArray(payload.messages)
5987
) {
6088
return;
6189
}
90+
const downgradedToolCalls = new Map<string, string>();
6291
for (const message of payload.messages) {
6392
if (!message || typeof message !== "object") {
6493
continue;
@@ -67,6 +96,8 @@ function applyVeniceGeminiThoughtSignatures(
6796
if (record.role !== "assistant" || !Array.isArray(record.tool_calls)) {
6897
continue;
6998
}
99+
let shouldDowngradeBatch = false;
100+
const describedCalls: Array<ReturnType<typeof describeHistoricalToolCall>> = [];
70101
for (const toolCall of record.tool_calls) {
71102
if (!toolCall || typeof toolCall !== "object") {
72103
continue;
@@ -78,16 +109,44 @@ function applyVeniceGeminiThoughtSignatures(
78109
: undefined;
79110
if (signature) {
80111
toolCallRecord.thought_signature = signature;
81-
} else if (
82-
fallbackSignature &&
83-
(typeof toolCallRecord.thought_signature !== "string" ||
84-
toolCallRecord.thought_signature.length === 0)
85-
) {
86-
// Gemini 3 validates every historical function call, including calls
87-
// imported from another provider or recorded before signature capture.
88-
toolCallRecord.thought_signature = fallbackSignature;
112+
} else if (requiresUnsignedCallFallback) {
113+
shouldDowngradeBatch = true;
114+
}
115+
describedCalls.push(describeHistoricalToolCall(toolCallRecord));
116+
}
117+
if (!shouldDowngradeBatch) {
118+
continue;
119+
}
120+
for (const call of describedCalls) {
121+
if (call.id) {
122+
downgradedToolCalls.set(call.id, call.name);
89123
}
90124
}
125+
const existingContent = stringifyHistoricalValue(record.content);
126+
record.content = [existingContent, ...describedCalls.map((call) => call.text)]
127+
.filter((part) => part.length > 0)
128+
.join("\n");
129+
delete record.tool_calls;
130+
}
131+
if (downgradedToolCalls.size === 0) {
132+
return;
133+
}
134+
for (const message of payload.messages) {
135+
if (!message || typeof message !== "object") {
136+
continue;
137+
}
138+
const record = message as Record<string, unknown>;
139+
const toolCallId = typeof record.tool_call_id === "string" ? record.tool_call_id : undefined;
140+
const toolName = toolCallId ? downgradedToolCalls.get(toolCallId) : undefined;
141+
if (record.role !== "tool" || !toolName) {
142+
continue;
143+
}
144+
const result = stringifyHistoricalValue(record.content);
145+
for (const key of Object.keys(record)) {
146+
delete record[key];
147+
}
148+
record.role = "user";
149+
record.content = `[Historical tool result for ${toolName}:\n${result}]`;
91150
}
92151
}
93152

@@ -106,6 +165,6 @@ export function createVeniceStreamWrapper(
106165
replaceNullReasoningContent: true,
107166
});
108167
}
109-
applyVeniceGeminiThoughtSignatures(payload, context, model);
168+
applyVeniceGeminiToolHistoryCompatibility(payload, context, model);
110169
});
111170
}

0 commit comments

Comments
 (0)