Next.js Discord

Discord Forum

Vercel AI - JSON output error

Unanswered
Gouty oak gall posted this in #help-forum
Open in Discord
Gouty oak gallOP
When using the example from the docs, it recieves an error. Any idea how to get json output or what im doing wrong to work with the Vercel AI and openai?

    const { object } = await generateObject({
      model: openai('gpt-4o-mini'),
      mode: 'json',
      output: 'object',
      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()),
        }),
      }),
      prompt: 'Generate a lasagna recipe.',
    });

ERROR [ExceptionsHandler] NoObjectGeneratedError [AI_NoObjectGeneratedError]: No object generated: response did not match schema.
...
    cause: ZodError: [
      {
        "expected": "object",
        "code": "invalid_type",
        "path": [
          "recipe"
        ],
        "message": "Invalid input: expected object, received string"
      }
    ]

0 Replies