fix(databricks): ensure parallel tool image responses don't interleave tool results#9241
Conversation
…e tool results When multiple parallel tool calls each return images, format_tool_response was emitting user-role image messages between tool-role result messages. Claude's API requires all tool_result blocks to be consecutive after tool_use blocks. Fix by deferring image user-messages until all tool result messages for a given goose Message have been emitted. Fixes aaif-goose#7449 Signed-off-by: Steve Marshall <steve.marshall@fasthosts.com>
- Merge with latest main (formatting updates from edition bump) - Trim redundant comments in code and tests to one-liners Signed-off-by: Douwe Osinga <douwe@squareup.com>
DOsinga
left a comment
There was a problem hiding this comment.
Clean fix for #7449. The deferred-image-messages approach is correct and minimal. I merged main, ran cargo fmt (Rust edition bump changed import ordering), and trimmed the verbose comments down to one-liners. Tests pass, clippy clean.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 874a70b880
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if msg.role == "user" { | ||
| pending_image_messages.push(msg); |
There was a problem hiding this comment.
Preserve image-to-tool association when reordering responses
Deferring every user image message until after all tool responses breaks the implicit pairing between a tool result and its image payload. In mixed parallel results (e.g., tool A has an image, tool B is text-only), the image from A is emitted after B, and these image messages carry no tool_call_id, so the model can misattribute which tool produced which image. This is a behavior regression from the prior adjacent ordering and can lead to incorrect reasoning about tool outputs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I've prepared a pull request into this branch so the approach I took can be reviewed first msteve123#1
@DOsinga Did you want to take a look?
Fixes #7449
Collects all tool-role messages first and appends user-role image messages after, so the API sees consecutive tool results followed by user image messages. This prevents Claude's API from rejecting requests when multiple parallel tool calls return images.