Issue with Key Ordering in Controlled Generation on Gemini 1.5

Hi everyone,

I noticed an issue with the key ordering in controlled generation when working with Gemini 1.5 (flash/pro/001/002). According to the documentation on controlled generation (example 2):

Keys such as “rating” and “flavor” are supposed to be generated in the order defined by the schema. However, when running this on Gemini 1.5 API (flash/pro/001/002), the keys are generated in alphabetical order instead.

This behavior becomes problematic for Chain-of-Thought (CoT) prompting, such as:

  • “country_of_origin_reasoning”: “…”
  • “country_of_origin_iso_code”: “xx”

In this scenario, the key country_of_origin_iso_code is generated before country_of_origin_reasoning , which disrupts the intended logic flow. This essentially makes it impossible to use controlled generation effectively in this context.

Is anyone else experiencing this issue, or does anyone have a workaround?

Thanks, /JT

Same issue reported here:

I found a workaround and reported it some time ago: Make Gemini JSON output better: Pydantic schemas and fields order | by Andrea Santoro | Sep, 2024 | Medium

As of now, gemini API should support the propertyOrdering keyword in the schema but the docs are a bit misleading and it’s not clear which version implements it. Installing the latest version doesn’t seem to work either.
It works in the VertexAI playground but for some reason it doesn’t work in Cloud Functions