Skip to content

Gemini Safety Filtering

When using Gemini AI for translation or speech recognition tasks, you may sometimes encounter errors such as "response content flagged."

image.png

This occurs because Gemini imposes safety restrictions on the content it processes. Although the code allows for some adjustments and uses the most lenient setting of "Block None," the final decision to filter content is determined by Gemini's comprehensive evaluation.

The adjustable safety filters in the Gemini API cover the following categories. Content outside these categories cannot be adjusted via code:

CategoryDescription
HarassmentNegative or harmful comments targeting identity and/or protected attributes.
Hate SpeechRude, disrespectful, or profane content.
Sexually ExplicitReferences to sexual acts or other obscene content.
Dangerous ContentContent that promotes, facilitates, or encourages harmful behavior.
Civic IntegrityQueries related to elections.

The table below explains the blocking settings that can be applied to each category in the code.

For example, if you set the blocking threshold for the Hate Speech category to Block Only High, the system will block all parts with a high probability of containing hate speech but allow any parts with a low probability of containing dangerous content.

Threshold (Google AI Studio)Threshold (API)Description
Block NoneBLOCK_NONEAlways display content, regardless of the likelihood of unsafe content.
Block Only HighBLOCK_ONLY_HIGHBlock content when the probability of unsafe content is high.
Block Medium and AboveBLOCK_MEDIUM_AND_ABOVEBlock content when the likelihood of unsafe content is medium or high.
Block Low and AboveBLOCK_LOW_AND_ABOVEBlock content when the likelihood of unsafe content is low, medium, or high.
Not ApplicableHARM_BLOCK_THRESHOLD_UNSPECIFIEDThreshold unspecified; default threshold is used for blocking.

You can enable BLOCK_NONE in the code with 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, note that even if all settings are set to BLOCK_NONE, Gemini may still filter content based on contextual safety evaluations.

How to Reduce the Likelihood of Safety Restrictions?

Generally, the flash series models have more safety restrictions, while the pro and thinking series models are relatively less restrictive. Try switching between different models.

Additionally, when dealing with potentially sensitive content, sending smaller amounts of content at a time and reducing the context length can help lower the frequency of safety filtering.

How to Completely Disable Gemini's Safety Judgments and Allow All Content?

Link a foreign credit card and switch to a monthly-paid premium account.