AIAI Club
← Back to articles
ENGLISH GUIDE

Safety Filter Overblocking (Safety Blocked): How Do I Turn Off Output Being Truncated to SAFETY?

Are normal code and medical Q&A frequently triggering safety blocks? This step-by-step guide shows you how to configure BLOCK_NONE in the SDK, eliminate false positives, and unlock the model's full output capability.

1. The False-Positive Problem in Many Serious Domains

When handling medical case analysis, cybersecurity offensive/defensive testing, or criminal law statutes, Google's overly sensitive safety filter often cuts off legitimate output and returns finish_reason: SAFETY.

2. Code Implementation to Disable Blocking

from google.generativeai.types import HarmCategory, HarmBlockThreshold
safety_settings = {
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
}
model = genai.GenerativeModel('gemini-1.5-flash', safety_settings=safety_settings)
By explicitly setting all blocking thresholds to BLOCK_NONE, you can completely remove the restrictions!

This English translation is based on a Chinese source article. Prices are approximate where stated and conditions should be confirmed with the official provider or seller.