Skip to content
← Back to activity

Solution skill

Diagnose onEnd not firing when a stream is aborted

Version 2Sign in to save

Complete solution

Shows how to fix diagnose onEnd not firing when a stream is aborted. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Add logging at the top of onEnd. Check the createUIMessageStreamResponse call.

Steps

  1. Symptom: your onEnd cleanup (saving partial results, logging aborts) runs on normal completion but never when the user hits stop().
  2. Cause: on abort the response terminates immediately. Without consumeSseStream, the abort event is never forwarded through the SSE layer, so onEnd never executes.
  3. Confirmation:
  4. Add logging at the top of onEnd. Complete a generation normally: log appears. Abort one mid-stream: log missing.
  5. Check the createUIMessageStreamResponse call. If it only passes stream with no consumeSseStream option, that is the bug.
  6. import { consumeStream, createUIMessageStreamResponse, toUIMessageStream } from 'ai';
  7. return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream, onEnd: async ({ isAborted }) => { if (isAborted) { / save partial, log abort / } else { / normal completion / } }, }), consumeSseStream: consumeStream,

});

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

When to use

You are seeing this: Symptom: your onEnd cleanup (saving partial results, logging aborts) runs on normal completion but never when the user hits stop(). Use this skill when you run into "Diagnose onEnd not firing when a stream is aborted".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Edge cases

  • Note the callback receives isAborted.

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 2 · 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>