fix: bridge tool image artifacts with an assistant message for strict providers#287
Open
JumpLink wants to merge 1 commit into
Open
fix: bridge tool image artifacts with an assistant message for strict providers#287JumpLink wants to merge 1 commit into
JumpLink wants to merge 1 commit into
Conversation
… providers
formatArtifactPayload appended the tool artifact as a trailing human message
directly after the tool result. Strict OpenAI-compatible providers (Scaleway,
Mistral) reject a user message that immediately follows a tool message
("Unexpected role 'user' after role 'tool'"), so image-returning tools failed
with a 400 validation error. Insert a short assistant bridge before the human
message to keep role alternation valid; behavior is unchanged for providers
that already accepted the trailing human message.
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.
Problem
When a tool returns an image artifact,
formatArtifactPayloadmoves the artifact into a trailingHumanMessageappended right after theToolMessage. Strict OpenAI-compatible providers reject ausermessage that immediately follows atoolmessage. On Scaleway (Mistral) this fails the whole request:(For reference, placing the image inside the tool message instead fails differently on the same provider —
3 validation errors for ToolMessage ... content.list[TextChunk].1.type Input should be <ChunkTypes.text: 'text'>— so the image can live neither in the tool message nor directly after it.)Fix
Insert a short
assistantbridge message between the tool result and the human message that carries the artifact. Role alternation (tool → assistant → user) is then valid on strict providers, while providers that already accepted the trailing human message are unaffected.Verified against the live Scaleway API with
mistral-small-3.2: the bridged sequence returns 200 and the vision model sees/evaluates the image, whereas the un-bridged sequence 400s.Adds a unit test asserting the
tool → ai → humanordering and that the image part lands on the trailing human message.