-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Description
Feature Description
In the documentation, this example is provided:
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai('gpt-4-turbo'),
messages,
});
return result.toDataStreamResponse();
}However, req.json() returns Promise<any>. There are no guarantees here that the request contains an array of CoreMessage objects, let alone an object with a messages key.
Since there is no runtime type checking here, we have to make a request to the model and wait for it to fail before we get an error message.
I think it would be helpful if this library provided a Zod schema for CoreMessage, that way the input can be verified at runtime without having to make a request to the model.
Use Case
- Verify the request at runtime which reduces the number of bogus requests to the AI model.
- Mutate the messages array (i.e. removing tools that may only be called once in a chat session).
Additional context
Writing the CoreMessage in a Zod schema, should allow you to also generate the same types from that schema by using type CoreMessage = z.infer<typeof CoreMessageSchema> which should reduce the overhead in maintaining the schema and types.
Related microsoft/TypeScript#26188