-
-
Notifications
You must be signed in to change notification settings - Fork 577
fix: filter out messages with empty content arrays for Bedrock API #31
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
Conversation
The convertToModelMessages function from AI SDK can produce messages with empty content arrays when assistant messages have only tool call parts or when tool results aren't properly converted. Bedrock API rejects these with 400 errors. This fix filters out invalid messages before sending to the API.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| // Filter out messages with empty content arrays (Bedrock API rejects these) | ||
| let enhancedMessages = modelMessages.filter((msg: any) => | ||
| msg.content && Array.isArray(msg.content) && msg.content.length > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 Critical Bug: This filter could break conversation flow
Filtering out messages with empty content arrays can cause these issues:
-
Broken conversation context: If an assistant message with only tool calls is removed, you lose the tool call context. This means subsequent tool result messages will be orphaned (no corresponding assistant message with tool calls).
-
API validation errors: Most AI APIs require proper message alternation (user → assistant → user). Removing messages can break this pattern.
-
Lost conversation history: Messages are part of the conversation flow. Removing them loses context.
Better approach: Instead of filtering out messages, you should:
- Ensure
convertToModelMessagesproperly handles tool calls and results (it should already do this) - If Bedrock specifically has issues, handle the conversion at the provider level
- Check if this is actually a bug in how messages are being constructed before
convertToModelMessagesis called
The root cause might be that the UI messages aren't properly structured. Can you check what messages contains when this happens? Add logging before the conversion to debug.
| // Filter out messages with empty content arrays (Bedrock API rejects these) | |
| let enhancedMessages = modelMessages.filter((msg: any) => | |
| msg.content && Array.isArray(msg.content) && msg.content.length > 0 | |
| // Convert UIMessages to ModelMessages and add system message | |
| const modelMessages = convertToModelMessages(messages); | |
| let enhancedMessages = [...modelMessages]; |
Consider investigating the root cause before filtering. If you must filter, do it in a provider-specific way and ensure it doesn't break message sequences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points. I've added diagnostic logging to help debug the root cause while keeping the filter as a safety measure.
The new commit adds logging that captures:
- Which messages have empty content after conversion (role + content length)
- The original UI message structure before conversion (parts count and types)
This will help us understand WHY convertToModelMessages produces empty content in certain cases. The filter prevents the 400 errors while we gather more data.
Regarding conversation flow concerns:
- The empty messages being filtered are pairs of (assistant + tool) with no content
- This represents failed/incomplete tool call conversions - no semantic value is lost
- Valid messages with content are preserved, maintaining conversation coherence
Added logging to capture the original UI message structure when empty content is detected after conversion. This helps debug the root cause while the filter provides a safety net for Bedrock API compatibility.
Summary
Problem
The
convertToModelMessagesfunction from AI SDK can produce messages with emptycontent: []arrays when:Bedrock's Claude Converse API strictly requires non-empty content arrays.
Solution
Added a filter after
convertToModelMessagesto remove any messages with empty content arrays before sending to the API.Test plan