From 22b4ac3cec1affaeb4948ff81f899171df2ceb49 Mon Sep 17 00:00:00 2001 From: HaiyiMei Date: Mon, 24 Nov 2025 23:28:48 +1100 Subject: [PATCH] fix: handle None or empty contents in Gemini token counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds null/empty check before processing contents in GoogleAIStudioTokenCounter to prevent errors when contents is None or empty. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- litellm/llms/gemini/count_tokens/handler.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/litellm/llms/gemini/count_tokens/handler.py b/litellm/llms/gemini/count_tokens/handler.py index 4d6c7fd88649..fdb77452d4c8 100644 --- a/litellm/llms/gemini/count_tokens/handler.py +++ b/litellm/llms/gemini/count_tokens/handler.py @@ -30,6 +30,10 @@ def _clean_contents_for_gemini_api(self, contents: Any) -> Any: from google.genai.types import FunctionResponse + # Handle None or empty contents + if not contents: + return contents + cleaned_contents = copy.deepcopy(contents) for content in cleaned_contents: