Skip to content

Commit b5a9452

Browse files
committed
fix: streaming
1 parent 8c4fe28 commit b5a9452

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

docs/sections/11-improvements.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fastify.post('/chat/stream', async function (request, reply) {
133133
const { messages } = request.body as any;
134134
try {
135135
const chunks = createNdJsonStream(await fastify.chat.runWithStreaming(messages));
136-
reply.type('application/x-ndjson').send(Readable.from(chunks));
136+
return reply.type('application/x-ndjson').send(Readable.from(chunks));
137137
} catch (_error: unknown) {
138138
const error = _error as Error;
139139
fastify.log.error(error);
@@ -144,7 +144,7 @@ fastify.post('/chat/stream', async function (request, reply) {
144144

145145
This time we call the `runWithStreaming()` method instead of `run()`. One key difference here is that we don't return the response directly, but we use a new helper function `createNdJsonStream()` to create the response chunks, then send them to the client as a stream using the `Readable.from()` method.
146146

147-
Let's add this new function to our file:
147+
Let's add this new function at the bottom of your file:
148148

149149
```ts
150150
// Transform the response chunks into a JSON stream

src/backend/src/routes/root.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const root: FastifyPluginAsync = async (fastify, _options): Promise<void> => {
2121
const { messages } = request.body as any;
2222
try {
2323
const chunks = createNdJsonStream(await fastify.chat.runWithStreaming(messages));
24-
reply.type('application/x-ndjson').send(Readable.from(chunks));
24+
return reply.type('application/x-ndjson').send(Readable.from(chunks));
2525
} catch (_error: unknown) {
2626
const error = _error as Error;
2727
fastify.log.error(error);

0 commit comments

Comments
 (0)