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.

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