Repository navigation
fix: sanitize UIMessages before sending to LLM to prevent empty content errors - #404
Conversation
…nt errors Add sanitizeMessagesForLLM utility to handle issues that occur when conversations are interrupted mid-stream: - Filter out empty text parts (prevents Bedrock error: "text field in ContentBlock is blank") - Filter out empty reasoning parts - Add placeholder for assistant messages that have no content after cleanup - Maintain conversation structure integrity Made-with: Cursor
📝 WalkthroughWalkthroughTwo new files modify message handling in the AI chat pipeline. A new Changes
Sequence Diagram(s)sequenceDiagram
participant ChatTransport as Chat Transport
participant Sanitizer as sanitizeMessagesForLLM
participant ModelConverter as convertToModelMessages
participant LLM as Language Model
ChatTransport->>Sanitizer: messages (with potential empty parts)
activate Sanitizer
Sanitizer->>Sanitizer: Filter empty text & reasoning parts
Sanitizer->>Sanitizer: Insert placeholder if assistant message empty
Sanitizer-->>ChatTransport: sanitized messages
deactivate Sanitizer
ChatTransport->>ModelConverter: sanitized messages
activate ModelConverter
ModelConverter-->>ChatTransport: model-compatible format
deactivate ModelConverter
ChatTransport->>LLM: formatted messages
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/ai-core/src/utils.ts`:
- Around line 265-267: The current logic returns assistant messages that have
missing or empty parts and later the messages.filter at the block handling lines
around the second check (the filter at ~296-299) drops them, losing the
assistant-turn; change the handling so that when you encounter message.role ===
'assistant' and (!message.parts || message.parts.length === 0) you do not simply
return the raw message but normalize it to include an explicit empty/placeholder
part (e.g., an "interruption" or empty text part) so the assistant-turn is
preserved, and update the later filter condition (the messages.filter(...) at
~296-299) to only remove truly null/undefined entries rather than any message
with empty parts so that assistant messages with the placeholder remain in the
array; reference symbols: message, message.parts, message.role, and the
messages.filter(...) call.
|
Note Docstrings generation - SUCCESS |
Docstrings generation was requested by @lixun910. * #404 (comment) The following files were modified: * `packages/ai-core/src/chatTransport.ts` * `packages/ai-core/src/utils.ts`
Problem
When conversations are interrupted mid-stream (e.g., user cancels a request), UIMessages can end up with empty text parts. When these messages are later sent to LLM APIs (particularly Bedrock), it causes errors like "text field in ContentBlock is blank".
Summary
sanitizeMessagesForLLMutility function to handle issues that occur when conversations are interrupted mid-streamSummary by CodeRabbit