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

Option to split validation schema and prompt schema #4924

Open
williamlmao opened this issue Feb 18, 2025 · 0 comments
Open

Option to split validation schema and prompt schema #4924

williamlmao opened this issue Feb 18, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@williamlmao
Copy link
Contributor

Feature Description

Even if we use structured outputs, we find that sometimes the LLMs will still get enums or other values slightly wrong, causing the AI SDK to throw validation errors.

In some cases, LLMs will return "Constitution" instead of "constitution" on this schema:

const schema = z.object({
  stat: z.enum([
    'strength',
    'dexterity',
    'constitution',
    'intelligence',
    'wisdom',
    'charisma',
  ]),
});

This throws an error.

What would be nice, is if I could generateObject with this as an input schema, but then pass in a validationSchema: which is what we check the validation against. With zod, this validation schema can also help us with fixing some of the data, using zod transforms.

const schema = z.object({
  stat: z.enum([
    'strength',
    'dexterity',
    'constitution',
    'intelligence',
    'wisdom',
    'charisma',
  ]),
});

const validationSchema = z.object({
  stat: z
    .string()
    .transform((val) => val?.toLowerCase())
    .pipe(
      z.enum([
        'strength',
        'dexterity',
        'constitution',
        'intelligence',
        'wisdom',
        'charisma',
      ])
    ),
});

const { object } = await generateObject({
  model: openai('gpt-4o'),
  schema,
  validationSchema,
  prompt: `Pick a stat`,
});

If the validationSchema is not defined, it should default to schema for validation.

In the above example, even if the language model returned "stat: "cOnSTITution", the validationSchema would handle transforming that to lower case and validating it. The end result is I get the value I wanted from the call without dealing with an error.

I think this would be an incredible valuable feature in the AI SDK!

Use Cases

No response

Additional context

No response

@williamlmao williamlmao added the enhancement New feature or request label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant