Gemini Safety Filters
When using Gemini AI for translation or speech recognition tasks, you may encounter errors such as "Response content was 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 applied, the final decision on whether to filter is still determined by Gemini's comprehensive evaluation.
Gemini API's adjustable safety filters cover the following categories. Other 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 encourages harmful acts. |
Civic Integrity | Queries related to elections. |
The table below describes the blocking settings that can be configured in the code for each category.
For example, if you set the blocking setting for the Hate Speech category to Block Few, the system will block any part with a high probability of containing hate speech. However, it will allow any part with a low probability of containing dangerous content.
Threshold (Google AI Studio) | Threshold (API) | Description |
---|---|---|
Block None | BLOCK_NONE | Always show, regardless of the likelihood of unsafe content. |
Block Few | BLOCK_ONLY_HIGH | Block when the probability of unsafe content is high. |
Block Some | BLOCK_MEDIUM_AND_ABOVE | Block when the probability of unsafe content is medium or high. |
Block Most | BLOCK_LOW_AND_ABOVE | Block when the probability of unsafe content is low, medium, or high. |
Not Applicable | HARM_BLOCK_THRESHOLD_UNSPECIFIED | Threshold not specified; uses the default threshold. |
The BLOCK_NONE
setting can be enabled in the code as follows:
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, note that even if all are set to BLOCK_NONE
, it does not mean that Gemini will allow the relevant content; it will still infer safety based on the context and filter accordingly.
How to reduce the probability of encountering security restrictions?
Generally, the flash series has more security restrictions, while the pro and thinking series models have relatively fewer. You can try switching between different models. Additionally, when potentially dealing with sensitive content, sending less content at a time and reducing the context length can also reduce the frequency of security filtering to some extent.
How to completely disable Gemini from making security judgments and allow all of the above content?
Bind a foreign credit card and switch to a paid premium account.