I want that the “reasoning” key should get filled first and then the “primary_categories”. But when i stream the response, i am observing that the opposite is happening.
I want reasoning key to be filled first so that i can get the benefit of chain of thought as compared to the model justifying the categories afterwards. So my question is:
How is the output order decided? Do the keys undergo lexical sorting? Because when i replaced “reasoning” with “explanation”, then “explanation” was getting filled first.
In gemini is the structured outputs always guaranteed (like openai does by using cfg)? If some grammar rules are being used, i dont think the order can be controlled by prompting.
@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.
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
Just tried out Gemini 1206 with a prompt used in OpenAI models in our production. I just noticed the same, structured outputs seems to order properties alphabetically and it totally wrecks the output. I hope this gets fixed, not much point in evaluating switching LLM provider with this huge flaw. Structured outputs are foundational for our products.