Default safety settings
By default, GemAI configures minimal content filtering:ModelBuilder.kt
Configuring safety settings
Use thesetSafetySettings() method to customize content filtering:
ModelBuilder.kt
Example: Comprehensive safety configuration
Harm categories
Gemini API provides four harm categories for content classification:Content that is rude, disrespectful, or profane. Includes bullying, intimidation, and targeted harassment.
Content that promotes or incites hatred against individuals or groups based on protected attributes (race, religion, gender, etc.).
Content that contains explicit sexual references or descriptions. Includes pornographic content.
Content that promotes, facilitates, or encourages harmful acts. Includes violence, self-harm, and illegal activities.
Block thresholds
Each harm category can be assigned a blocking threshold:Allow all content regardless of harm probability. No filtering applied.
Block content with low, medium, or high probability of harm. Most restrictive setting.
Block content with medium or high probability of harm. Recommended for most applications.
Only block content with high probability of harm. More permissive, suitable for mature audiences.
Safety setting examples
How safety settings work
Safety settings are passed to theGenerativeModel during initialization:
ModelBuilder.kt
Handling blocked content
When content is blocked, the Gemini API returns:- A safety rating indicating which category triggered the block
- The probability level (low, medium, high) for each harm category
- No generated content
You should implement error handling to gracefully inform users when content is blocked, without revealing the specific harm category.
Choosing the right settings
Use this guide to select appropriate safety settings:| Application type | Recommended threshold | Rationale |
|---|---|---|
| Kids’ apps | LOW_AND_ABOVE | Maximum protection for young users |
| General audience | MEDIUM_AND_ABOVE | Balanced safety for most users |
| Educational tools | MEDIUM_AND_ABOVE | Protect students while allowing academic discussions |
| Professional tools | ONLY_HIGH | Allow technical discussions while blocking clearly harmful content |
| Research/analysis | NONE (with warnings) | Minimal filtering for analyzing sensitive topics |
Best practices
Start conservative
Begin with stricter settings (
MEDIUM_AND_ABOVE) and adjust based on user feedback and your app’s needs.Test thoroughly
Test with various prompts, including edge cases, to understand how your settings affect responses.
Provide user controls
Consider allowing users to adjust safety settings (within acceptable ranges) based on their preferences.
Compliance considerations
Safety settings help protect users but don’t guarantee complete filtering. Consider these additional measures:
- Age verification for apps targeting specific age groups
- Terms of service that outline acceptable use
- User reporting mechanisms for problematic content
- Human moderation for high-risk applications
- Legal compliance with regulations like COPPA, GDPR, etc.
Troubleshooting
Too much content is blocked
If legitimate responses are being blocked:- Lower the threshold to
MEDIUM_AND_ABOVEorONLY_HIGH - Review your system prompts - they might be eliciting flagged content
- Test with different models (Pro models may handle edge cases better)
Harmful content is getting through
If inappropriate content appears:- Increase the threshold to
LOW_AND_ABOVE - Add explicit safety instructions to your system prompts
- Implement additional client-side filtering
- Report issues to Google’s safety team