oneOf in response_schema

I’m using the following response_schema:

"response_schema": {
                    "type": "ARRAY",
                    "items": {
                          "one_of": [
                            {
                                "type": "OBJECT",
                                "properties": {
                                    "stem": {"type": "STRING"}
                                },
                                "required": "stem",
                            },
                            {
                                "type": "OBJECT",
                                "properties": {
                                    "stimuli": {"type": "STRING"}
                                },
                                "required": "stimuli",
                            }
                        ]
                    }
                }

And I’m getting the following error:

GenerateContentRequest.generation_config.response_schema.items.type: must be specified when not using one_of

But as you can see, I’m already using one_of. What I’m doing wrong? Does Gemini API even support oneOf feature of json schema?

3 Likes

Hi,

Welcome to the forum.

Hm, it looks like your schema is missing a closing square bracket “]” to close the “oneOf” array.

Hi, my bad, I lost it during formatting in this post, thnak you for noticiing. But the issue stays the same.

It’s relatively easy to generate an exception:

 google.protobuf.json_format.ParseError: Message type "google.cloud.aiplatform.v1beta1.Schema" has no field named "$defs" at "Schema".
 Available Fields(except extensions): "['type', 'format', 'title', 'description', 'nullable', 'default', 'items', 'minItems', 'maxItems', 'enum', 'properties', 'propertyOrdering', 'required', 'minProperties', 'maxProperties', 'minimum', 'maximum', 'minLength', 'maxLength', 'pattern', 'example', 'anyOf']"

It kindly provides all fields interpreted, and oneOf is not invited to this party.

3 Likes

Thank you for the additional information :+1:

Error message you posted mentions any_of as allowed option. I tried it and the result error the same as when I tried one_of:

        import requests

        url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent"
        api_key = self.api_key  # Replace with your actual API key
        headers = {
            "Content-Type": "application/json"
        }
        payload = {
            "contents": [{
                "parts": [
                    {"text": "Generate some questions"}
                ]
            }],
            "generationConfig": {
                "response_mime_type": "application/json",
                "response_schema": {
                    "type": "ARRAY",
                    "items": {
                        "any_of": [
                            {
                                "type": "OBJECT",
                                "properties": {
                                    "stem": {"type": "STRING"}
                                },
                                "required": "stem",
                            },
                            {
                                "type": "OBJECT",
                                "properties": {
                                    "stimuli": {"type": "STRING"}
                                },
                                "required": "stimuli",
                            }
                        ]
                    }
                }
            }
        }
        response = requests.post(url, headers=headers, params={"key": api_key}, json=payload)
{'error': {'code': 400, 'message': '* GenerateContentRequest.generation_config.response_schema.items.type: must be specified when not using one_of\n', 'status': 'INVALID_ARGUMENT'}}

I tried to name the field anyOf too, no difference.

Does it mean, that it’s impossible to get a list of different types from Gemini API?

Looks like Vertex AI docs are misleading. AnyOf is not supported. I thought it’s only issue with GenAI, but its same for all forms of accessing Gemini. It is a significant downside comparing to OpenAI

2 Likes

that really “Modified by moderator”. is there a known workaround? I guess you could define a separate function for each type in anyOf but that is just ridiculous.

Missing support for anyOf is just obscene

2 Likes

@GUNAND_MAYANGLAMBAM would you know if support for this is on your roadmap? this info would be really helpful

1 Like

@GUNAND_MAYANGLAMBAM i saw your thumbs up reaction, so I take it that this support is on your roadmap. can you also kindly provide us with an ETA? anyOf support is extremely critical for my use cases

Hey @dabbbulaaax , I can’t provide an ETA, but I have escalated this issue to the team.

@GUNAND_MAYANGLAMBAM appreciate it! pls keep us up to date

@GUNAND_MAYANGLAMBAM congrats on the GA launch of gemini 2.0 flash! Very excited to use it but cant until anyOf is supported. Would you happen to have any updates?

Also a blocker for us. We want to move some of our function calling workflows to Gemini and our schemas use anyOf / oneOf.

Having reliable structured output is a must have for any production ready application with LLMs. The anyOf type absolutely needs to be supported. It is quite misleading that anyOf is claimed to be supported here:

Hope this can be resolved soon.

1 Like

thats a documentation for vertex AI. AFAIK, vertex AI and gemini AI are separate products

@dabbbulaaax Thanks for the comment, you are in fact correct, the anyOf type is supported with vertex ai.

Here’s a working example:

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "google-genai",
# ]
# ///
from google import genai
from google.genai.types import GenerateContentConfig
from pydantic import BaseModel

client = genai.Client(vertexai=True)
model_id = "gemini-2.0-flash-001"


class A(BaseModel):
    a: str


class B(BaseModel):
    b: int


class C(BaseModel):
    value: A | B


response = client.models.generate_content(
    model=model_id,
    contents="The number is 42",
    config=GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=C,
    ),
)


print(response.text)

Note that it fails if you put client = genai.Client(vertexai=False).

@Vincent_Min if you do client = genai.Client(vertexai=True), does your billing and rate limits go through vertex AI or gemini ai?

@GUNAND_MAYANGLAMBAM do you happen to have any updates on this? I cant use Gemini through Vertex AI because, unlike Google AI Studio, Vertex AI does not guarantee rate limits (it uses a dynamic shared quota).

1 Like

@GUNAND_MAYANGLAMBAM hi, would you happen to have any updates?

Hi,

anyOf has been added to the Schema definition. Maybe this already helps?

Cheers.