Skip to content

fix: match userMessage fixtures on split image turns for Gemini Live - #648

Merged
jpr5 merged 4 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/gemini-live-image-only-turns
Dec 1, 2026
Merged

jpr5 merged 4 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/gemini-live-image-only-turns

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor

When a Gemini Live client sends a prompt and then an image only turn, a userMessage fixture now matches the prompt, the same as every other provider after #643.

What this code is for

geminiTurnsToMessages turns the turns of each Live clientContent message into the shared ChatMessage[] list (called here). The handler then matches fixtures on the whole session, [...session.conversationHistory, ...newMessages], and getLastUserText picks the user text a userMessage fixture is compared against: it skips user messages whose content is null and stops at the first string, including "".

Before this PR

A test using a strict LLMock with the fixture { match: { userMessage: "what is in this image" } } opens the Live WebSocket and sends one clientContent with two user turns: the prompt, then a turn holding only an inlineData image.

  1. geminiTurnsToMessages converts the prompt turn to { role: "user", content: "what is in this image" }.
  2. The image turn has no text part, so the plain user branch joins zero parts and pushes { role: "user", content: "" }.
  3. getLastUserText reaches that message first, sees "", and returns it.
  4. No fixture matches "", so strict mode closes the socket with 1008.
  5. Sending the image as its own clientContent after the prompt fails the same way, since the history ends with that "" message too.

After this PR

Same test, same message.

  1. Same as above for the prompt turn.
  2. The image turn has no text part, so its content is now null.
  3. getLastUserText skips the null message and returns "what is in this image".
  4. The fixture matches and the client gets serverContent with "A logo.".
  5. A turn with { text: "" }, or with an empty parts list, still converts to "", so fixtures keyed on an empty body behave as before.
flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: strict close 1008"]
direction TB
  B1["clientContent: prompt turn, image only turn"] --> B2["geminiTurnsToMessages"]
  B2 --> B3{"image only turn"}
  B3 -->|"content becomes empty string"| B4["getLastUserText returns empty string"]
  B4 --> B5["no fixture matched, close 1008"]
  B3 -->|"turn with a text part"| B6["text kept"]
end

subgraph AFTER["AFTER: fixture matches, A logo."]
direction TB
  A1["clientContent: prompt turn, image only turn"] --> A2["geminiTurnsToMessages"]
  A2 --> A3{"THE CHANGE: no text part means null"}
  A3 -->|"image only turn is null"| A4["getLastUserText skips it, returns prompt"]
  A4 --> A5["userMessage fixture matches"]
  A3 -->|"text part or empty parts"| A6["same as before"]
end

B1 ~~~ A1

class B1,B2,B6 box
class A1,A2,A6 box
class B3,B4,B5 bad
class A3,A4,A5 good

linkStyle 2 stroke:#cc0000,stroke-width:5px
linkStyle 7 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

geminiTurnsToMessages focus view
geminiTurnsToMessages with its one caller processMessage and its one callee generateToolCallId, Functions view in Architect

The change

In src/ws-gemini-live.ts, the plain user branch of geminiTurnsToMessages now uses the same rule as the generateContent branch in src/gemini.ts:

const hasText = textParts.length > 0 || turn.parts.length === 0;
messages.push({ role: "user", content: hasText ? text : null });

That is the whole fix. clientContent is the only Live message that builds user messages (toolResponse only builds tool messages, anything else is rejected), so nothing else needed to change. The null goes through the same matching and journal code that #643 already sends null through for Gemini generateContent.

The new src/__tests__/split-image-turns-live.test.ts sits next to the #643 tests and covers: the split in one clientContent, the image sent as its own clientContent after the prompt, an explicit { text: "" } part, an empty parts list, and one turn mixing text and an image. On main the first four fail (two strict closes, two "" where null is expected), and all five pass here. Typecheck, lint, prettier and the full test suite pass.

What changes for a user

  • A prompt plus image split across turns now matches its userMessage fixture on Gemini Live, whether both turns come in one clientContent or in two.
  • Text turns, turns mixing text and an image, function response turns, and turns with { text: "" } or no parts match exactly as before.
  • I added a line under Unreleased in CHANGELOG.md so you don't have to write one this time. It has no PR number yet; would you rather I add the number, or fold it into the fix: match userMessage fixtures on split image turns for Anthropic, Gemini, Bedrock and Cohere #643 line?

