Adapter stream(): the wrapped stream has no return(), so the source stream is never closed
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
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vercel/chat
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
X and Twitch: paragraphs inside a block quote are joined with no spacePossibly taken @abhiramshaji-cyber claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 58/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
solidjs/solid-docs#1716 ·
Maintainers usually reply within 1 day
-
bug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Progress indicated by console logs is off by onePossibly taken @marceliwac claimed this today. Openbug needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
aws-samples/aws-cdk-examples#1336 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
melgarafael/DeskcommCRM#4999 · 1 comment ·
Maintainers usually reply within 1 day
-
type: bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 2 days