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