From 292bfc6e0ca15edcf4a6d9abb5a3bdad869ea17d Mon Sep 17 00:00:00 2001 From: Abhiram Shaji Date: Mon, 30 Nov 2026 14:55:50 -0500 Subject: [PATCH 1/4] test: cover split prompt and image turns on Gemini Live --- src/__tests__/split-image-turns-live.test.ts | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/__tests__/split-image-turns-live.test.ts diff --git a/src/__tests__/split-image-turns-live.test.ts b/src/__tests__/split-image-turns-live.test.ts new file mode 100644 index 00000000..a61bafcd --- /dev/null +++ b/src/__tests__/split-image-turns-live.test.ts @@ -0,0 +1,81 @@ +import { afterEach, describe, expect, it } from "vitest"; +import type { Fixture } from "../types.js"; +import { isChatCompletionBody } from "../journal.js"; +import { createServer, type ServerInstance } from "../server.js"; +import { connectWebSocket, type WSTestClient } from "./ws-test-client.js"; + +const WS_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent"; +const PROMPT = "what is in this image"; +const text = (t: string) => ({ role: "user", parts: [{ text: t }] }); +const image = { + role: "user", + parts: [{ inlineData: { mimeType: "image/png", data: "iVBORw0KGgo=" } }], +}; +const clientContent = (...turns: unknown[]) => + JSON.stringify({ clientContent: { turns, turnComplete: true } }); + +const fixtures: Fixture[] = [{ match: { userMessage: PROMPT }, response: { content: "A logo." } }]; + +let instance: ServerInstance | null = null; + +afterEach(async () => { + if (instance) { + await new Promise((resolve) => instance!.server.close(() => resolve())); + instance = null; + } +}); + +async function open(strict: boolean): Promise { + instance = await createServer(fixtures, { strict }); + const ws = await connectWebSocket(instance.url, WS_PATH); + ws.send(JSON.stringify({ setup: { model: "gemini-2.0-flash-exp" } })); + await ws.waitForMessages(1); + return ws; +} + +function lastContents(): unknown[] { + const body = instance!.journal.getLast()?.body; + if (!isChatCompletionBody(body)) throw new Error("journal entry has no chat body"); + return body.messages.map((m) => m.content); +} + +describe("Gemini Live: prompt split from its image", () => { + it("matches the prompt when an image only turn follows it in one clientContent", async () => { + const ws = await open(true); + ws.send(clientContent(text(PROMPT), image)); + const raw = await ws.waitForMessages(3); + expect(JSON.parse(raw[1]).serverContent.modelTurn.parts[0].text).toBe("A logo."); + expect(lastContents()).toEqual([PROMPT, null]); + ws.close(); + }); + + it("matches the earlier prompt when the image arrives in its own clientContent", async () => { + const ws = await open(true); + ws.send(clientContent(text(PROMPT))); + await ws.waitForMessages(3); + ws.send(clientContent(image)); + const raw = await ws.waitForMessages(5); + expect(JSON.parse(raw[3]).serverContent.modelTurn.parts[0].text).toBe("A logo."); + expect(lastContents()).toEqual([PROMPT, "A logo.", null]); + ws.close(); + }); + + it.each([ + ["an explicit empty text part", { role: "user", parts: [{ text: "" }] }], + ["an empty parts list", { role: "user", parts: [] }], + ])("keeps %s as an empty string", async (_, turn) => { + const ws = await open(false); + ws.send(clientContent(turn, image)); + await ws.waitForMessages(2); + expect(lastContents()).toEqual(["", null]); + ws.close(); + }); + + it("keeps the text of a turn that mixes text and an image", async () => { + const ws = await open(true); + ws.send(clientContent({ role: "user", parts: [...image.parts, { text: PROMPT }] })); + await ws.waitForMessages(3); + expect(lastContents()).toEqual([PROMPT]); + ws.close(); + }); +}); From d4977875637d655706b6a0c423407b29885ce37b Mon Sep 17 00:00:00 2001 From: Abhiram Shaji Date: Mon, 30 Nov 2026 14:55:52 -0500 Subject: [PATCH 2/4] fix: keep image only Gemini Live user turns null so userMessage matches the prompt --- src/ws-gemini-live.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ws-gemini-live.ts b/src/ws-gemini-live.ts index 5ef6aab6..6cce9511 100644 --- a/src/ws-gemini-live.ts +++ b/src/ws-gemini-live.ts @@ -178,7 +178,8 @@ function geminiTurnsToMessages(turns: GeminiLiveTurn[], logger?: Logger): ChatMe } } else { const text = textParts.map((p) => p.text!).join(""); - messages.push({ role: "user", content: text }); + const hasText = textParts.length > 0 || turn.parts.length === 0; + messages.push({ role: "user", content: hasText ? text : null }); } } else if (role === "model") { const funcCalls = turn.parts.filter((p) => p.functionCall); From 7e1a3a28aab776f05694e0cb934ddf3111a82c6f Mon Sep 17 00:00:00 2001 From: Abhiram Shaji Date: Mon, 30 Nov 2026 14:55:52 -0500 Subject: [PATCH 3/4] docs(changelog): note Gemini Live split image turn fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e61a2b79..79aa81e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Fixed - Split prompt + image-only turns match `userMessage` on Anthropic, Gemini, Bedrock, Cohere (#643) +- Split prompt + image-only turns match `userMessage` on Gemini Live too ## [1.48.1] - 2026-11-20 From 33d08b3624d2ad0cb21ff07bc17c2b35c4eadfda Mon Sep 17 00:00:00 2001 From: Jordan Ritter Date: Mon, 30 Nov 2026 18:40:01 -0800 Subject: [PATCH 4/4] docs(changelog): add PR reference --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79aa81e8..f1698a16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ ### Fixed - Split prompt + image-only turns match `userMessage` on Anthropic, Gemini, Bedrock, Cohere (#643) -- Split prompt + image-only turns match `userMessage` on Gemini Live too +- Split prompt + image-only turns match `userMessage` on Gemini Live too (#648) ## [1.48.1] - 2026-11-20