Skip to content

fix: match userMessage fixtures on split image turns for Anthropic, Gemini, Bedrock and Cohere - #643

Merged
jpr5 merged 3 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/split-image-turns-user-message
Nov 26, 2026
Merged

jpr5 merged 3 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/split-image-turns-user-message

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor

When a client sends one prompt as a text message followed by an image only message, userMessage fixtures now match on Anthropic, Gemini, Bedrock and Cohere v2 the same way they already do on OpenAI.

What this code is for

Each provider handler turns its request body into the shared ChatMessage[] list before fixture matching, for example claudeToCompletionRequest in src/messages.ts. getLastUserText then picks the text a userMessage fixture is compared against: it skips trailing user messages whose content is null and stops at the first one that has a string, including "".

Before this PR

A test using a strict LLMock with the fixture { match: { userMessage: "what is in this image" } } sends one turn to POST /v1/messages as two user messages: the prompt, then a message holding only an image block.

  1. claudeToCompletionRequest converts the prompt message to { role: "user", content: "what is in this image" }.
  2. It converts the image only message with extractClaudeTextContent, which finds no text block and returns "".
  3. getLastUserText reaches that last message first, sees "", and returns it as the user text.
  4. No fixture matches "", so strict mode answers 503 "Strict mode: no fixture matched".

The same thing happens in the Gemini generateContent user branch, Gemini Interactions (user_input steps and role turns), Bedrock invoke, Bedrock Converse and Cohere v2. OpenAI Chat Completions and Responses (after #611) already return 200.

After this PR

Same test, same request.

  1. Same as above for the prompt message.
  2. The image only message has no text block, so extractClaudeUserContent returns null instead of "".
  3. getLastUserText skips the null message and returns "what is in this image" from the message before it.
  4. The fixture matches and the request gets 200 with "A logo.".
  5. A message that does carry a text block, even { type: "text", text: "" }, 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: 503 on 6 provider paths"]
direction TB
  B1["user: prompt, then user: image only"] --> B2["provider converter"]
  B2 --> B3{"image only message"}
  B3 -->|"content becomes empty string"| B4["getLastUserText returns empty string"]
  B4 --> B5["no fixture matched, 503"]
  B3 -->|"message with a text part"| B6["text kept"]
end

subgraph AFTER["AFTER: 200, same as OpenAI"]
direction TB
  A1["user: prompt, then user: image only"] --> A2["provider converter"]
  A2 --> A3{"THE CHANGE: no text part means null"}
  A3 -->|"image only message is null"| A4["getLastUserText skips it, returns prompt"]
  A4 --> A5["userMessage fixture matches, 200"]
  A3 -->|"explicit empty text part"| A6["still empty string, 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

getLastUserText focus view
getLastUserText with its one caller matchFixtureDiagnostic and its one callee getTextContent, Functions view in Architect

extractClaudeUserContent focus view
extractClaudeUserContent called by claudeToCompletionRequest and calling extractClaudeTextContent, Functions view in Architect

The change

Every user message converter now applies the #611 rule: when the message content is a list of parts and none of them is a text part, the content is null; otherwise it is the joined text as before.

  • src/messages.ts: new extractClaudeUserContent, used for plain Anthropic user messages.
  • src/bedrock.ts: new extractUserContent, used for plain Bedrock invoke user messages.
  • src/cohere.ts: new extractUserContent, used for Cohere v2 user messages.
  • src/gemini.ts: the plain user branch returns null when textParts is empty.
  • src/gemini-interactions.ts: both the user_input step and the user role turn return null when there is no text part.
  • src/bedrock-converse.ts: the plain user message returns null when no block has a text key. A block with text: "" still counts, so it stays "".

String content, missing content, an empty part list, tool result messages, system and assistant messages are untouched, so the existing tests that pin "" for those (for example handles Step[] user_input with empty content) still pass. The new src/__tests__/split-image-turns.test.ts runs the same three checks on each of the seven paths (Interactions has two input formats): a strict server returns 200 for the split turn, the converter gives [prompt, null], and an explicit empty text part stays "". On main the first two fail on every path (14 failures, 503 and ""), and all 21 pass on this branch.

What changes for a user

  • A split prompt plus image turn now matches its userMessage fixture on Anthropic Messages, Gemini generateContent, Gemini Interactions, Bedrock invoke, Bedrock Converse and Cohere v2, the same as OpenAI Chat Completions and Responses.
  • Fixtures keyed on an explicit empty text body still see "", as you asked.
  • Single message turns, multi message turns where every message has text, and tool result turns match exactly as before.
  • Converse is included for consistency, as you suggested, even though real Converse clients rarely send this split.
  • Ollama /api/chat is left alone, since its content is always a string, so "" there is what the client sent.

One question: the Gemini Live WebSocket has the same plain user branch in src/ws-gemini-live.ts. It wasn't in your list so I kept it out, but would you like it in this PR too? Happy to add it either way.

Thanks again @jpr5 for the quick go ahead and for pointing at the #611 shape, it made this one really easy to line up.

Closes #639

…ompt (CopilotKit#639)

Applies the CopilotKit#611 shape to Anthropic Messages, Gemini generateContent and
Interactions, Bedrock invoke and Converse, and Cohere v2. A user message
whose parts hold no text part converts to null, so getLastUserText falls
back to the prompt before it. An explicit empty text part, string content
and an empty part list keep their current values.
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 92c8517

@jpr5
jpr5 merged commit 6d9f87e into CopilotKit:main Nov 26, 2026
30 checks passed
@jpr5

jpr5 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks for turning this around so fast. I ran your tests against main first and every one failed, so the fix does exactly what it needs to. I put a changelog line on top.

Gemini Live can wait. If you run into the same thing there, send a PR.

jpr5 added a commit that referenced this pull request Dec 1, 2026
…648)

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`](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/ws-gemini-live.ts#L151-L211)
turns the `turns` of each Live `clientContent` message into the shared
`ChatMessage[]` list ([called
here](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/ws-gemini-live.ts#L474)).
The handler then matches fixtures on the whole session,
[`[...session.conversationHistory,
...newMessages]`](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/ws-gemini-live.ts#L516),
and
[`getLastUserText`](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/router.ts#L172-L179)
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](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/ws-gemini-live.ts#L179-L182)
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.

```mermaid
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
```

![geminiTurnsToMessages focus
view](https://raw.githubusercontent.com/abhiramshaji-cyber/Architect/7a2edf049f42a7f39add684eab695ad7e4aec631/aimock-live/geminiTurnsToMessages.png)
geminiTurnsToMessages with its one caller processMessage and its one
callee generateToolCallId, Functions view in
[Architect](https://github.com/abhiramshaji-cyber/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`](https://github.com/CopilotKit/aimock/blob/445235e280082e1cea2bb773c40739f8faa3a385/src/gemini.ts#L222-L223):

```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 #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.
@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.

Split image turns miss userMessage fixtures on Anthropic, Gemini, Bedrock and Cohere

2 participants