generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Gemini does not respect the order of the properties in a schema

Open seungduk-yanolja opened this issue 1 year ago • 3 comments

Description of the bug:

Let's imagine a situation where you want an answer and a rationale from the LLM. You want it to be in a JSON format like

{
  "choice": 4,
  "rationale": "The answer is 4 because..."
}

If the LLM completes the choice property first, then the next rationale can be made up to justify the choice. However, if the rational comes first like

{
  "rationale": "The answer is 4 because...",
  "choice": 4
}

Then, the choice will be determined by rationale. It is similar to CoT.

It is important that the LLM follows the given order of the properties in an object. However, I found that Gemini SDK ignores it.

    properties: MutableMapping[str, "Schema"] = proto.MapField(
        proto.STRING,
        proto.MESSAGE,
        number=3,
        message="Schema",
    )

I think this one better to be a repeated, not a map field, to keep the order of the properties.

Actual vs expected behavior:

Gemini SDK ignores the order of the properties in an object.

Any other information you'd like to share?

No response

seungduk-yanolja avatar Sep 02 '24 14:09 seungduk-yanolja

Any updates? also there is another issue: https://github.com/google/generative-ai-go/issues/211

seungduk-yanolja avatar Sep 14 '24 18:09 seungduk-yanolja

I am having the same issue that Gemini ignores the order; it tends to hallucinate

httplups avatar Sep 30 '24 19:09 httplups

Hi. I was able to make the model follow the order by changing the field names to 'step_one' and 'step_two'. It is not ideal, but its a workaround to have an structured output with reasoning.

httplups avatar Oct 01 '24 11:10 httplups

You can give a propertyOrdering property with a list of the property names, then it will respect that order.

tfriedel avatar Oct 15 '24 22:10 tfriedel

@httplups The object it returns is unordered. It may just be printed alphabetically, the order it generates them may just be incidental.

propertyOrdering

The API could add something like that to solve the problem.

MarkDaoust avatar Oct 15 '24 22:10 MarkDaoust

Any update?

IsaSalemHame avatar Oct 30 '24 21:10 IsaSalemHame

@IsaSalemHame the API has added a propertyOrdering field. But we are slowly reducing development on this SDK.

propertyOrdering is supported in the new SDK we released with Gemini-2: google-genai - https://github.com/googleapis/python-genai

MarkDaoust avatar Feb 19 '25 14:02 MarkDaoust