Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zod schema for CoreMessage #2593

Open
davidbarratt opened this issue Aug 8, 2024 · 1 comment · May be fixed by #4408
Open

Zod schema for CoreMessage #2593

davidbarratt opened this issue Aug 8, 2024 · 1 comment · May be fixed by #4408
Labels
ai/ui enhancement New feature or request

Comments

@davidbarratt
Copy link

davidbarratt commented Aug 8, 2024

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

@lgrammel lgrammel added enhancement New feature or request ai/ui labels Aug 8, 2024
@regexyl regexyl linked a pull request Jan 15, 2025 that will close this issue
@regexyl
Copy link

regexyl commented Jan 15, 2025

Ran into the same need on my end - have submitted a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/ui enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants