Skip to content
New issue

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

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

Already on GitHub? # to your account

Allow null on generateObject #4614

Open
cjoecker opened this issue Jan 30, 2025 · 0 comments
Open

Allow null on generateObject #4614

cjoecker opened this issue Jan 30, 2025 · 0 comments
Labels
ai/core enhancement New feature or request

Comments

@cjoecker
Copy link

Feature Description

Sometimes, I want the AI to return null if it cannot return a reliable response. Therefore, I would like to be able to return a null.

Example:

import { generateObject } from 'ai';
import { z } from 'zod';

const { object } = await generateObject({
  model: yourModel,
  schema: z.object({
    recipe: z.object({
      name: z.string(),
      ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
      steps: z.array(z.string()),
    }).nullable(),
  }),
  prompt: `Generate a lasagna recipe with these ingredients. If there is an egg in the ingredients, return null.

Ingredients:
- tomato
- cheese
- eggs
`,
});

Use Cases

I want to get an object only if the information is reliable.

Additional context

No response

@cjoecker cjoecker added the enhancement New feature or request label Jan 30, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
ai/core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants