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?

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.

2 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?