Output order in structured outputs

@jkirstaetter system instruction won’t work. For whatever reason, gemini API uses alphabetical order for fields in structured generation. In the vertex api, you can specify properyOrdering, as a superset of the OpenAPI schema definition. In the AI studio api, there is no such option.

Here’s my rubber-ducking in the bug tracker when I ran into this issue. Has some links to relevant docs.

https://issuetracker.google.com/issues/383595390

My hope is that the devs will change the default behavior here at some (non-breaking) point , however this ordering still seems to be the case for gemini-2.0-flash-exp in AI studio UI. IMO like a terrible idea to re-order fields alphabetically for structured generation considering the auto-regressive nature of generation.

One very ugly workaround is to prefix your field names with something to help with sorting. For example “a_reasoning” and “b_primary_categories”. Or just rename “reasoning” to “analysis” to hack it. My use case has many fields though, so it gets a little crazy to use these workarounds

2 Likes