Skip to content

Conversation

@ibetitsmike
Copy link
Contributor

When a stream is interrupted before producing content, an empty placeholder message could remain in chat history. On retry, this could cause:

undefined: The content field in the Message object at messages.N is empty. Add a ContentBlock object to the content field and try again.

Root Cause

  1. Stream starts → empty placeholder appended to chat.jsonl
  2. User interrupts before any content arrives
  3. commitToHistory doesn't commit (no content) but doesn't delete the empty placeholder
  4. On retry, the empty placeholder is sent to the API → error

Solution

Two-pronged fix:

  1. Defense-in-depth filtering in transformModelMessages

    • Added filterEmptyContentMessages pass that removes any ModelMessage with empty content array or empty string content
    • Catches edge cases where upstream transforms leave empty messages
  2. Clean up empty placeholders in partialService.commitToHistory

    • When partial has no content AND there's an empty placeholder in history, delete the placeholder
    • Added historyService.deleteMessageBySequence() for targeted message deletion

Tests

  • Added tests for empty content filtering in transformModelMessages
  • Added tests for deleteMessageBySequence in historyService

Generated with mux

When a stream is interrupted before producing content, an empty placeholder
message could remain in chat history. On retry, this could cause:
'The content field in the Message object at messages.N is empty'

Two fixes applied:

1. Defense-in-depth filtering in transformModelMessages
   - Added filterEmptyContentMessages pass that removes any ModelMessage
   - with empty content array or empty string content
   - Catches edge cases where upstream transforms leave empty messages

2. Clean up empty placeholders in partialService.commitToHistory
   - When partial has no content AND there's an empty placeholder in history
   - Delete the empty placeholder via new deleteMessageBySequence method
   - Prevents empty messages from accumulating in history

Also added:
- historyService.deleteMessageBySequence() for targeted message deletion
- Tests for both the filtering and placeholder cleanup

_Generated with mux_
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.

1 participant