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

Closed
davidbarratt opened this issue Aug 8, 2024 · 3 comments
Closed

Zod schema for CoreMessage #2593

davidbarratt opened this issue Aug 8, 2024 · 3 comments
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

@regexyl
Copy link
Contributor

regexyl commented Jan 15, 2025

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

@lgrammel
Copy link
Collaborator

messages are in fact not core messages. they are messages from useChat, let's call them ui messages for now. so the validation with the core msg schema would fail / be the wrong approach. streamText automatically converts ui messages to core messages fwiw

@lgrammel
Copy link
Collaborator

Schemas are exposed in 4.1.3

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