Structured Output is randomly renaming schema field names. The names returned are usually synonyms of the requested field name.
I’m having to add so much sanitization (over 200 lines) to the returned object to normalise the schema back to match the output and it’s infuriating. I’ve even tried adding to the prompt instructions such as “The response MUST match the provided schema.” but this is largely ignored.
One example:
schema requested is “score_awarded”
gemini returns “mark” or “mark_awarded” or “score” or “grade”
I also just had returned a stringified JSON object instead of an actual JSON object
Please fix Structured Output - this inconsistency is making it unworkable to use reliably.
Hello @Ian_Dunning, thank you for sharing your experience with Gemini API.
To assist us in effectively investigating and reproducing it, please provide a minimal, runnable code example, including the exact API call (cURL/snippet), the full request payload, and the complete output.
I don’t have time to create a dedicated example, and my code can’t be shared.
However, I might have fixed this myself - the culprit seemed to have been Zod.
Previously I was declaring a zod object then sending that in the AI call, just as in the recipe example on the docs page https://ai.google.dev/gemini-api/docs/structured-output?example=recipe
config: {
responseMimeType: "application/json",
responseJsonSchema: zodToJsonSchema(recipeSchema),
Eventually out of frustration I decided to try it without zod, creating the schema structure directly in this sort of manner:
config: {
responseMimeType: "application/json",
responseSchema: {
type: "array",
items: {
type: "object",
properties: {
...
Now it works and I have been able to delete all my sanitation code. The schema structure is obeyed.
Thank you for the update. It is great to hear that the Structured Output feature is working as expected.
Well, it’s not working as per your own documentation…