Skip to content

Commit 33cd189

Browse files
committed
fix: filter empty content blocks for Bedrock provider
- Filter empty text blocks from tool-result content arrays - Skip empty text in tool output when attachments exist - Replace assistant content "" with null for openai-compatible API Fixes anomalyco#16332
1 parent 4a2a046 commit 33cd189

3 files changed

Lines changed: 55 additions & 18 deletions

File tree

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,23 @@ export namespace Provider {
12211221
}
12221222
}
12231223

1224+
// Anthropic/Bedrock via openai-compatible rejects assistant messages with content: ""
1225+
// When an assistant turn has only tool_calls and no text, the AI SDK serializes
1226+
// content as "". Replace with null so the API accepts the request.
1227+
if (model.api.npm === "@ai-sdk/openai-compatible" && opts.body && opts.method === "POST") {
1228+
const body = JSON.parse(opts.body as string)
1229+
if (Array.isArray(body.messages)) {
1230+
let changed = false
1231+
for (const msg of body.messages) {
1232+
if (msg.role === "assistant" && msg.content === "") {
1233+
msg.content = null
1234+
changed = true
1235+
}
1236+
}
1237+
if (changed) opts.body = JSON.stringify(body)
1238+
}
1239+
}
1240+
12241241
const res = await fetchFn(input, {
12251242
...opts,
12261243
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,41 @@ export namespace ProviderTransform {
5151
): ModelMessage[] {
5252
// Anthropic rejects messages with empty content - filter out empty string messages
5353
// and remove empty text/reasoning parts from array content
54-
if (model.api.npm === "@ai-sdk/anthropic" || model.api.npm === "@ai-sdk/amazon-bedrock") {
54+
if (
55+
model.api.npm === "@ai-sdk/anthropic" ||
56+
model.api.npm === "@ai-sdk/amazon-bedrock" ||
57+
model.api.npm === "@ai-sdk/openai-compatible"
58+
) {
5559
msgs = msgs
5660
.map((msg) => {
5761
if (typeof msg.content === "string") {
5862
if (msg.content === "") return undefined
5963
return msg
6064
}
6165
if (!Array.isArray(msg.content)) return msg
62-
const filtered = msg.content.filter((part) => {
63-
if (part.type === "text" || part.type === "reasoning") {
64-
return part.text !== ""
65-
}
66-
return true
67-
})
66+
const filtered = msg.content
67+
.map((part: any) => {
68+
// Filter empty text blocks nested inside tool-result content arrays
69+
if (part.type === "tool-result" && part.output?.type === "content" && Array.isArray(part.output.value)) {
70+
const cleaned = part.output.value.filter(
71+
(block: any) => block.type !== "text" || (block.text && block.text !== ""),
72+
)
73+
return {
74+
...part,
75+
output: {
76+
...part.output,
77+
value: cleaned.length > 0 ? cleaned : [{ type: "text", text: "[No output]" }],
78+
},
79+
}
80+
}
81+
return part
82+
})
83+
.filter((part: any) => {
84+
if (part.type === "text" || part.type === "reasoning") {
85+
return part.text && part.text !== ""
86+
}
87+
return true
88+
})
6889
if (filtered.length === 0) return undefined
6990
return { ...msg, content: filtered }
7091
})

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -536,19 +536,18 @@ export namespace MessageV2 {
536536
return attachment.url.startsWith("data:") && attachment.url.includes(",")
537537
})
538538

539+
const mediaBlocks = attachments.map((attachment) => ({
540+
type: "media",
541+
mediaType: attachment.mime,
542+
data: iife(() => {
543+
const commaIndex = attachment.url.indexOf(",")
544+
return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
545+
}),
546+
}))
547+
const value = [...(outputObject.text ? [{ type: "text", text: outputObject.text }] : []), ...mediaBlocks]
539548
return {
540549
type: "content",
541-
value: [
542-
{ type: "text", text: outputObject.text },
543-
...attachments.map((attachment) => ({
544-
type: "media",
545-
mediaType: attachment.mime,
546-
data: iife(() => {
547-
const commaIndex = attachment.url.indexOf(",")
548-
return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
549-
}),
550-
})),
551-
],
550+
value: value.length > 0 ? value : [{ type: "text", text: "[No output]" }],
552551
}
553552
}
554553

0 commit comments

Comments
 (0)