Responsible AI with Gemini API in Vertex AI: Safety ratings and thresholds
Source notebook
Repo path:
gemini/responsible-ai/gemini_safety_ratings.ipynb· Open on GitHub · intro
Shows how to inspect Gemini safety ratings and set stricter safety thresholds in Vertex AI.
Summary
This notebook teaches how to call the Gemini API in Vertex AI with the Google Gen AI SDK and inspect returned safety ratings. It demonstrates generating content, displaying category, probability, severity, finish reason, and prompt feedback, then applying safety settings to block low-and-above risk content. The workflow covers project setup, client creation, deterministic generation config, streaming responses, and interpreting blocked outputs.
Key code patterns
Create Vertex AI GenAI client
from google import genai
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION,
)Uses the Google Gen AI SDK against Vertex AI with project and region configuration.
Deterministic generation config
generation_config = GenerateContentConfig(
temperature=0,
top_p=0.1,
top_k=1,
max_output_tokens=1024,
seed=1,
candidate_count=1,
)Reduces response variability so safety-rating inspection is more repeatable.
Inspect safety ratings
candidate = response.candidates[0]
for rating in candidate.safety_ratings:
print(rating.category)
print(rating.probability)
print(rating.probability_score)
print(rating.severity)
print(rating.severity_score)Extracts the per-category safety metadata returned with a Gemini response.
Set strict safety thresholds
generation_config.safety_settings = [
SafetySetting(
category="HARM_CATEGORY_HARASSMENT",
threshold="BLOCK_LOW_AND_ABOVE",
)
]Shows how to configure category-specific blocking thresholds for safer outputs.
Models & APIs used
- Models: gemini-2.0-flash-001
- APIs / services: Vertex AI
- SDKs / libraries:
google-genai
When to use this
Use this pattern when validating Gemini output safety behavior and tuning content-filter thresholds for a Vertex AI application.
Gotchas & caveats
- Requires an existing Google Cloud project with the Vertex AI API enabled.
- Colab requires an explicit auth.authenticate_user() step; Vertex AI Workbench does not.
- The notebook uses billable Vertex AI components.
- response.text is empty when content filters block the output.
- The notebook sets LOCATION from GOOGLE_CLOUD_REGION or defaults to us-central1.
Best practices
- Inspect safety_ratings instead of relying only on generated text.
- Test prompts against safety categories before deployment.
- Set safety thresholds according to business policies and use case needs.
- Use low-variability generation settings when comparing safety behavior.
- Check finish_reason to understand why generation stopped.
Related
- Concepts: Responsible AI · Getting Started
- Entities: Vertex AI · Google GenAI SDK · Gemini
- Area: Gemini Notebooks
- Best practices: Responsible AI - Best Practices · Getting Started - Best Practices