Structured Output Improvements announcement and actual support

With regard to the announcement in the November blog post:

Google announces support for JSON Schema and implicit property ordering in Gemini API.

I wanted to test the new structured output features, specifically $defs and $ref, for my definition, which is otherwise too lengthy to be accepted. I attempted to input my definition into AI Studio, but I encountered an error indicating that the $defs section was not recognized. I have not yet experimented with the APIs directly, as this would be rather inconvenient for debugging, and I would prefer not to invest time in a feature that is not clearly documented. However, I would like to ascertain whether the features presented in the blog are indeed available, whether AI Studio is not up-to-date, or whether I am making an error. Thanks!

Hi @Bragma , Welcome to the AI Forum!!!

To help us replicate the issue and find a fix, could you please share the steps to reproduce it along with the screenshot of the error?

Verified: Zainab Fatima

Sure:

  1. Open Google AI Studio
  2. On the right toolbar, under “Tools”, enable “Structured outputs”.
  3. Click on edit to insert a definition.
  4. Enter a sample definition using “$defs” and “$ref”, such as this.
{
  "type": "object",
  "properties": {
    "recipe_name": {
      "type": "string",
      "description": "The name of the recipe."
    },
    "prep_time_minutes": {
      "$ref": "#/$defs/prep_time_minutes"
    }
  },
  "required": ["recipe_name"],
  "$defs": {
    "prep_time_minutes": {
      "type": "integer",
      "description": "Optional time in minutes to prepare the recipe."
    }
  }
}

The schema is not recognized. See screenshot below:

This might be an AIS bug. Using the following schema with the Python SDK worked for me:


client = genai.Client(api_key="YOUR_API_KEY")

# The schema provided by the user containing $defs and $ref
schema_with_defs = {
  "type": "object",
  "properties": {
    "recipe_name": {
      "type": "string",
      "description": "The name of the recipe."
    },
    "prep_time_minutes": {
      "$ref": "#/$defs/prep_time_minutes"
    }
  },
  "required": ["recipe_name"],
  "$defs": {
    "prep_time_minutes": {
      "type": "integer",
      "description": "Optional time in minutes to prepare the recipe."
    }
  }
}

try:
    response = client.models.generate_content(
        model="gemini-3-flash-preview",
        contents="Generate a simple cookie recipe.",
        config=types.GenerateContentConfig(
            response_mime_type="application/json",
            response_schema=schema_with_defs
        )
    )
    print("Generate Content Response:")
    print(response.text)
except Exception as e:
    print("Error occurred:")
    print(e)