Hope you're doing well @jpr5, and thanks again for merging #643 so quickly and for the note that Gemini Live could come as its own PR. Your hasText shape from gemini.ts carried straight over, so this one stayed tiny.

@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@648

commit: 33d08b3

@jpr5
jpr5 merged commit 20de2de into CopilotKit:main Dec 1, 2026
30 checks passed
@jpr5

jpr5 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks! Added the PR number to your changelog line on the way in.

@jpr5 jpr5 mentioned this pull request Dec 1, 2026
jpr5 added a commit that referenced this pull request Dec 1, 2026
## [1.49.0] - 2026-11-30

### Added

- Replayable seeded chaos: `--chaos-seed`/`x-aimock-chaos-seed`; journal `chaosOrigin` (#618, #653)
- Recorder writes `requestHash`; stale replays get journal `staleFixture` and a warning (#640, #651)

### Changed

- `aimock-pytest` 0.7.4 defaults to `@copilotkit/aimock` 1.49.0

### Fixed

- Split image turns match `userMessage` on Anthropic, Gemini (+Live), Bedrock, Cohere (#643, #648)
- `llmock` now errors on blank `--port`/`--host`/`--latency`/... values instead of starting (#644)
jpr5 added a commit that referenced this pull request Dec 1, 2026
**Merging this publishes @copilotkit/aimock 1.49.0 to npm and
aimock-pytest 0.7.4 to PyPI.**

## [1.49.0] - 2026-11-30

### Added

- Replayable seeded chaos: `--chaos-seed`/`x-aimock-chaos-seed`; journal
`chaosOrigin` (#618, #653)
- Recorder writes `requestHash`; stale replays get journal
`staleFixture` and a warning (#640, #651)

### Changed

- `aimock-pytest` 0.7.4 defaults to `@copilotkit/aimock` 1.49.0

### Fixed

- Split image turns match `userMessage` on Anthropic, Gemini (+Live),
Bedrock, Cohere (#643, #648)
- `llmock` now errors on blank `--port`/`--host`/`--latency`/... values
instead of starting (#644)

Two new features, so this is a minor release.

## Version carriers (from `git grep -F 1.48.1`)

Bumped to 1.49.0:
- `package.json` version
- `charts/aimock/Chart.yaml` appVersion
- `.claude-plugin/plugin.json` version
- `.claude-plugin/marketplace.json` source version (`^1.49.0`)
- `docs/index.html` banner
- `packages/aimock-pytest/README.md` `--aimock-version` default
- `packages/aimock-pytest/src/aimock_pytest/_version.py`
`AIMOCK_VERSION`

Also bumped: `packages/aimock-pytest/pyproject.toml` 0.7.3 → 0.7.4,
because the pin moved (same pattern as #628).

Left as is: the `## [1.48.1]` CHANGELOG section.

## CHANGELOG edits

- `## [Unreleased]` becomes `## [1.49.0] - 2026-11-30`.
- Seven bullets became four, plus the aimock-pytest `Changed` line: the
two #618 lines are one, #643 and #648 are one, and #651 joins the
stale-fixture line.
- The stale-fixture line now references PR #640 instead of issue #638.

## Docs

- `docs/record-replay`: the `llmock` flag table gets `--chaos-seed` and
`--chaos-keys-max`, and a note that blank
`--port`/`--host`/`--latency`/`--journal-max`/`--fixture-counts-max`
values are a usage error (#644).
- `docs/fixtures`: the "last user message" rule now says a trailing
image-only user message is skipped (#643, #648).
- `README.md`: the Record & Replay line mentions stale-fixture flagging.
Seeded chaos was already in the Chaos Testing line.

Already covered, left alone: `docs/chaos-testing` (seeds,
`x-aimock-chaos-seed`, `chaosOrigin`, `--chaos-keys-max`) and the Stale
Fixtures section of `docs/record-replay` (`requestHash`, `staleFixture`,
the `STALE FIXTURE` warning, `requestTransform`). The `package.json`
description already matches the README subtitle.

## Local checks
- `pnpm install --frozen-lockfile`, `format:check`, `lint`, `typecheck`,
`build`: pass
- `api:report`: 14 entries, 0 failed, no report change
- `vitest run`: 503 files, 20,346 passed, 11 skipped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants