Skip to content

Gemini Safety Filtering

When using Gemini AI for translation or speech recognition tasks, you may encounter errors such as "Response was blocked due to safety settings."

image.png

This is because Gemini has safety restrictions on the content it processes. Although the code allows for some adjustments and the most lenient "Block None" setting is applied, the final decision to filter is still made by Gemini based on a comprehensive assessment.

Gemini API's adjustable safety filters cover the following categories. Other content not listed here cannot be adjusted through code:

CategoryDescription
Harassment ContentNegative 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 encourages harmful acts.
Civic IntegrityQueries related to elections.

The following table describes the blocking settings that can be used 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 all parts with a high probability of containing hate speech content. However, it will allow any parts with a low probability of containing dangerous content.

Threshold (Google AI Studio)Threshold (API)Description
Block NoneBLOCK_NONEDisplay regardless of the likelihood of unsafe content.
Block 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.
N/AHARM_BLOCK_THRESHOLD_UNSPECIFIEDThreshold not specified, use the default threshold for blocking.

The following settings can be used in the code to enable BLOCK_NONE:

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 is important to note that even if all settings are set to BLOCK_NONE, Gemini may still not allow the content and will filter it based on its inferred safety based on the context.

How to Reduce the Probability of Safety Restrictions?

Generally, the flash series has more safety restrictions, while the pro and thinking series models have relatively fewer. You can try switching to different models. In addition, when potentially sensitive content is involved, sending less content at a time and reducing the context length can also reduce the frequency of safety filtering to some extent.

How to Completely Disable Gemini's Safety Judgment and Allow All of the Above Content?

Bind a foreign credit card and switch to a paid premium account.