Skip to content

fix(amazon-bedrock): preserve empty text blocks when reasoning content is present#14175

Merged
aayush-kapoor merged 2 commits intomainfrom
aayush/bedrock-filter-text
Apr 7, 2026
Merged

fix(amazon-bedrock): preserve empty text blocks when reasoning content is present#14175
aayush-kapoor merged 2 commits intomainfrom
aayush/bedrock-filter-text

Conversation

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

Background

#14071

bedrock's claude models can return assistant messages with empty text blocks sandwiched between reasoning blocks. their API strictly validates that when you replay assistant messages in conversation history, the content blocks must appear in the exact same order.

AI SDK was dropping those empty text blocks

Summary

  • Added a hasReasoningBlocks check so that empty text blocks are only skipped when the message does not contain reasoning blocks. When reasoning blocks are present, empty text blocks are preserved to maintain correct indices.
  • changed if (part.text) to if (part.text != null) : the old check silently dropped empty text blocks from the response before they ever entered the SDK's message history

Manual Verification

na

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

fixes #14071

@aayush-kapoor aayush-kapoor added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 6, 2026
@tigent tigent bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/amazon-bedrock Issues related to the @ai-sdk/amazon-bedrock provider labels Apr 6, 2026
@nelsonauner
Copy link
Copy Markdown

@aayush-kapoor
Thanks for putting together this PR!

For my own edification can you explain why you trim empty text messages at all? Is there some provider that emits many empty text messages and wants them to be trimmed?

Besides curiosity, I also want to avoid a situation where some other model provider has this exact same issue, since your fix seems to only be on bedrock but the bedrock team insists this is valid Anthropic model behavior (so may exist on anthropic first party or vertex)

@aayush-kapoor
Copy link
Copy Markdown
Collaborator Author

aayush-kapoor commented Apr 6, 2026

i see that the trimming was introduced in as a part of #7559. so there must've been a reason why it has been added (if you look at the attached issue to the original pr)

Besides curiosity, I also want to avoid a situation where some other model provider has this exact same issue, since your fix seems to only be on bedrock but the bedrock team insists this is valid Anthropic model behavior (so may exist on anthropic first party or vertex)

we prefer making fixes if/when observed to avoid bloat. and that's why reproduction is critical. if you happen to see the same issue on the anthropic provider, let us know by creating an issue!

@nelsonauner
Copy link
Copy Markdown

#7559

Wow, that's so annoying. I will bring this up to the bedrock team. IMO very poor API design to emit empty-ish blocks in text and then error if they are sent back in a text message but error if they aren't sent back in a text message between reasoning blocks.

@aayush-kapoor aayush-kapoor merged commit 24ac76f into main Apr 7, 2026
18 checks passed
@aayush-kapoor aayush-kapoor deleted the aayush/bedrock-filter-text branch April 7, 2026 14:55
vercel-ai-sdk bot pushed a commit that referenced this pull request Apr 7, 2026
…t is present (#14175)

## Background

#14071 

bedrock's claude models can return assistant messages with empty text
blocks sandwiched between reasoning blocks. their API strictly validates
that when you replay assistant messages in conversation history, the
content blocks must appear in the exact same order.

AI SDK was dropping those empty text blocks

## Summary

- Added a `hasReasoningBlocks` check so that empty text blocks are only
skipped when the message does not contain reasoning blocks. When
reasoning blocks are present, empty text blocks are preserved to
maintain correct indices.
- changed `if (part.text)` to `if (part.text != null)` : the old check
silently dropped empty text blocks from the response before they ever
entered the SDK's message history

## Manual Verification

na

## Checklist

- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

## Related Issues

fixes #14071
@vercel-ai-sdk vercel-ai-sdk bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 7, 2026
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Apr 7, 2026

✅ Backport PR created: #14189

vercel-ai-sdk bot added a commit that referenced this pull request Apr 7, 2026
…ing content is present (#14189)

This is an automated backport of #14175 to the release-v6.0 branch. FYI
@aayush-kapoor

Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Apr 7, 2026

🚀 Published in:

Package Version
ai 7.0.0-beta.69
@ai-sdk/alibaba 2.0.0-beta.21
@ai-sdk/amazon-bedrock 5.0.0-beta.25
@ai-sdk/angular 3.0.0-beta.69
@ai-sdk/anthropic 4.0.0-beta.21
@ai-sdk/assemblyai 3.0.0-beta.16
@ai-sdk/azure 4.0.0-beta.25
@ai-sdk/baseten 2.0.0-beta.19
@ai-sdk/black-forest-labs 2.0.0-beta.15
@ai-sdk/bytedance 2.0.0-beta.15
@ai-sdk/cerebras 3.0.0-beta.19
@ai-sdk/cohere 4.0.0-beta.16
@ai-sdk/deepgram 3.0.0-beta.15
@ai-sdk/deepinfra 3.0.0-beta.19
@ai-sdk/deepseek 3.0.0-beta.17
@ai-sdk/devtools 1.0.0-beta.8
@ai-sdk/elevenlabs 3.0.0-beta.15
@ai-sdk/fal 3.0.0-beta.15
@ai-sdk/fireworks 3.0.0-beta.19
@ai-sdk/gateway 4.0.0-beta.38
@ai-sdk/gladia 3.0.0-beta.15
@ai-sdk/google 4.0.0-beta.29
@ai-sdk/google-vertex 5.0.0-beta.38
@ai-sdk/groq 4.0.0-beta.18
@ai-sdk/huggingface 2.0.0-beta.19
@ai-sdk/hume 3.0.0-beta.15
@ai-sdk/klingai 4.0.0-beta.16
@ai-sdk/langchain 3.0.0-beta.69
@ai-sdk/llamaindex 3.0.0-beta.69
@ai-sdk/lmnt 3.0.0-beta.15
@ai-sdk/luma 3.0.0-beta.15
@ai-sdk/mcp 2.0.0-beta.20
@ai-sdk/mistral 4.0.0-beta.17
@ai-sdk/moonshotai 3.0.0-beta.19
@ai-sdk/open-responses 2.0.0-beta.16
@ai-sdk/openai 4.0.0-beta.25
@ai-sdk/openai-compatible 3.0.0-beta.19
@ai-sdk/otel 1.0.0-beta.15
@ai-sdk/perplexity 4.0.0-beta.18
@ai-sdk/prodia 2.0.0-beta.18
@ai-sdk/provider 4.0.0-beta.8
@ai-sdk/provider-utils 5.0.0-beta.14
@ai-sdk/react 4.0.0-beta.69
@ai-sdk/replicate 3.0.0-beta.16
@ai-sdk/revai 3.0.0-beta.16
@ai-sdk/rsc 3.0.0-beta.70
@ai-sdk/svelte 5.0.0-beta.69
@ai-sdk/togetherai 3.0.0-beta.19
@ai-sdk/valibot 3.0.0-beta.14
@ai-sdk/vercel 3.0.0-beta.19
@ai-sdk/vue 4.0.0-beta.69
@ai-sdk/xai 4.0.0-beta.28

gr2m pushed a commit that referenced this pull request Apr 7, 2026
…t is present (#14175)

## Background

#14071 

bedrock's claude models can return assistant messages with empty text
blocks sandwiched between reasoning blocks. their API strictly validates
that when you replay assistant messages in conversation history, the
content blocks must appear in the exact same order.

AI SDK was dropping those empty text blocks

## Summary

- Added a `hasReasoningBlocks` check so that empty text blocks are only
skipped when the message does not contain reasoning blocks. When
reasoning blocks are present, empty text blocks are preserved to
maintain correct indices.
- changed `if (part.text)` to `if (part.text != null)` : the old check
silently dropped empty text blocks from the response before they ever
entered the SDK's message history

## Manual Verification

na

## Checklist

- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

## Related Issues

fixes #14071
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/amazon-bedrock Issues related to the @ai-sdk/amazon-bedrock provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI SDK drops "" reasoning blocks but these MUST be included for validation

3 participants