Repository navigation
fix: match userMessage fixtures on split image turns for Anthropic, Gemini, Bedrock and Cohere - #643
Merged
jpr5 merged 3 commits intoNov 26, 2026
Conversation
…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.
This was referenced Nov 26, 2026
Closed
commit: |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a client sends one prompt as a text message followed by an image only message,
userMessagefixtures 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 exampleclaudeToCompletionRequestinsrc/messages.ts.getLastUserTextthen picks the text auserMessagefixture is compared against: it skips trailing user messages whose content isnulland 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 toPOST /v1/messagesas two user messages: the prompt, then a message holding only an image block.claudeToCompletionRequestconverts the prompt message to{ role: "user", content: "what is in this image" }.extractClaudeTextContent, which finds notextblock and returns"".getLastUserTextreaches that last message first, sees"", and returns it as the user text."", so strict mode answers 503 "Strict mode: no fixture matched".The same thing happens in the Gemini generateContent user branch, Gemini Interactions (
user_inputsteps 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.
extractClaudeUserContentreturnsnullinstead of"".getLastUserTextskips thenullmessage and returns"what is in this image"from the message before it.{ 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:#111827getLastUserText with its one caller matchFixtureDiagnostic and its one callee getTextContent, Functions view in Architect
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: newextractClaudeUserContent, used for plain Anthropic user messages.src/bedrock.ts: newextractUserContent, used for plain Bedrock invoke user messages.src/cohere.ts: newextractUserContent, used for Cohere v2 user messages.src/gemini.ts: the plain user branch returnsnullwhentextPartsis empty.src/gemini-interactions.ts: both theuser_inputstep and the user role turn returnnullwhen there is notextpart.src/bedrock-converse.ts: the plain user message returnsnullwhen no block has atextkey. A block withtext: ""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 examplehandles Step[] user_input with empty content) still pass. The newsrc/__tests__/split-image-turns.test.tsruns 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
userMessagefixture on Anthropic Messages, Gemini generateContent, Gemini Interactions, Bedrock invoke, Bedrock Converse and Cohere v2, the same as OpenAI Chat Completions and Responses."", as you asked./api/chatis left alone, since itscontentis 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