Gemini Security Filtering
When using Gemini AI to perform translation or speech recognition tasks, you may sometimes encounter errors such as "Response content is flagged".
This is because Gemini has security restrictions on the content it processes. Although the code allows for some adjustments and the most lenient "Block None" setting has been made, the final decision on whether to filter is still determined by Gemini's comprehensive assessment.
Gemini API's adjustable safety filters cover the following categories, and content not listed here cannot be adjusted through code:
Category | Description |
---|---|
Harassment | Negative or harmful comments targeting identity and/or protected attributes. |
Hate Speech | Rude, disrespectful, or profane content. |
Sexually Explicit | Contains references to sexual acts or other obscene content. |
Dangerous Content | Promotes, facilitates, or enables harm. |
Civic Integrity | Election-related queries. |
The table below describes the blocking settings in the code that can be used for each category.
For example, if you set the blocking setting for the Hate Speech category to Block a few, the system will block all parts that contain a high probability of hate speech content. But it allows any parts that contain a low probability of dangerous content.
Threshold (Google AI Studio) | Threshold (API) | Description |
---|---|---|
Block Nothing | BLOCK_NONE | Always display, regardless of the likelihood of unsafe content |
Block a Few | BLOCK_ONLY_HIGH | Block when there is a high probability of unsafe content |
Block Some | BLOCK_MEDIUM_AND_ABOVE | Block when the likelihood of unsafe content is medium or high |
Block Most | BLOCK_LOW_AND_ABOVE | Block when the likelihood of unsafe content is low, medium, or high |
Not Applicable | HARM_BLOCK_THRESHOLD_UNSPECIFIED | Threshold is not specified, use the default threshold to block |
In the code, BLOCK_NONE
can be enabled through the following settings
safetySettings = [
{
"category": HarmCategory.HARM_CATEGORY_HARASSMENT,
"threshold": HarmBlockThreshold.BLOCK_NONE,
},
{
"category": HarmCategory.HARM_CATEGORY_HATE_SPEECH,
"threshold": HarmBlockThreshold.BLOCK_NONE,
},
{
"category": HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
"threshold": HarmBlockThreshold.BLOCK_NONE,
},
{
"category": HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
"threshold": HarmBlockThreshold.BLOCK_NONE,
},
]
model = genai.GenerativeModel('gemini-2.0-flash-exp')
model.generate_content(
message,
safety_settings=safetySettings
)
However, it should be noted that: Even if all are set to BLOCK_NONE
, it does not mean that Gemini will allow the relevant content to pass. It will still infer security based on the context and filter accordingly.
How to Reduce the Probability of Security Restrictions?
In general, the flash series has more security restrictions, and the pro and thinking series models have relatively few. You can try switching different models. In addition, when sensitive content may be involved, sending less content at a time and reducing the context length can also reduce the frequency of security filtering to a certain extent.
How to Completely Prohibit Gemini from Making Security Judgments and Allow All of the Above Content to Pass?
Bind a foreign credit card and switch to a paid monthly premium account