Generating the structured output in a specific order

Is there a way to enforce Gemini models to return the structured response in a specific order?
I want the response to begin with a scratchpad, and then the rest of the JSON, but Gemini generates the response in an arbitrary order.
I tried gemini-1.5-flash, gemini-1.5-pro, gemini-2.0-flash-exp, and gemini-exp-1206 on AI Studio and via python-genai library, facing this issue with all of them.

Hello,

Welcome to the forum.

The Schemadefinition object has a property called PropertyOrdering which is a list of strings. Eventually you shall be able to specify the order of the structured output from the API. Disclaimer: I haven’t tried it myself yet but it’s in the API discovery description.

It has the following description: “Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.”

Cheers

Thanks for the tip.
Sadly that doesn’t seem to be supported in Google AI Studio (I might need to switch to Vertex AI to use this).

1 Like