Skip to content

Commit 1f9ee89

Browse files
steipetePeter Steinberger
andauthored
fix(google): preserve canonical replay and provider error ownership (#117193)
* fix(google): preserve provider signatures and failure outcomes * fix(google): satisfy SDK stream event and optional text types * fix(google): preserve signed empty SDK response parts --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent fcb7338 commit 1f9ee89

3 files changed

Lines changed: 619 additions & 35 deletions

File tree

‎packages/ai/src/providers/google-shared.convert.test.ts‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ describe("google-shared convertTools", () => {
167167
});
168168

169169
describe("google-shared convertMessages", () => {
170+
it.each([
171+
{ label: "serialized object", value: '{"query":"cats"}', expected: { query: "cats" } },
172+
{ label: "malformed JSON", value: "{not valid json", expected: {} },
173+
{ label: "JSON array", value: ["not", "an", "object"], expected: {} },
174+
])("coerces $label tool-call arguments to the SDK object contract", ({ value, expected }) => {
175+
const model = makeModel("gemini-3-flash");
176+
const contents = convertMessagesForTest(model, {
177+
messages: [
178+
makeGoogleAssistantMessage(model.id, [
179+
{ type: "toolCall", id: "call_1", name: "lookup", arguments: value },
180+
]),
181+
],
182+
} as Context);
183+
184+
expect(contents[0]?.parts?.[0]?.functionCall?.args).toEqual(expected);
185+
});
186+
170187
it.each([
171188
{ label: "empty user text", messages: [{ role: "user", content: "" }] },
172189
{
@@ -184,6 +201,27 @@ describe("google-shared convertMessages", () => {
184201
]);
185202
});
186203

204+
it.each([
205+
{
206+
label: "text",
207+
block: { type: "text", text: "", textSignature: "c2lnbmVk" },
208+
part: { text: "", thoughtSignature: "c2lnbmVk" },
209+
},
210+
{
211+
label: "thinking",
212+
block: { type: "thinking", thinking: "", thinkingSignature: "c2lnbmVk" },
213+
part: { thought: true, text: "", thoughtSignature: "c2lnbmVk" },
214+
},
215+
])("preserves the empty content field of a signed $label part", ({ block, part }) => {
216+
const model = makeModel("gemini-3-flash");
217+
218+
const contents = convertMessagesForTest(model, {
219+
messages: [makeGoogleAssistantMessage(model.id, [block])],
220+
} as Context);
221+
222+
expect(contents).toEqual([{ role: "model", parts: [part] }]);
223+
});
224+
187225
it("supplies the documented Gemini 3 thought-signature placeholder for unsigned calls", () => {
188226
const model = makeModel("gemini-3-flash");
189227
const contents = convertMessagesForTest(model, {
@@ -197,6 +235,183 @@ describe("google-shared convertMessages", () => {
197235
expect(contents[0]?.parts?.[0]?.thoughtSignature).toBe("skip_thought_signature_validator");
198236
});
199237

238+
it("keeps unsigned parallel Gemini 3 calls exactly as the provider issued them", () => {
239+
const model = makeModel("gemini-3-flash");
240+
const contents = convertMessagesForTest(model, {
241+
messages: [
242+
makeGoogleAssistantMessage(model.id, [
243+
{
244+
type: "toolCall",
245+
id: "call_1",
246+
name: "signed",
247+
arguments: {},
248+
thoughtSignature: "c2lnbmVk",
249+
},
250+
{ type: "toolCall", id: "call_2", name: "unsigned", arguments: {} },
251+
]),
252+
],
253+
} as Context);
254+
255+
expect(contents[0]?.parts).toEqual([
256+
{ functionCall: { name: "signed", args: {} }, thoughtSignature: "c2lnbmVk" },
257+
{ functionCall: { name: "unsigned", args: {} } },
258+
]);
259+
});
260+
261+
it.each([
262+
{ label: "identical arguments", first: { q: "cats" }, second: { q: "cats" } },
263+
{
264+
label: "reordered nested arguments",
265+
first: { first: 1, nested: { alpha: 2, beta: 3 } },
266+
second: { nested: { beta: 3, alpha: 2 }, first: 1 },
267+
},
268+
{
269+
label: "canonically distinct Unicode keys",
270+
first: { é: 1, "e\u0301": 2 },
271+
second: { "e\u0301": 2, é: 1 },
272+
},
273+
])(
274+
"keeps an earlier Gemini tool-call signature on its original $label part",
275+
({ first, second }) => {
276+
const model = makeModel("gemini-3-flash");
277+
const toolCall = { type: "toolCall", id: "call_1", name: "lookup" };
278+
const contents = convertMessagesForTest(model, {
279+
messages: [
280+
makeGoogleAssistantMessage(model.id, [
281+
{ ...toolCall, arguments: first, thoughtSignature: "c2lnbmVk" },
282+
]),
283+
{
284+
role: "toolResult",
285+
toolCallId: "call_1",
286+
toolName: "lookup",
287+
content: [{ type: "text", text: "cats" }],
288+
isError: false,
289+
timestamp: 0,
290+
},
291+
makeGoogleAssistantMessage(model.id, [{ ...toolCall, arguments: second }]),
292+
],
293+
} as Context);
294+
295+
const signatures = contents
296+
.flatMap((content) => content.parts ?? [])
297+
.filter((part) => part.functionCall)
298+
.map((part) => part.thoughtSignature);
299+
expect(signatures).toEqual(["c2lnbmVk", "skip_thought_signature_validator"]);
300+
},
301+
);
302+
303+
it("never replays a cached signature onto a later parallel Gemini function call", () => {
304+
const model = makeModel("gemini-3-flash");
305+
const cachedCall = { type: "toolCall", id: "call_1", name: "lookup", arguments: {} };
306+
const contents = convertMessagesForTest(model, {
307+
messages: [
308+
makeGoogleAssistantMessage(model.id, [{ ...cachedCall, thoughtSignature: "c2lnbmVk" }]),
309+
{
310+
role: "toolResult",
311+
toolCallId: "call_1",
312+
toolName: "lookup",
313+
content: [{ type: "text", text: "ok" }],
314+
isError: false,
315+
timestamp: 0,
316+
},
317+
makeGoogleAssistantMessage(model.id, [
318+
{ type: "toolCall", id: "other", name: "different", arguments: {} },
319+
cachedCall,
320+
]),
321+
],
322+
} as Context);
323+
324+
const signatures = contents
325+
.flatMap((content) => content.parts ?? [])
326+
.filter((part) => part.functionCall)
327+
.map((part) => part.thoughtSignature);
328+
expect(signatures).toEqual(["c2lnbmVk", "skip_thought_signature_validator", undefined]);
329+
});
330+
331+
it.each(["google-vertex", "openai-responses"])(
332+
"never replays a Gemini tool-call signature onto the foreign %s API route",
333+
(api) => {
334+
const model = makeModel("gemini-3-flash");
335+
const toolCall = { type: "toolCall", id: "call_1", name: "lookup", arguments: {} };
336+
const contents = convertMessagesForTest(model, {
337+
messages: [
338+
makeGoogleAssistantMessage(model.id, [{ ...toolCall, thoughtSignature: "c2lnbmVk" }]),
339+
{
340+
...makeGoogleAssistantMessage(model.id, [toolCall]),
341+
api,
342+
},
343+
],
344+
} as Context);
345+
346+
const signatures = contents
347+
.flatMap((content) => content.parts ?? [])
348+
.filter((part) => part.functionCall)
349+
.map((part) => part.thoughtSignature);
350+
expect(signatures).toEqual(["c2lnbmVk", "skip_thought_signature_validator"]);
351+
},
352+
);
353+
354+
it("never replays a prior user turn's Gemini tool-call signature", () => {
355+
const model = makeModel("gemini-3-flash");
356+
const toolCall = { type: "toolCall", id: "call_1", name: "lookup", arguments: {} };
357+
const contents = convertMessagesForTest(model, {
358+
messages: [
359+
makeGoogleAssistantMessage(model.id, [{ ...toolCall, thoughtSignature: "c2lnbmVk" }]),
360+
{ role: "user", content: "a new question", timestamp: 1 },
361+
makeGoogleAssistantMessage(model.id, [toolCall]),
362+
],
363+
} as Context);
364+
365+
const signatures = contents
366+
.flatMap((content) => content.parts ?? [])
367+
.filter((part) => part.functionCall)
368+
.map((part) => part.thoughtSignature);
369+
expect(signatures).toEqual(["c2lnbmVk", "skip_thought_signature_validator"]);
370+
});
371+
372+
it("resets signature replay for runtime-context carriers emitted as Google user turns", () => {
373+
const model = makeModel("gemini-3-flash");
374+
const toolCall = { type: "toolCall", id: "call_1", name: "lookup", arguments: {} };
375+
const contents = convertMessagesForTest(model, {
376+
messages: [
377+
makeGoogleAssistantMessage(model.id, [{ ...toolCall, thoughtSignature: "c2lnbmVk" }]),
378+
{
379+
role: "user",
380+
content: "volatile runtime context",
381+
runtimeContextCarrier: true,
382+
timestamp: 1,
383+
},
384+
makeGoogleAssistantMessage(model.id, [toolCall]),
385+
],
386+
} as Context);
387+
388+
const signatures = contents
389+
.flatMap((content) => content.parts ?? [])
390+
.filter((part) => part.functionCall)
391+
.map((part) => part.thoughtSignature);
392+
expect(signatures).toEqual(["c2lnbmVk", "skip_thought_signature_validator"]);
393+
});
394+
395+
it.each(["gemini-2.5-pro", "claude-3-opus"])(
396+
"does not replay thought signatures for the unsupported %s model",
397+
(modelId) => {
398+
const model = makeModel(modelId);
399+
const toolCall = { type: "toolCall", id: "call_1", name: "lookup", arguments: {} };
400+
const contents = convertMessagesForTest(model, {
401+
messages: [
402+
makeGoogleAssistantMessage(model.id, [{ ...toolCall, thoughtSignature: "c2lnbmVk" }]),
403+
makeGoogleAssistantMessage(model.id, [toolCall]),
404+
],
405+
} as Context);
406+
407+
const signatures = contents
408+
.flatMap((content) => content.parts ?? [])
409+
.filter((part) => part.functionCall)
410+
.map((part) => part.thoughtSignature);
411+
expect(signatures).toEqual(["c2lnbmVk", undefined]);
412+
},
413+
);
414+
200415
function expectConsecutiveMessagesNotMerged(params: {
201416
modelId: string;
202417
first: string;

0 commit comments

Comments
 (0)