Skip to content
Merged
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/friendly-schools-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ai-sdk/amazon-bedrock": patch
---

fix(amazon-bedrock): preserve empty text blocks when reasoning content is present
70 changes: 70 additions & 0 deletions packages/amazon-bedrock/src/bedrock-chat-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4628,6 +4628,76 @@ describe('doGenerate', () => {
`);
});

it('should preserve empty text blocks between reasoning blocks', async () => {
server.urls[generateUrl].response = {
type: 'json-value',
body: {
output: {
message: {
role: 'assistant',
content: [
{
reasoningContent: {
reasoningText: {
text: 'thinking...',
signature: 'sig-1',
},
},
},
{ text: '' },
{
reasoningContent: {
reasoningText: {
text: 'more thinking...',
signature: 'sig-2',
},
},
},
{ text: 'The answer is 42.' },
],
},
},
usage: { inputTokens: 4, outputTokens: 34, totalTokens: 38 },
stopReason: 'stop_sequence',
},
};

const result = await model.doGenerate({
prompt: TEST_PROMPT,
});

expect(result.content).toMatchInlineSnapshot(`
[
{
"providerMetadata": {
"bedrock": {
"signature": "sig-1",
},
},
"text": "thinking...",
"type": "reasoning",
},
{
"text": "",
"type": "text",
},
{
"providerMetadata": {
"bedrock": {
"signature": "sig-2",
},
},
"text": "more thinking...",
"type": "reasoning",
},
{
"text": "The answer is 42.",
"type": "text",
},
]
`);
});

it('should extract reasoning text without signature', async () => {
server.urls[generateUrl].response = {
type: 'json-value',
Expand Down
2 changes: 1 addition & 1 deletion packages/amazon-bedrock/src/bedrock-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export class BedrockChatLanguageModel implements LanguageModelV4 {
// map response content to content array
for (const part of response.output.message.content) {
// text
if (part.text) {
if (part.text != null) {
content.push({ type: 'text', text: part.text });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,70 @@ describe('assistant messages', () => {
system: [],
});
});

it('should preserve empty text blocks when reasoning blocks are present', async () => {
const result = await convertToBedrockChatMessages([
{
role: 'user',
content: [{ type: 'text', text: 'Hello' }],
},
{
role: 'assistant',
content: [
{
type: 'reasoning',
text: 'thinking...',
providerOptions: {
bedrock: { signature: 'sig-1' },
},
},
{ type: 'text', text: '' },
{
type: 'reasoning',
text: 'more thinking...',
providerOptions: {
bedrock: { signature: 'sig-2' },
},
},
{ type: 'text', text: 'response text' },
{
type: 'tool-call',
toolCallId: 'call-123',
toolName: 'test',
input: {},
},
],
},
]);

expect(result).toEqual({
messages: [
{
role: 'user',
content: [{ text: 'Hello' }],
},
{
role: 'assistant',
content: [
{
reasoningContent: {
reasoningText: { text: 'thinking...', signature: 'sig-1' },
},
},
{ text: '' },
{
reasoningContent: {
reasoningText: { text: 'more thinking...', signature: 'sig-2' },
},
},
{ text: 'response text' },
{ toolUse: { toolUseId: 'call-123', name: 'test', input: {} } },
],
},
],
system: [],
});
});
});

describe('tool messages', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,18 @@ export async function convertToBedrockChatMessages(
const message = block.messages[j];
const isLastMessage = j === block.messages.length - 1;
const { content } = message;
const hasReasoningBlocks = content.some(
part => part.type === 'reasoning',
);

for (let k = 0; k < content.length; k++) {
const part = content[k];
const isLastContentPart = k === content.length - 1;

switch (part.type) {
case 'text': {
// Skip empty text blocks
if (!part.text.trim()) {
// Skip empty text blocks unless reasoning blocks are present
if (!part.text.trim() && !hasReasoningBlocks) {
break;
}

Expand Down
Loading