ResponseSchema and JSON Schema specs of "type" as array

Hi there,

I’m facing an issue regarding the generation of JSON Schema used as value of GenerationConfig.ResponseSchema given the following scenario. The class is defined like this

class Recipe {
    public string Name { get; set; }
}

Which is then passed into the property as a list / array in order to retrieve multiple suggestions from the Gemini API.

var generationConfig = new GenerationConfig()
{
    ResponseMimeType = "application/json",
    ResponseSchema = new List<Recipe>()
};

The generated output looks like this

{
  "model" : "models/gemini-1.5-pro-latest",
  "contents" : [ {
    "role" : "user",
    "parts" : [ {
      "text" : "List a few popular cookie recipes."
    } ]
  } ],
  "generationConfig" : {
    "responseMimeType" : "application/json",
    "responseSchema" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "name" : {
            "type" : [ "string", "null" ]
          }
        },
        "required" : [ "name" ]
      }
    }
  }
}

Specifying the property Name as a nullable string. This is conform with the specs of type according to JSON Schema - Type-specific keywords allowing instances that can be of multiple primitive types.

The type keyword may either be a string or an array:
** If it’s a string, it is the name of one of the basic types above.*
** If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, the JSON snippet is valid if it matches any of the given types.*

See also JSON Schema: A Media Type for Describing JSON Documents and following paragraphs.

Currently, the Gemini API returns an HTTP 400 Bad Request with this information.

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"type\" at 'generation_config.response_schema.items.properties[0].value': Proto field is not repeating, cannot start list.\nInvalid JSON payload received. Unknown name \"type\" at 'generation_config.response_schema.items.properties[1].value': Proto field is not repeating, cannot start list.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "generation_config.response_schema.items.properties[0].value",
            "description": "Invalid JSON payload received. Unknown name \"type\" at 'generation_config.response_schema.items.properties[0].value': Proto field is not repeating, cannot start list."
          },
          {
            "field": "generation_config.response_schema.items.properties[1].value",
            "description": "Invalid JSON payload received. Unknown name \"type\" at 'generation_config.response_schema.items.properties[1].value': Proto field is not repeating, cannot start list."
          }
        ]
      }
    ]
  }
}

Indicating that the value of type cannot be a list/array of primitives types.

This doesn’t align with the specs of JSON Schema.

Changing the type value to a single primitive type returns an HTTP 200 OK.

{
  "model" : "models/gemini-1.5-pro-latest",
  "contents" : [ {
    "role" : "user",
    "parts" : [ {
      "text" : "List a few popular cookie recipes."
    } ]
  } ],
  "generationConfig" : {
    "responseMimeType" : "application/json",
    "responseSchema" : {
      "type" : "array",
      "items" : {
        "type" : "object",
        "properties" : {
          "name" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

and the result as expected.

{
  "candidates" : [ {
    "content" : {
      "parts" : [ {
        "text" : "[{\"name\": \"Chocolate Chip Cookies\"}, {\"name\": \"Peanut Butter Cookies\"}, {\"name\": \"Oatmeal Raisin Cookies\"}, {\"name\": \"Snickerdoodles\"}, {\"name\": \"Shortbread Cookies\"}]"
      } ],
      "role" : "model"
    },
    "finishReason" : "STOP",
    "avgLogprobs" : -0.009499770402908326
  } ],
  "usageMetadata" : {
    "promptTokenCount" : 8,
    "candidatesTokenCount" : 45,
    "totalTokenCount" : 53
  },
  "modelVersion" : "gemini-1.5-pro-002"
}

Someone else observing this issue?
Right now, this seems to be an issue with the Gemini API not accepting a conforming JSON Schema with multiple primitive types as the type definition of a property.

Assistance please. Thanks.

Cheers, JoKi

Adding to this observation on the API side, using the same response schema in AIS gives me the following error message on Save.

Sorry to say but this seems to be a short-coming on Gemini’s side regarding the interpretation of JSON Schema specifications.

Similarly, I’m getting HTTP 400 Bad Request responses when other keys, like eg. “readOnly” and others are used in the schema definition.

Tagging @GUNAND_MAYANGLAMBAM :wink:

1 Like

Hey @jkirstaetter , I reproduced the issue on my end and got the same error as you.

Let me check this with the team.

1 Like

Hi,
Thanks for validating this issue.

FYI, I wrote a bit more background information in a blog this morning.

This is mainly related to how .NET generates JSON schema out of the box.

The additional keyword readOnly as mentioned in the blog is also part of the JSON Schema annotations and has been added in the draft 7

Perhaps interesting to elaborate on this, too?

Cheers, JoKi

1 Like