You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{openai}from'@ai-sdk/openai';import{streamText}from'ai';// Allow streaming responses up to 30 secondsexportconstmaxDuration=30;exportasyncfunctionPOST(req: Request){const{ messages }=awaitreq.json();constresult=awaitstreamText({model: openai('gpt-4-turbo'),
messages,});returnresult.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.
Feature Description
In the documentation, this example is provided:
However,
req.json()
returnsPromise<any>
. There are no guarantees here that the request contains an array ofCoreMessage
objects, let alone an object with amessages
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
Additional context
Writing the
CoreMessage
in a Zod schema, should allow you to also generate the same types from that schema by usingtype CoreMessage = z.infer<typeof CoreMessageSchema>
which should reduce the overhead in maintaining the schema and types.Related microsoft/TypeScript#26188
The text was updated successfully, but these errors were encountered: