vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.6k stars 1.41k forks source link

Unknown name minItems - Google AI SDK and OpenAI SDK #2762

Closed BleedingDev closed 1 month ago

BleedingDev commented 1 month ago

Description

I updated to "@ai-sdk/google": "^0.0.43", and now I get error. It seems like this SDK tries to use feature that is not supported as stated in issues here: https://github.com/google-gemini/generative-ai-js/issues/200 https://github.com/google/generative-ai-docs/issues/210

Edit: Seems like this is broken with OpenAI "@ai-sdk/openai": "^0.0.51", as well. But only when structuredOutputs is true.

    model: openai('gpt-4o-mini', {
      structuredOutputs: true
    }),

Error

Invalid JSON payload received. Unknown name \\"minItems\\" at 'generation_config.response_schema.properties[1].value.items.properties[1].value': Cannot find field.\\nInvalid JSON payload received. Unknown name \\"minItems\\" at 'generation_config.response_schema.properties[1].value': Cannot find field.",\n

Code example

'use server'
import { generateObject } from 'ai'
import { google } from '@ai-sdk/google'
import { z } from 'zod'
const getQuizSchema = (amountQuestions: number, amountAnswers: number) => {
  const answerSchema = z.object({
    text: z.string(),
    isCorrect: z.boolean(),
  })

  const taskSchema = z.object({
    question: z.string(),
    answers: z.array(answerSchema).length(amountAnswers),
  })

  const quizSchema = z.object({
    chapterName: z.string(),
    tasks: z.array(taskSchema).length(amountQuestions),
  })
  return quizSchema
}

export async function generateQuiz(
  content: string,
  {
    outputLang = 'English',
    amountQuestions = 5,
    amountAnswers = 4,
    allowMultiple = false,
  }: { outputLang?: string; amountQuestions?: number; amountAnswers?: number; allowMultiple?: boolean },
) {
  const schema = getQuizSchema(amountQuestions, amountAnswers)
  const { object } = await generateObject({
    model: google('models/gemini-1.5-flash-latest'),
    schema: schema,
    system: trim`
...
    `,
    prompt: trim`
...
    `,
  })

  return object
}

Additional context

Google

Broken in both tool and json modes.

OpenAI

Broken when structuredOutputs is true.

lgrammel commented 1 month ago

The provider JSON specs don't fully support the OpenAPI 3.0 spec (Google) or, as it seems, JSON Schema 7 (OpenAI) fully.

In the OpenAI case, just disable structured outputs.

In the Google case, I'm working on a workaround: #2743