Skip to content

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".

image.png

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:

CategoryDescription
HarassmentNegative or harmful comments targeting identity and/or protected attributes.
Hate SpeechRude, disrespectful, or profane content.
Sexually ExplicitContains references to sexual acts or other obscene content.
Dangerous ContentPromotes, facilitates, or enables harm.
Civic IntegrityElection-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 NothingBLOCK_NONEAlways display, regardless of the likelihood of unsafe content
Block a FewBLOCK_ONLY_HIGHBlock when there is a high probability of unsafe content
Block SomeBLOCK_MEDIUM_AND_ABOVEBlock when the likelihood of unsafe content is medium or high
Block MostBLOCK_LOW_AND_ABOVEBlock when the likelihood of unsafe content is low, medium, or high
Not ApplicableHARM_BLOCK_THRESHOLD_UNSPECIFIEDThreshold 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