fix(bedrock): handle `toolResult` attachment co-location per-model via `bedrock_tool_result_colocatable_content`
Closes #6081
Problem
On Bedrock, pydantic-ai merges consecutive user turns into one Converse message. When a tool-result turn is followed by a user turn carrying an attachment, they merge into one user message co-locating a toolResult block with the attachment — which some models reject with a misleading ValidationException about a missing tool_result.
The original fix split every such turn with a synthetic assistant message for all models. Live testing against Bedrock (us-east-1) shows the constraint is per-model, so a blanket split is both incomplete (misses models with a stricter rule) and over-broad (needlessly pollutes models with no rule).
Evidence (live Converse, what may share a user message with a toolResult)
| Family | text | image | document (pdf) | document (csv/txt/docx) | video |
|---|---|---|---|---|---|
| Anthropic | ✅ | ✅ | ✅ | ❌ | ❌ (video unsupported) |
| Amazon Nova | ✅ | ✅ | ✅ | ✅ | ✅ |
| Meta Llama 4 | ❌ | ❌ | ❌ | ❌ | ❌ |
| Mistral (pixtral) | ❌ | ❌ | ❌ | ❌ | ❌ |
| Qwen | ✅ | ✅ | — (no doc support) | — | — |
- Anthropic rejects documents and video beside a
toolResult, but accepts text and images. (PDF happens to pass, but the pdf-vs-other-document split is undocumented and brittle, so we conservatively treat all documents as non-co-locatable.) - Nova has no constraint at all — the old blanket fix inserted a pointless dummy turn here.
- Llama and Mistral reject any content sharing a
toolResultturn, even plain text — a broader form of the same bug that the attachment-only guard missed.
Mechanism notes, also verified: a bare split into two consecutive user turns does not work (Bedrock re-merges same-role turns), so an alternating assistant turn is required; the separator must be non-whitespace (. works everywhere); block order within the message is irrelevant. AWS does not document the toolResult+document constraint — it's model behavior — which is why this is driven by empirical per-family testing.
Fix
Add a bedrock_tool_result_colocatable_content model-profile flag listing the content-block kinds a model accepts alongside a toolResult. The merge pass splits the turns (separating them with a synthetic . assistant message) only when the combined content isn't co-locatable for that model; otherwise it merges as before.
| Family | flag value | effect |
|---|---|---|
| Anthropic | {'text', 'image'} | splits out documents + video |
| Meta, Mistral | frozenset() | toolResult isolated from all other content |
| everyone else (default) | all kinds | merges freely, no dummy turn |
The default is permissive, so no behavior change for uncovered families.
Tests
- Per-family mapping unit tests pinning the exact reshaped sequence (Anthropic split, Nova merged, Llama isolated).
- VCR cassettes recorded against live Bedrock proving each family accepts its reshaped history: Anthropic with a
text/plaindocument (the original PDF recording is accepted even without the fix, so it proved nothing — a text document actually reproduces the rejection), Nova co-located with no dummy turn, and Llama with text isolated. - Provider-profile assertions for the new flag values.
Follow-ups (separate PRs)
Live testing surfaced two adjacent, distinct defects that are handled in their own PRs rather than widening this one:
bedrock_supported_media_kinds_in_tool_returnshas wrong per-family values (Mistral/Writer acceptdocumentbut rejectimageinside tool results; Qwen accepts neither; Nova rejects binary documents).- The "conversation must start with a user message" workaround is provider-specific (Anthropic and Qwen accept a leading assistant turn) and currently applied as a blanket rule.
🤖 Generated with Claude Code