Skip to content

fix: bridge tool image artifacts with an assistant message for strict providers#287

Open
JumpLink wants to merge 1 commit into
danny-avila:mainfrom
faktenforum:fix/tool-image-role-order-scaleway
Open

fix: bridge tool image artifacts with an assistant message for strict providers#287
JumpLink wants to merge 1 commit into
danny-avila:mainfrom
faktenforum:fix/tool-image-role-order-scaleway

Conversation

@JumpLink

@JumpLink JumpLink commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Problem

When a tool returns an image artifact, formatArtifactPayload moves the artifact into a trailing HumanMessage appended right after the ToolMessage. Strict OpenAI-compatible providers reject a user message that immediately follows a tool message. On Scaleway (Mistral) this fails the whole request:

400 Unexpected role 'user' after role 'tool'

(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 assistant bridge 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 → human ordering and that the image part lands on the trailing human message.

… 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.
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.

1 participant