fix(bedrock): deliver unsupported tool-return media to models that isolate `toolResult` turns

Merged
DO
5mo ago
+401-17

Stacked on #6098 (contains its commits until it merges). Depends on the bedrock_tool_result_colocatable_content flag and the corrected bedrock_supported_media_kinds_in_tool_returns sets that #6098 introduces. The diff will show #6098's changes too until it merges.

The problem

When a tool returns media a Bedrock model can't put inside a toolResult (per bedrock_supported_media_kinds_in_tool_returns), the mapping falls back to a {'text': 'See file X.'} placeholder inside the toolResult plus the file as a sibling block in the same user message ([toolResult, 'This is file X:', file_block]).

Models like Llama and Mistral require a toolResult to be alone in its turn (their bedrock_tool_result_colocatable_content is frozenset()). They reject that co-located message, so e.g. an image returned from a tool to Mistral (which accepts documents inside a toolResult but not images) could not be delivered at all.

The naive fix — emit the sibling as its own user message and let the co-location merge pass split each toolResult/media pair apart — is insufficient for multiple tool calls, because it pushes later toolResults behind earlier media, and Bedrock requires every toolResult for a tool-use turn to arrive in the immediately-following message.

Live findings (us.mistral.pixtral-large-2502-v1:0, image tool return)

LayoutResult
single: user[toolResult, "file:", image] (current, co-located)REJECTED — "Conversation blocks and tool result blocks cannot be provided in the same turn."
single: user[toolResult] / assistant["."] / user["file:", image]ACCEPTED
two: user[TR1, TR2] / assistant["."] / user["f1:", img1, "f2:", img2] (grouped, deferred)ACCEPTED
two: user[TR1] / a / user[img1] / a / user[TR2] / a / user[img2] (interleaved)REJECTED — "Expected toolResult blocks at messages.2.content for the following Ids: t2"
two: user[TR1] / a / user[TR2] (results split across turns, no media)REJECTED — same
two: user[TR1, TR2] (grouped, no media)ACCEPTED

Baseline confirmed: for a multi-tool-call turn, every toolResult must sit together in the message immediately after the tool-use turn — splitting them across turns is rejected. (Also learned live: Mistral requires tool-call ids to be exactly 9 alphanumeric chars.) So the media must be deferred past all the grouped results, not interleaved.

Nova (permissive) accepts the old interleaved layout and the grouped one — it has no constraint — so preserving its exact current output is safe and intended.

The design

Keep all toolResult blocks grouped in the user message immediately after the tool-use turn (as today). For media that can't co-locate with a toolResult for this model (kind not in bedrock_tool_result_colocatable_content), collect it across the whole consecutive tool-return group and flush it as a separate user message after the grouped tool results. The existing #6098 co-location merge pass then separates it with one synthetic assistant turn for isolate models (Llama, Mistral). Media the model does allow beside a toolResult stays co-located exactly as before.

This mirrors the existing model-level pattern in google.py::_map_tool_return, which also places fallback media after the tool result.

Permissive models unchanged: for Nova/Qwen (and any model whose media kind is co-locatable), the media stays in the toolResult's own turn byte-for-byte — verified by mapping-shape unit tests.

Tests

  • Unit tests pinning the reshaped mapping (our cassette matcher isn't request-body-sensitive): Mistral single sibling-media tool return, Mistral two sibling-media tool returns (grouped results + deferred media), and Nova showing media stays co-located (unchanged).
  • VCR tests with real recorded cassettes proving Bedrock accepts the reshaped history end-to-end: Mistral single image tool return, and the two-image case.

🤖 Generated with Claude Code