Skip to content

onFinish callback never fires when stream is aborted - partial AI messages lost on client disconnect #1060

Open
@ldriss

Description

@ldriss

When a user aborts a streaming AI response (client disconnect, page refresh, or manual stop), the onFinish callback never executes, causing partial AI messages to be lost forever. This breaks conversation history and
creates a poor UX.

🔄 Current Behavior


// User message - saved immediately ✅
await saveMessages([userMessage]);

const result = streamText({
  model,
  messages,
  onFinish: async ({ response }) => {
    // This NEVER executes when stream is aborted ❌
    await saveMessages([aiMessage]); 
  }
});

result.consumeStream(); // Doesn't help - still gets killed

Result: User refreshes page → only user message visible, AI response gone.

✅ Expected Behavior
Partial AI responses should be persisted even when streams are interrupted, maintaining conversation continuity.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions