"Gemini responseSchema 400 invalid argument: Flutter fix"
"Your schema looks valid, Gemini rejects it with 400 INVALID_ARGUMENT before generating a single token. The cause is the schema-subset rule: maxItems, minLength, $ref, and friends are illegal. Here is the before/after Dart fix for firebase_ai."
Seeing "gemini responseSchema 400 invalid argument flutter" in your logs? Strip every JSON Schema keyword outside the supported subset — maxItems , minItems , maxLength , minLength , $ref , additionalProperties — and enforce those constraints in your prompt or code instead. The failure happens before Gemini generates a single token: it's the schema compiler refusing your schema, not your prompt misbehaving. Here's the full fix in Dart with firebase_ai . What the error looks like: "gemini responseSchema 400 invalid argument flutter" You ask for guaranteed JSON with firebase_ai and get a 400 back instead of an answer: final model = FirebaseAI.googleAI().generativeModel( model: 'gemini-2.5-flash', generationConfig: GenerationConfig( responseMimeType: 'application/json', responseSchema: Schema.object( properties: { 'summary': Schema.string(description: 'One-line summary'), 'items': Schema.array( items: Schema.object( properties: { 'title': Schema.string(), 'score': Schema.number(), }, ), ), }, ), ), ); final response = await model.generateContent([Content.text('Extract 3 key points from: $articleText')]); // throws: 400 Request contains an invalid argument. // (message varies; "Invalid JSON payload received. Unknown name ..." on raw REST) The maddening part: this exact schema shape works fine as a plain OpenAPI or JSON Schema document. It's Gemini-specific — and it fails *before* generation starts, so no retry, temperature tweak, or prompt change will ever fix it. Why it happens Gemini's structured output isn't "the model tries to write JSON." It's constrained decoding : your schema gets compiled into an automaton that forces every generated token to stay inside the…