Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#648)

## [1.48.1] - 2026-11-20

Expand Down
81 changes: 81 additions & 0 deletions src/__tests__/split-image-turns-live.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>((resolve) => instance!.server.close(() => resolve()));
instance = null;
}
});

async function open(strict: boolean): Promise<WSTestClient> {
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();
});
});
3 changes: 2 additions & 1 deletion src/ws-gemini-live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading