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

Conversation

MaxAller
Copy link

@MaxAller MaxAller commented May 9, 2025

Background

Without this, with maxSteps set, we sometimes see messages sent to the API like this:

{
  role: 'assistant',
  content: [ { text: '\n\n' }, { toolUse: [Object] } ]
},

This leads to API errors like this:

The text field in the ContentBlock object at messages.7.content.0 is blank. Add text to the text field, and try again.

This happens often enough, maybe half the time in my experience, to cause a lot of pain on our side.

Summary

This change simply filters out this useless but apparently harmful message.

Verification

I've been running with this change locally for a day and haven't seen the issue recur, and nothing else seems to have obviously broken.

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • [n/a] 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)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

Future Work

Related Issues

Fixes #6254.

…errors.

Without this, with maxSteps set, we sometimes see messages sent to the API like this:

```
{
  role: 'assistant',
  content: [ { text: '\n\n' }, { toolUse: [Object] } ]
},
```

This leads to API errors like this:

```
The text field in the ContentBlock object at messages.$n.content.0 is blank. Add text to the text field, and try again.
```

This change simply filters out this useless but apparently harmful message.
@@ -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.

@MaxAller MaxAller marked this pull request as ready for review May 9, 2025 21:59
@seannetlife
Copy link

thanks for this! i am going to use patch-package to get this in as i am desperate for the extended rates aws provides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Amazon Bedrock + streamText + maxSteps often leads to APICallError
2 participants