The behavior doesn’t reflect in AI studio.
Safety settings always act as defaults regardless of the custom parameters passed if tools=[Tool(google_search = GoogleSearch())]
is present.
For example:
from google.genai import types
from google.genai.types import Tool, GoogleSearch
response = client.models.generate_content(
model = "gemini-2.0-flash",
contents = "Some prompt that makes the model not-so-safe content",
config = types.GenerateContentConfig(
system_instruction="""
Some instruction to make the model generate not-so-safe content
""",
tools=[Tool(google_search = GoogleSearch())],
safety_settings=[
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=types.HarmBlockThreshold.BLOCK_ONLY_HIGH,
),
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold=types.HarmBlockThreshold.BLOCK_ONLY_HIGH,
),
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold=types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
),
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
)
]
)
)
Let’s assume that response was blank because of FinishReason.SAFETY
.
When checking the ratings(pprint(response.candidates[0].safety_ratings, indent=2)
), the reason is somehow:
[ SafetyRating(blocked=None, category=<HarmCategory.HARM_CATEGORY_HATE_SPEECH: 'HARM_CATEGORY_HATE_SPEECH'>, probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'>, probability_score=None, severity=None, severity_score=None),
SafetyRating(blocked=None, category=<HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: 'HARM_CATEGORY_DANGEROUS_CONTENT'>, probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'>, probability_score=None, severity=None, severity_score=None),
SafetyRating(blocked=True, category=<HarmCategory.HARM_CATEGORY_HARASSMENT: 'HARM_CATEGORY_HARASSMENT'>, probability=<HarmProbability.MEDIUM: 'MEDIUM'>, probability_score=None, severity=None, severity_score=None),
SafetyRating(blocked=None, category=<HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: 'HARM_CATEGORY_SEXUALLY_EXPLICIT'>, probability=<HarmProbability.NEGLIGIBLE: 'NEGLIGIBLE'>, probability_score=None, severity=None, severity_score=None)]
But the block threshold is set to BLOCK_ONLY_HIGH
! I doubt that this is intended. Matter of fact, it even persists with models that don’t have search grounding support at all(gemini-2.0-flash-lite
). Just declare the search tool and the safety configs are broken, even if unsupported.
The behavior doesn’t persist with search tool being disabled and works vice-versa when tool is enabled(if the threshold is set to BLOCK_LOW_AND_ABOVE
, it will still block according to default configuration, so medium and above).