45.1 The model ignores instructions

Overview and links for this section of the guide.

Symptom

You said "only return JSON" but it adds explanation. You said "be concise" but it's verbose. The model does its own thing.

Common Causes

  • Lost in long context: Rules at the top get forgotten
  • No examples: Model doesn't understand what you want
  • Conflicting instructions: "Be helpful" overrides "be brief"
  • Weak wording: "Try to" vs "You must"

Fixes

// Fix 1: Put critical rules at the END
const prompt = `
${contextAndExamples}

CRITICAL RULES (MUST FOLLOW):
1. Output ONLY valid JSON
2. No explanations before or after
3. If unsure, return {"error": "reason"}
`;

// Fix 2: Show, don't tell
const prompt = `
Return JSON like this example:
{"name": "Alice", "score": 95}

NOT like this (no explanations):
The answer is: {"name": "Alice", "score": 95}

User input: ${input}
`;

// Fix 3: Use response_mime_type (Gemini)
const response = await model.generateContent({
  contents: [{ role: 'user', parts: [{ text: prompt }] }],
  generationConfig: {
    responseMimeType: 'application/json'  // Forces JSON
  }
});

// Fix 4: Stronger wording
// Weak:
"Try to be concise if possible"
// Strong:
"Maximum 50 words. No exceptions."

Where to go next