Skip to content

fix: guard total_cached_tokens against None in Gemini metrics#208

Open
sumleo wants to merge 1 commit into
Intelligent-Internet:mainfrom
sumleo:fix/gemini-cached-tokens-none-guard
Open

fix: guard total_cached_tokens against None in Gemini metrics#208
sumleo wants to merge 1 commit into
Intelligent-Internet:mainfrom
sumleo:fix/gemini-cached-tokens-none-guard

Conversation

@sumleo

@sumleo sumleo commented Jun 17, 2026

Copy link
Copy Markdown

What

GeminiInteractions._get_metrics assigns cache_read_tokens without a null guard:

# src/ii_agent/agents/models/google/interactions.py:981
metrics.cache_read_tokens = response_usage.total_cached_tokens

total_cached_tokens is Optional[int] and comes back as None on a cache miss, so cache_read_tokens ends up None. That value is later summed in Metrics.__add__, which raises TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' when metrics are accumulated across turns.

Fix

Default to 0, matching the sibling assignments right above it (lines 976-978) and the equivalent handling in gemini.py:1439:

metrics.cache_read_tokens = response_usage.total_cached_tokens or 0

Test

Added test_cache_read_tokens_none in TestGeminiInteractionsGetMetrics, mirroring the existing test_cache_read_tokens and asserting that a None cached-token count maps to 0.

Notes

The test module currently carries a module-level pytest.skip(...) left over from an earlier refactor, so the suite is skipped in CI as-is. I confirmed the new case passes locally with the skip temporarily lifted (the whole TestGeminiInteractionsGetMetrics class is green). I left the skip untouched to keep this change scoped to the bug fix.

@sumleo

sumleo commented Jun 18, 2026

Copy link
Copy Markdown
Author

Hi @baquy96, gentle nudge on this when you have a moment. It's a small, self-contained prompt-caching fix, and I'm happy to rebase or tweak anything if that would make review easier. Thanks for the project and your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant