fix: filter empty content blocks for Bedrock provider (#14586)
parent
8f8c74cfb8
commit
4a2a046d79
|
|
@ -51,7 +51,7 @@ export namespace ProviderTransform {
|
||||||
): ModelMessage[] {
|
): ModelMessage[] {
|
||||||
// Anthropic rejects messages with empty content - filter out empty string messages
|
// Anthropic rejects messages with empty content - filter out empty string messages
|
||||||
// and remove empty text/reasoning parts from array content
|
// 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
|
msgs = msgs
|
||||||
.map((msg) => {
|
.map((msg) => {
|
||||||
if (typeof msg.content === "string") {
|
if (typeof msg.content === "string") {
|
||||||
|
|
|
||||||
|
|
@ -1096,6 +1096,38 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
|
||||||
expect(result[0].content[1]).toEqual({ type: "text", text: "Result" })
|
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", () => {
|
test("does not filter for non-anthropic providers", () => {
|
||||||
const openaiModel = {
|
const openaiModel = {
|
||||||
...anthropicModel,
|
...anthropicModel,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue