Skip to content

Conversation

@DayuanJiang
Copy link
Owner

Summary

  • Filter out messages with empty content arrays before sending to Bedrock API
  • Fixes 400 errors: "The content field in the Message object at messages.X is empty"

Problem

The convertToModelMessages function from AI SDK can produce messages with empty content: [] arrays when:

  1. Assistant messages have only tool call parts (no text)
  2. Tool results aren't properly converted

Bedrock's Claude Converse API strictly requires non-empty content arrays.

Solution

Added a filter after convertToModelMessages to remove any messages with empty content arrays before sending to the API.

Test plan

  • Local testing with multi-turn conversations including tool calls
  • Deploy to Vercel and verify 400 errors are resolved

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.
@vercel
Copy link

vercel bot commented Nov 30, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
next-ai-draw-io Ready Ready Preview Comment Nov 30, 2025 4:06pm

Comment on lines 93 to 95
// 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

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:

  1. 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).

  2. API validation errors: Most AI APIs require proper message alternation (user → assistant → user). Removing messages can break this pattern.

  3. Lost conversation history: Messages are part of the conversation flow. Removing them loses context.

Better approach: Instead of filtering out messages, you should:

  • Ensure convertToModelMessages properly 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 convertToModelMessages is 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.

Suggested change
// 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.

Copy link
Owner Author

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:

  1. Which messages have empty content after conversion (role + content length)
  2. 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.
@DayuanJiang DayuanJiang merged commit d2d4dd0 into main Nov 30, 2025
9 checks passed
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.

2 participants