Gemini Safety Filter Blocked Response
Your response has finish_reason: "SAFETY" and empty content — Gemini's safety system blocked either the prompt or the output. Check prompt_feedback.block_reason for prompt blocks or safety_ratings on the candidate for output blocks. Handle gracefully in your UX.
finish_reason === "SAFETY"Category: SafetyCommon cause: Content policy violationLast tested: Nov 15, 2026Two block types: (1) prompt block — response has no candidates, check prompt_feedback.block_reason; (2) output block — candidate exists but empty content, finish_reason: "SAFETY", check candidate.safety_ratings. Adjust safety_settings thresholds for legitimate use cases, or rephrase to avoid triggers.
The Symptoms You're Seeing
# Prompt block — no candidates at all
response.candidates = []
response.prompt_feedback.block_reason = "SAFETY"
response.prompt_feedback.safety_ratings = [
{"category": "HARM_CATEGORY_HARASSMENT", "probability": "HIGH"},
...
]
# Output block — candidate exists, empty content
response.candidates[0].finish_reason = "SAFETY"
response.candidates[0].content = None # or empty parts
response.candidates[0].safety_ratings = [
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "MEDIUM", "blocked": true},
...
]
# Attempting response.text raises:
ValueError: Cannot get text from candidate with no valid Parts.Harm Categories & Block Reasons
| Category | Meaning | Typical triggers |
|---|---|---|
HARM_CATEGORY_HARASSMENT | Targeting individuals or groups | Personal attacks, bullying language |
HARM_CATEGORY_HATE_SPEECH | Slurs, discrimination | Protected class targeting |
HARM_CATEGORY_SEXUALLY_EXPLICIT | Sexual content | Explicit descriptions |
HARM_CATEGORY_DANGEROUS_CONTENT | Instructions for harm | Weapons, drugs, self-harm details |
HARM_CATEGORY_CIVIC_INTEGRITY | Election/civic disinformation | Fake voting info, election fraud claims |
| block_reason values: SAFETY, OTHER, BLOCKLIST, PROHIBITED_CONTENT, IMAGE_SAFETY | ||
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Robust Response Handler
2Adjust Safety Thresholds
AI Studio defaults are stricter than Vertex AI for most categories. Vertex allows finer configuration and lower thresholds by default in enterprise contexts. If migrating, expect different block rates.
3User-Facing Remediation
Preventing This Error Going Forward
- Always check for empty candidates first. Prompt blocks return no candidates.
- Never call response.text without checking finish_reason. Raises on blocks.
- Adjust safety_settings for your domain context. Medical, education, security research.
- Log block category + probability. Tune thresholds from data.
- Show user-friendly, non-scary messages. Not "ERROR" — explain what to do.
- Provide crisis resources for self-harm context. Don't just block silently.
- Never set all thresholds to BLOCK_NONE. Legal + reputational risk.
Researcher · AI Error Hub
Frequently Asked Questions
You can set most categories to BLOCK_NONE — but Google's core content moderation (CSAM, extreme violence, etc.) remains regardless. Setting NONE is intended for specific approved use cases; document why in your code and consider legal/PR risk.
Yes for input tokens. Output tokens generated before the block also billed. Prompt blocks (no generation) typically bill only input.
Input and output moderation are independent. Model may generate content that scores higher than the input on some category. Common in creative writing where scenes escalate. Adjust output category thresholds if legitimate.
No — RECITATION means model output closely reproduced copyrighted training material and was blocked separately. Different from SAFETY (content policy). Can't be disabled via safety_settings.
Aimed at election-specific disinformation (fake voting locations, fraud claims), not general political discussion. Normal political conversation isn't typically blocked. Adjust category threshold if false positives.
Related Errors
Ship Safe Gemini Applications
Weekly deep dives on safety, moderation, and responsible AI. 12,000+ developers subscribe.
Subscribe Free →