Resumable Streams
Description
Currently I'm trying to setup resumable streams in a project, I tried running it in a bare bones project as well just to confirm it was happening.
I am following the guide in the ai sdk docs which I will leave attached at the end.
import { createUIMessageStream, JsonToSseTransformStream } from "ai";
import { after } from "next/server";
import { createResumableStreamContext } from "resumable-stream";
import { convertToModelMessages, generateId, streamText, UIMessage } from "ai";
import { openai } from "@ai-sdk/openai";
const streamContext = createResumableStreamContext({
waitUntil: after,
});
export async function GET(request: Request) {
const streamContext = createResumableStreamContext({
waitUntil: after,
});
const { searchParams } = new URL(request.url);
const chatId = searchParams.get("chatId");
const recentStreamId = "random-string";
const emptyDataStream = createUIMessageStream({
execute: () => {},
});
return new Response(
await streamContext.resumableStream(recentStreamId, () =>
emptyDataStream.pipeThrough(new JsonToSseTransformStream())
)
);
}
export async function POST(request: Request) {
const { chatId, messages }: { chatId: string; messages: UIMessage[] } = await request.json();
const streamId = generateId();
// Build the data stream that will emit tokens
const stream = createUIMessageStream({
execute: ({ writer }) => {
const result = streamText({
model: openai("gpt-4o"),
messages: convertToModelMessages(messages),
});
// Return a resumable stream to the client
writer.merge(result.toUIMessageStream());
},
});
const resumableStream = await streamContext.resumableStream(streamId, () => stream);
return resumableStream.toUIMessageStreamResponse({
originalMessages: messages,
onFinish: ({ messages }) => {
saveChat({ chatId, messages });
},
});
}
This is an example of the barebone projects without having setup the actual stream id's since I am not trying to run it, and the error is related to typescript not runtime.
The typescript error Im getting is in the POST router specifically this part
const resumableStream = await streamContext.resumableStream(streamId, () => stream);
is giving me the following error:
Type 'ReadableStream<InferUIMessageChunk<UIMessage<unknown, UIDataTypes, UITools>>>' is not assignable to type 'ReadableStream<string>'.
Type 'InferUIMessageChunk<UIMessage<unknown, UIDataTypes, UITools>>' is not assignable to type 'string'.
Type '{ type: "text-start"; id: string; providerMetadata?: SharedV2ProviderMetadata | undefined; }' is not assignable to type 'string'.ts(2322)
types.d.ts(38, 53): The expected type comes from the return type of this signature.
And this part:
return resumableStream.toUIMessageStreamResponse({
originalMessages: messages,
onFinish: ({ messages }) => {
saveChat({ chatId, messages });
},
});
is giving me the following three errors:
1: 'resumableStream' is possibly 'null'.ts(18047)
2: Property 'toUIMessageStreamResponse' does not exist on type 'ReadableStream<string>'
3: Binding element 'messages' implicitly has an 'any' type.ts(7031)
The third one is kind of irrelevant since fixing the second one will fix that one but just thought I would include it to be thorough
Thank you for any help in advance.
AI SDK Version
- "@ai-sdk/openai": "^2.0.8",
- "ai": "^5.0.9",
- "resumable-stream": "^2.2.3"
Link to guide
https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-message-persistence
+1 to this having the same issue. Maybe the docs are for v4?
@JacoboGanon docs seem to be outdated I saw this commit in their ai-chatbot a month ago and it fixed the issue for me
https://github.com/vercel/ai-chatbot/commit/256305d19fb82109ab135fb4b3b6b65a77bc2403
That works, thank you for the link to the ai chatbot it helped me solve a lot of problems from migrating to ai sdk 5 (haven't finished it yet but working towards it.)
There are more docs issues than this, too:
resumeStreamnow appears to callGET /api/chat/{streamId}/stream, but this is undocumented (the docs say to put theGEThandler on/api/chat). No documentation on how to change this, either -prepareReconnectToStreamRequestis undocumented and the JSDoc comment does not appear to be accurate- The implementation in the docs does not actually resume the stream if it's still going on.
- The comments in the doc examples still talk about 'Data Streams', which is a redundant v4 concept
resume, a property onuseChat, appears to be undocumented
@mattpocock it also took me some time to be able to implement resumable stream with regenerate (both on user message and AI message) there were a lot of custom things that had to e done in order to resume streams from regenerate
@mattpocock it also took me some time to be able to implement resumable stream with regenerate (both on user message and AI message) there were a lot of custom things that had to e done in order to resume streams from regenerate
Is there a way we could do implement this in a client page? In both docs and chatbot template, the pages are server. So keeping empty dependency array in useEffect in main chat component works.
we are working on updating the docs regarding resumability. please check out this example in the meantime: https://github.com/vercel/ai/tree/main/examples/next
Not working with EXPO client implementation
Identify issue type
Identified type: Documentation
Confidence: High
Rationale: The issue centers on outdated or incorrect documentation for resumable streams rather than requesting a new SDK capability or reporting a runtime SDK failure. The comments and maintainer response frame the needed resolution as documentation updates.
Evidence:
- Reporter says they are following the AI SDK docs guide for resumable streams and the shown code produces TypeScript errors with AI SDK 5 packages.
- A commenter says “docs seem to be outdated” and links to an ai-chatbot commit that fixed the issue.
- Another commenter lists multiple documentation problems around resumable streams, including undocumented routes/properties and v4 terminology in doc examples.
- Maintainer says they are working on updating the docs regarding resumability and points to an example in the meantime.
The resumable streams guide now covers the current POST/GET setup, resume, resumeStream(), and prepareReconnectToStreamRequest.