Skip to content

Bedrock Converse API validation error: empty text blocks in user messages #4975

@DysektAI

Description

@DysektAI

Problem

The Bedrock Converse API rejects user messages that contain empty or whitespace-only text blocks with the validation error:

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

Root Cause

In packages/ai/src/providers/amazon-bedrock.ts, the convertMessages function handles user messages differently than assistant messages:

  • Assistant messages: already skip empty text blocks with if (c.text.trim().length === 0) continue;
  • User messages: push all text blocks directly without filtering, even if they're empty after sanitizeSurrogates()

When a user message contains an empty string or whitespace-only text, it creates a ContentBlock with { text: "" } or { text: " " }, which Bedrock rejects.

Impact

  • Users encounter validation errors when sending empty messages or when message content becomes empty after surrogate character sanitization
  • This was previously patched locally but broke after updates
  • Affects any workflow where user messages might be empty or whitespace-only

Solution

Apply the same empty-text filtering to user messages that assistant messages already use:

  1. For string content: only push text block if sanitized.trim().length > 0
  2. For array content: skip text blocks where sanitized.trim().length === 0

Reproduction

  1. Use Pi agent with Amazon Bedrock provider
  2. Send a message that results in an empty or whitespace-only text block
  3. Observe validation error from Bedrock Converse API

Fix Branch

I've prepared a fix in my fork at DysektAI/pi:fix/bedrock-empty-text-blocks that applies the same filtering pattern already used for assistant messages. Would be happy to open a PR if this approach looks good.

Metadata

Metadata

Assignees

Labels

inprogressIssue is being worked on

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions