Adapter stream(): the wrapped stream has no return(), so the source stream is never closed

Open
#1,170 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@abhiramshaji-cyber is already working on this.

Since Nov 16, 2026.

  • #1172 by @abhiramshaji-cyber — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, typescript
Domain
api, backend

Research direction

Start in packages/chat/src/thread.ts at handleStream() (around line 845) and inspect the wrapper iterator; also check textOnlyStream (around line 891) and takeUntilAborted() (around line 169). Reproduce with the async generator in the issue, then verify that early exit closes the source in both stream paths.

Written by the indexing model from the issue text.

Description

Bug Description

When an adapter implements stream() and stops reading early (for example it has a length limit, or it throws), the source stream passed to thread.post() is never closed. The model keeps generating tokens nobody reads.

The cause is the wrapper in handleStream() (packages/chat/src/thread.ts L845). Its iterator has next() but no return(). So when the adapter's for await exits early, return() is not forwarded, and the finally block in takeUntilAborted() (L169), which calls iterator.return() on the source, never runs. textOnlyStream (L891) has the same gap for the post-and-edit fallback.

Steps to Reproduce
let cleanedUp = false;
async function* model() {
  try { while (true) yield "word "; } finally { cleanedUp = true; }
}
adapter.stream = async (threadId, textStream) => {
  for await (const chunk of textStream) throw new Error("too long");
};
await thread.post(model()).catch(() => {});
console.log(cleanedUp); // false
Expected Behavior

cleanedUp is true: leaving the adapter's loop closes the source, as with a plain async generator.

Actual Behavior

cleanedUp stays false. The source generator stays suspended, and an AI SDK textStream keeps its request open.

Code Sample

A fix that forwards return():

return {
  next: async () => { /* unchanged */ },
  return: async (value?: unknown) => {
    await iterator.return?.(value);
    return { done: true, value: undefined };
  },
};
Chat SDK Version

4.44.0 (reproduced). Same code on main at 990411e.

Node.js Version

24.14.0

Platform Adapter

Any adapter that implements stream(). Found while building an SMS adapter that sends a streamed reply as one message and stops reading past the SMS length limit.

Additional Context

Passing an AbortSignal to the model call is a workaround, but thread.post(stream) gives the caller no hook to know the adapter stopped reading.

Dominant language
TypeScript
Stars
2.5k
Forks
355
Avg merge
2d 15h
Merged PRs (30d)
42

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vercel/chat

All issues in vercel/chat

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.