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

must be specified when not using one_of - error on structured output using zod on google provider #4153

Closed
cloudycotton opened this issue Dec 19, 2024 · 8 comments
Assignees
Labels
ai/provider bug Something isn't working

Comments

@cloudycotton
Copy link

cloudycotton commented Dec 19, 2024

Description

{
  "error": {
    "code": 400,
    "message": "* GenerateContentRequest.generation_config.response_schema.properties[evening_routines].items.type: must be specified when not using one_of\n",
    "status": "INVALID_ARGUMENT"
  }
}

This is the error I get when I try to stream a structured response using the schema below on google-experimental-1206 model.

Code example

export const Product = z.object({
  name: z.string(),
  category: z.string(),
  description: z.string(),
  key_ingredients: z.string(),
  usage_instructions: z.string(),
  precautions: z.string(),
});

export const Prescription = z.object({
  morning_routines: z.array(Product),
  evening_routines: z.array(Product),
  lifestyle_changes: z.array(z.string()),
  treatment_notes: z.string(),
});

AI provider

^4.0.20 and 1.0.10 for google provider.

const response = await streamObject({
      model: google("gemini-exp-1206"),
      messages: [], // removed for brevity
      schema: Prescription,
    });

|

Additional context

No response

@cloudycotton cloudycotton added the bug Something isn't working label Dec 19, 2024
@cloudycotton cloudycotton changed the title must be specified when not using one_of - error on structured output using zod on google provider using gemini-1206 must be specified when not using one_of - error on structured output using zod on google provider Dec 19, 2024
@shaper
Copy link
Contributor

shaper commented Dec 20, 2024

Hi there, we are looking into this, thanks for raising.

In the immediate term as a workaround, it looks like if you define separate schemas for Morning and Evening products (even if the definition is the same) it will fix the issue.

  // Define separate Product schemas for morning and evening routines
  const MorningProduct = z.object({
    name: z.string(),
    category: z.string(),
    description: z.string(),
    key_ingredients: z.string(),
    usage_instructions: z.string(),
    precautions: z.string(),
  });

  const EveningProduct = z.object({
    name: z.string(),
    category: z.string(),
    description: z.string(),
    key_ingredients: z.string(),
    usage_instructions: z.string(),
    precautions: z.string(),
  });

  const Prescription = z.object({
    morning_routines: z.array(MorningProduct),
    evening_routines: z.array(EveningProduct),
    lifestyle_changes: z.array(z.string()),
    treatment_notes: z.string(),
  });

Another workaround is to disable structured outputs:

    model: google('gemini-exp-1206', {
      structuredOutputs: false,
    }),

@shaper shaper self-assigned this Dec 20, 2024
@marko911
Copy link

I'm also getting an error on the json schema that's created from the zod when calling Gemini.

const outputSchema = z.object({
  years: z.record(
    z.string({ message: "Year must be a string" }).default("2020"),
    z.array(
      z.object({
        account: z.string(),
        amount: z.number(),
      }),
    ),
  ),
});

This somehow gets mangled before its sent in the HTTP request.

generateText error: {"temperature":0,"responseMimeType":"application/json","responseSchema":{"required":["years"],"type":"object","properties":{}}}
2025-01-10 14:11:07.241 ERROR   [blitz-rpc]     parseAnnualReport()

 AI_APICallError  * GenerateContentRequest.generation_config.response_schema.properties: should be non-empty for OBJECT type
* GenerateContentRequest.generation_config.response_schema.required[0]: property is not defined
, AI_APICallError, , https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent, [object Object], 400, [object Object], {
  "error": {
    "code": 400,
    "message": "* GenerateContentRequest.generation_config.response_schema.properties: should be non-empty for OBJECT type\n* GenerateContentRequest.generation_config.response_schema.required[0]: property is not defined\n",
    
    ```
    
    generateText error is my own log

@agamm
Copy link

agamm commented Jan 11, 2025

I'm also getting this with gemini.
output: "array",

and zod:

const answerSchema = z.object({
	id: z.string().min(1),
	value: valueSchema.or(tableValueSchema),
	hint: z.object({
		text: z.string(),
		id: z.number(),
	}),
});

edit: seems related: https://discuss.ai.google.dev/t/oneof-in-response-schema/55926/8

@lgrammel
Copy link
Collaborator

This might require several fixes. Fix for main reported bug: #4370

@lgrammel
Copy link
Collaborator

@marko911 can you trying using z.object instead of z.record?

@lgrammel
Copy link
Collaborator

@agamm or might not work - can you try a discriminated union instead?

@agamm
Copy link

agamm commented Jan 13, 2025

@lgrammel with original schema getting:

Invalid value at 'generation_config.response_s
chema.properties[0].value.items.properties[1].value.any_of[1].type' (type.googleapis.com/google.ai.genera
tivelanguage.v1beta.Type), "null"
    at eval (webpack-internal:///(action-browser)/../node_modules/.pnpm/@[email protected]_zod@
3.24.1/node_modules/@ai-sdk/provider-utils/dist/index.mjs:481:14)

after trying the discriminated union I get:

Invalid value at 'generation_config.response_s
chema.properties[0].value.items.any_of[0].properties[2].value.any_of[1].type' (type.googleapis.com/google
.ai.generativelanguage.v1beta.Type), "null"
    at eval (webpack-internal:///(action-browser)/../node_modules/.pnpm/@[email protected]_zod@
3.24.1/node_modules/@ai-sdk/provider-utils/dist/index.mjs:481:14)

Can I open this issue or another one? @lgrammel

EDIT:
using these versions:

		"@ai-sdk/google": "^1.0.16",
		"ai": "^4.0.34",

@lgrammel
Copy link
Collaborator

@agamm Yes - pls open another issue and attach the call(s) incl. schema that you are making.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/provider bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants