fix: filter empty content blocks for Bedrock provider (#14586)

This commit is contained in:
Tom Ryder 2026-03-12 20:13:09 -07:00 committed by GitHub
parent 8f8c74cfb8
commit 4a2a046d79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 1 deletions

View File

@ -51,7 +51,7 @@ export namespace ProviderTransform {
): ModelMessage[] {
// Anthropic rejects messages with empty content - filter out empty string messages
// and remove empty text/reasoning parts from array content
if (model.api.npm === "@ai-sdk/anthropic") {
if (model.api.npm === "@ai-sdk/anthropic" || model.api.npm === "@ai-sdk/amazon-bedrock") {
msgs = msgs
.map((msg) => {
if (typeof msg.content === "string") {

View File

@ -1096,6 +1096,38 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
expect(result[0].content[1]).toEqual({ type: "text", text: "Result" })
})
test("filters empty content for bedrock provider", () => {
const bedrockModel = {
...anthropicModel,
id: "amazon-bedrock/anthropic.claude-opus-4-6",
providerID: "amazon-bedrock",
api: {
id: "anthropic.claude-opus-4-6",
url: "https://bedrock-runtime.us-east-1.amazonaws.com",
npm: "@ai-sdk/amazon-bedrock",
},
}
const msgs = [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "" },
{
role: "assistant",
content: [
{ type: "text", text: "" },
{ type: "text", text: "Answer" },
],
},
] as any[]
const result = ProviderTransform.message(msgs, bedrockModel, {})
expect(result).toHaveLength(2)
expect(result[0].content).toBe("Hello")
expect(result[1].content).toHaveLength(1)
expect(result[1].content[0]).toEqual({ type: "text", text: "Answer" })
})
test("does not filter for non-anthropic providers", () => {
const openaiModel = {
...anthropicModel,