Skip to content

fix(providers/amazon-bedrock): strip empty text content to avoid API errors #6255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-teachers-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/amazon-bedrock': patch
---

filter out blank text blocks
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,46 @@ describe('assistant messages', () => {
system: [],
});
});

it('should filter out empty text blocks in assistant messages', async () => {
const result = convertToBedrockChatMessages([
{
role: 'user',
content: [{ type: 'text', text: 'Hello' }],
},
{
role: 'assistant',
content: [
{ type: 'text', text: '\n\n' },
{
type: 'tool-call',
toolCallId: 'call-123',
toolName: 'test',
args: {},
},
{ type: 'text', text: ' ' },
{ type: 'text', text: 'actual content' },
],
},
]);

expect(result).toEqual({
messages: [
{
role: 'user',
content: [{ text: 'Hello' }],
},
{
role: 'assistant',
content: [
{ toolUse: { toolUseId: 'call-123', name: 'test', input: {} } },
{ text: 'actual content' },
],
},
],
system: [],
});
});
});

describe('tool messages', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ export function convertToBedrockChatMessages(prompt: LanguageModelV1Prompt): {

switch (part.type) {
case 'text': {
// Skip empty text blocks
if (!part.text.trim()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am making the assumption here that this is sufficient for Bedrock's definition of "blank", but it seems reasonable enough to me.

break;
}

bedrockContent.push({
text:
// trim the last text part if it's the last message in the block
Expand Down