Skip to content

Commit 4b75014

Browse files
fix(provider): port upstream Bedrock tool-result image hoisting (anomalyco/opencode#50272)
Bedrock Converse only accepts media in tool results for Claude, Nova and Llama 4; hoist tool-result media into a user message for other Bedrock models. Closes #1713 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 13a2a01 commit 4b75014

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,12 @@ export namespace MessageV2 {
741741
const supportsMediaInToolResults = (() => {
742742
if (model.api.npm === "@ai-sdk/anthropic") return true
743743
if (model.api.npm === "@ai-sdk/openai") return true
744-
if (model.api.npm === "@ai-sdk/amazon-bedrock") return true
744+
// altimate_change start — upstream_fix: Bedrock Converse only accepts tool-result media for Claude, Nova and Llama 4 (anomalyco/opencode#50272)
745+
if (model.api.npm === "@ai-sdk/amazon-bedrock") {
746+
const id = model.api.id.toLowerCase()
747+
return id.includes("anthropic.") || id.includes("nova") || id.includes("llama4") || id.includes("llama-4")
748+
}
749+
// altimate_change end
745750
if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
746751
if (model.api.npm === "@ai-sdk/google") {
747752
const id = model.api.id.toLowerCase()

‎packages/opencode/test/session/message-v2.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,93 @@ describe("session.message-v2.toModelMessage", () => {
608608
])
609609
})
610610

611+
describe("bedrock tool-result images", () => {
612+
const png = Buffer.from("png").toString("base64")
613+
const bedrock = (apiID: string): Provider.Model => ({
614+
...model,
615+
id: ModelID.make(`amazon-bedrock/${apiID}`),
616+
providerID: ProviderID.make("amazon-bedrock"),
617+
api: { id: apiID, url: "https://bedrock-runtime.us-east-1.amazonaws.com", npm: "@ai-sdk/amazon-bedrock" },
618+
capabilities: {
619+
...model.capabilities,
620+
attachment: true,
621+
input: { ...model.capabilities.input, image: true },
622+
},
623+
})
624+
const history = (): SessionV1.WithParts[] => [
625+
{
626+
info: userInfo("m-user-bedrock-png"),
627+
parts: [{ ...basePart("m-user-bedrock-png", "u1"), type: "text", text: "read it" }] as SessionV1.Part[],
628+
},
629+
{
630+
info: assistantInfo("m-assistant-bedrock-png", "m-user-bedrock-png"),
631+
parts: [
632+
{
633+
...basePart("m-assistant-bedrock-png", "a1"),
634+
type: "tool",
635+
callID: "call-bedrock-png-1",
636+
tool: "read",
637+
state: {
638+
status: "completed",
639+
input: { filePath: "/tmp/a.png" },
640+
output: "Image read successfully",
641+
title: "Read",
642+
metadata: {},
643+
time: { start: 0, end: 1 },
644+
attachments: [
645+
{
646+
...basePart("m-assistant-bedrock-png", "f1"),
647+
type: "file",
648+
mime: "image/png",
649+
filename: "a.png",
650+
url: `data:image/png;base64,${png}`,
651+
},
652+
],
653+
},
654+
},
655+
] as SessionV1.Part[],
656+
},
657+
]
658+
659+
test.each(["us.anthropic.claude-sonnet-4-6", "amazon.nova-pro-v1:0", "us.meta.llama4-maverick-17b-instruct-v1:0"])(
660+
"keeps images in the tool result for %s",
661+
async (apiID) => {
662+
const result = await MessageV2.toModelMessages(history() as unknown as MessageV2.WithParts[], bedrock(apiID))
663+
expect(result).toHaveLength(3)
664+
expect(result[2]?.content).toMatchObject([
665+
{
666+
type: "tool-result",
667+
output: {
668+
type: "content",
669+
value: [
670+
{ type: "text", text: "Image read successfully" },
671+
{ type: "media", mediaType: "image/png", data: png },
672+
],
673+
},
674+
},
675+
])
676+
},
677+
)
678+
679+
test.each(["openai.gpt-oss-120b-1:0", "mistral.pixtral-large-2502-v1:0"])(
680+
"hoists images into a user message for %s",
681+
async (apiID) => {
682+
const result = await MessageV2.toModelMessages(history() as unknown as MessageV2.WithParts[], bedrock(apiID))
683+
expect(result).toHaveLength(4)
684+
expect(result[2]?.content).toMatchObject([
685+
{ type: "tool-result", output: { type: "text", value: "Image read successfully" } },
686+
])
687+
expect(result[3]).toMatchObject({
688+
role: "user",
689+
content: [
690+
{ type: "text", text: MessageV2.SYNTHETIC_ATTACHMENT_PROMPT },
691+
{ type: "file", mediaType: "image/png" },
692+
],
693+
})
694+
},
695+
)
696+
})
697+
611698
test("omits provider metadata when assistant model differs", async () => {
612699
const userID = "m-user"
613700
const assistantID = "m-assistant"

0 commit comments

Comments
 (0)