Skip to content

fix(gemini): handle safety-blocked metadata#2353

Merged
mehtarac merged 1 commit into
strands-agents:mainfrom
he-yufeng:fix/gemini-safety-usage-metadata
May 28, 2026
Merged

fix(gemini): handle safety-blocked metadata#2353
mehtarac merged 1 commit into
strands-agents:mainfrom
he-yufeng:fix/gemini-safety-usage-metadata

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Summary

Fixes #2347.

This makes the Gemini adapter tolerate safety-blocked responses where Google returns usage_metadata with missing token counts. It also maps Gemini's SAFETY finish reason to guardrail_intervened, matching the guardrail stop reason used elsewhere in Strands.

Changes

  • Default missing Gemini token counts to zero before computing usage metadata.
  • Clamp computed output tokens at zero for defensive consistency.
  • Map SAFETY message stops to guardrail_intervened.
  • Add regression coverage for metadata-only safety-blocked responses.

To verify

Run from strands-py:

uv run pytest tests/strands/models/test_gemini.py -q --basetemp .tmp/pytest-2347 -p no:cacheprovider
uv run ruff check src/strands/models/gemini.py tests/strands/models/test_gemini.py
python -m py_compile src/strands/models/gemini.py tests/strands/models/test_gemini.py
git diff --check

Local result on Windows: 56 passed.

@zastrowm zastrowm self-requested a review May 28, 2026 15:49
@mehtarac mehtarac enabled auto-merge (squash) May 28, 2026 15:50
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mehtarac mehtarac merged commit 8bccf57 into strands-agents:main May 28, 2026
20 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] GeminiModel crashes with TypeError when safety_settings block content (None usage_metadata)

2 participants