Fastify

Learn how to use the AI TOOLKIT in a Fastify server

2 min readapi serversstreamingView source

You can use the AI TOOLKIT in a Fastify server to generate and stream text and objects to the client.

Run it locally

$ npm install ai

Examples

The examples start a simple HTTP server that listens on port 8080. You can e.g. test it using curl:

curl -X POST http://localhost:8080

<Note>

The examples use the Vercel AI Gateway. Ensure that your AI Gateway API key is

set in the AI_GATEWAY_API_KEY environment variable.

</Note>

Full example: github.com/khulnasoft/ai-toolkit/examples/fastify

### Data Stream

You can use the toDataStream method to get a data stream from the result and then pipe it to the response.

import { streamText } from 'ai-toolkit';

import Fastify from 'fastify';

const fastify = Fastify({ logger: true });

fastify.post('/', async function (request, reply) {

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

// Mark the response as a v1 data stream:

reply.header('X-Vercel-AI-Data-Stream', 'v1');

reply.header('Content-Type', 'text/plain; charset=utf-8');

return reply.send(result.toDataStream({ data }));

});

fastify.listen({ port: 8080 });

### Sending Custom Data

createDataStream can be used to send custom data to the client.

import { createDataStream, streamText } from 'ai-toolkit';

import Fastify from 'fastify';

const fastify = Fastify({ logger: true });

fastify.post('/stream-data', async function (request, reply) {

// immediately start streaming the response

const dataStream = createDataStream({

execute: async dataStreamWriter => {

dataStreamWriter.writeData('initialized call');

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

result.mergeIntoDataStream(dataStreamWriter);

},

onError: error => {

// Error messages are masked by default for security reasons.

// If you want to expose the error message to the client, you can do so here:

return error instanceof Error ? error.message : String(error);

},

});

// Mark the response as a v1 data stream:

reply.header('X-Vercel-AI-Data-Stream', 'v1');

reply.header('Content-Type', 'text/plain; charset=utf-8');

return reply.send(dataStream);

});

fastify.listen({ port: 8080 });

### Text Stream

You can use the textStream property to get a text stream from the result and then pipe it to the response.

import { streamText } from 'ai-toolkit';

import Fastify from 'fastify';

const fastify = Fastify({ logger: true });

fastify.post('/', async function (request, reply) {

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

reply.header('Content-Type', 'text/plain; charset=utf-8');

return reply.send(result.textStream);

});

fastify.listen({ port: 8080 });

bash
  1. 1curl -X POST http://localhost:8080
index.ts
ts
  1. 1import { streamText } from 'ai-toolkit';
  2. 2import Fastify from 'fastify';
  3. 3const fastify = Fastify({ logger: true });
  4. 4fastify.post('/', async function (request, reply) {
  5. 5 const result = streamText({
  6. 6 model: 'openai/gpt-4o',
  7. 7 prompt: 'Invent a new holiday and describe its traditions.',
  8. 8 });
  9. 9 // Mark the response as a v1 data stream:
  10. 10 reply.header('X-Vercel-AI-Data-Stream', 'v1');
  11. 11 reply.header('Content-Type', 'text/plain; charset=utf-8');
  12. 12 return reply.send(result.toDataStream({ data }));
  13. 13});
  14. 14fastify.listen({ port: 8080 });
index.ts
ts
  1. 1import { createDataStream, streamText } from 'ai-toolkit';
  2. 2import Fastify from 'fastify';
  3. 3const fastify = Fastify({ logger: true });
  4. 4fastify.post('/stream-data', async function (request, reply) {
  5. 5 // immediately start streaming the response
  6. 6 const dataStream = createDataStream({
  7. 7 execute: async dataStreamWriter => {
  8. 8 dataStreamWriter.writeData('initialized call');
  9. 9 const result = streamText({
  10. 10 model: 'openai/gpt-4o',
  11. 11 prompt: 'Invent a new holiday and describe its traditions.',
  12. 12 });
  13. 13 result.mergeIntoDataStream(dataStreamWriter);
  14. 14 },
  15. 15 onError: error => {
  16. 16 // Error messages are masked by default for security reasons.
  17. 17 // If you want to expose the error message to the client, you can do so here:
  18. 18 return error instanceof Error ? error.message : String(error);
  19. 19 },
  20. 20 });
  21. 21 // Mark the response as a v1 data stream:
  22. 22 reply.header('X-Vercel-AI-Data-Stream', 'v1');
  23. 23 reply.header('Content-Type', 'text/plain; charset=utf-8');
  24. 24 return reply.send(dataStream);
  25. 25});
  26. 26fastify.listen({ port: 8080 });
index.ts
ts
  1. 1import { streamText } from 'ai-toolkit';
  2. 2import Fastify from 'fastify';
  3. 3const fastify = Fastify({ logger: true });
  4. 4fastify.post('/', async function (request, reply) {
  5. 5 const result = streamText({
  6. 6 model: 'openai/gpt-4o',
  7. 7 prompt: 'Invent a new holiday and describe its traditions.',
  8. 8 });
  9. 9 reply.header('Content-Type', 'text/plain; charset=utf-8');
  10. 10 return reply.send(result.textStream);
  11. 11});
  12. 12fastify.listen({ port: 8080 });

Troubleshooting

- Streaming not working when proxied