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
This is hurting our use case too, gemini-2.5-pro is an excellent model; I hope the google team solves this soon. OpenAI follows this beautifully - https://platform.openai.com/docs/guides/structured-outputs/key-ordering?api-mode=chat#key-ordering
Hi,
I agree with your resentiments.
A workaround right now, would be to give the properties a prefix to indicate the order.
Cheers
