Replies: 2 comments 3 replies
-
Do these examples work for you? https://ai-sdk.dev/cookbook/api-servers/express |
Beta Was this translation helpful? Give feedback.
-
Hey! 👋 In AI SDK 5.0, the streaming API changed a bit. The old pipeDataStreamToResponse with its execute callback is gone, but you can still stream custom data from Express by separating three steps: Create the UI Message Stream Write your custom data Pipe the stream to Express Here’s a minimal example: import express from "express"; const app = express(); app.post("/ai", async (req, res) => { // 2️⃣ Write your custom data // 3️⃣ Pipe to Express response // Alternatively, if you need raw access: app.listen(3000, () => console.log("Server listening on 3000")); Key points: createUIMessageStream gives you both a writer and a Readable stream. pipeUIMessageStreamToResponse sets headers and closes the response automatically. If you need to transform the stream (e.g. to JSON), you can convert it using the SDK’s bridge: const dataStream = aiBridge.convertUIMessageStreamToDataStream(uiStream); This way you can keep the same streaming behavior as before without the old execute API. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In AI SDK 5.0 i can't find a way to stream custom data from express.
The examples show how to do this by calling
createUIMessageStream<T>
using thewriter
inexecute
. I couldn't find a way to copy the stream returned from this call to the expressResponse
object. For exress the docs show usingpipeUIMessageStreamToResponse<T>
to pipe the message stream to theResponse
but this API doesn't expose awriter
. In v4 this could be done withpipeDataStreamToResponse
which had anexecute
callback.Thanks,
Nic
Beta Was this translation helpful? Give feedback.
All reactions