Immutable solution version
Diagnose onEnd not firing when a stream is aborted
Complete solution
The toUIMessageStream onEnd callback is skipped on abort unless createUIMessageStreamResponse gets consumeSseStream: consumeStream.
Symptom: your onEnd cleanup (saving partial results, logging aborts) runs on normal completion but never when the user hits stop().
Cause: on abort the response terminates immediately. Without consumeSseStream, the abort event is never forwarded through the SSE layer, so onEnd never executes.
Confirmation:
- Add logging at the top of onEnd. Complete a generation normally: log appears. Abort one mid-stream: log missing.
- Check the createUIMessageStreamResponse call. If it only passes stream with no consumeSseStream option, that is the bug.
Fix:
import { consumeStream, createUIMessageStreamResponse, toUIMessageStream } from 'ai';
return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream, onEnd: async ({ isAborted }) => { if (isAborted) { / save partial, log abort / } else { / normal completion / } }, }), consumeSseStream: consumeStream, });
Verification: start a long generation, abort it, and confirm the onEnd log line appears with isAborted true. Then let one complete and confirm isAborted is false.
Note the callback receives isAborted. Branch on it: partial-save on abort, full-save on completion. And keep onEnd fast; heavy work belongs in a background queue.
Evidence
From the AI SDK troubleshooting docs (onEnd not called when stream is aborted entry), verified 2026-09-26: the docs show the exact fix of adding consumeSseStream: consumeStream to createUIMessageStreamResponse, with onEnd receiving { isAborted }.
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_rZFRNXAjZVpTb2QiweOZrA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Diagnose onEnd not firing when a stream is aborted <span>— Vectle</span></a>