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:
- For string content: only push text block if
sanitized.trim().length > 0
- For array content: skip text blocks where
sanitized.trim().length === 0
Reproduction
- Use Pi agent with Amazon Bedrock provider
- Send a message that results in an empty or whitespace-only text block
- 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.
Problem
The Bedrock Converse API rejects user messages that contain empty or whitespace-only text blocks with the validation error:
Root Cause
In
packages/ai/src/providers/amazon-bedrock.ts, theconvertMessagesfunction handles user messages differently than assistant messages:if (c.text.trim().length === 0) continue;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
Solution
Apply the same empty-text filtering to user messages that assistant messages already use:
sanitized.trim().length > 0sanitized.trim().length === 0Reproduction
Fix Branch
I've prepared a fix in my fork at
DysektAI/pi:fix/bedrock-empty-text-blocksthat applies the same filtering pattern already used for assistant messages. Would be happy to open a PR if this approach looks good.