refactor: Extract duplicate Gemini tool call ID cleanup logic#2438
Merged
seratch merged 3 commits intoFeb 9, 2026
Merged
Conversation
This commit extracts duplicate code for cleaning up Gemini tool call IDs into a shared utility function in ChatCmplHelpers. ## Problem Two files had identical logic to remove the "__thought__" suffix from Gemini tool call IDs: - src/agents/models/chatcmpl_stream_handler.py - src/agents/extensions/models/litellm_model.py This duplication made maintenance harder and could lead to inconsistencies. ## Solution Added a new classmethod `ChatCmplHelpers.clean_gemini_tool_call_id()` that: - Takes a tool_call_id and optional model parameter - Returns the cleaned tool call ID - Includes comprehensive documentation ## Changes 1. **chatcmpl_helpers.py**: Added `clean_gemini_tool_call_id()` method - Well-documented with docstring - Follows existing code patterns - Returns cleaned tool call ID 2. **chatcmpl_stream_handler.py**: Replaced inline logic with helper call - Reduced from 7 lines to 2 lines - Maintains same behavior 3. **litellm_model.py**: Replaced inline logic with helper call - Added ChatCmplHelpers import - Reduced from 7 lines to 2 lines - Maintains same behavior ## Context LiteLLM adds a "__thought__" suffix to Gemini tool call IDs to track thought signatures. This suffix is removed because: - It's redundant (thought_signature is in provider_specific_fields) - It causes validation errors when cross-model passing - Reference: BerriAI/litellm#16895 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Shortened first line of docstring to stay under 100 characters - Split long explanation into multiple lines
Ruff's isort check (I001) requires imports to be sorted alphabetically. The import statement was updated to: HEADERS, HEADERS_OVERRIDE, ChatCmplHelpers This ensures the import block is properly sorted and passes lint checks. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
seratch
approved these changes
Feb 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR extracts duplicate code for cleaning up Gemini tool call IDs into a shared utility function in
ChatCmplHelpers.Problem
Two files had identical logic to remove the
"__thought__"suffix from Gemini tool call IDs:src/agents/models/chatcmpl_stream_handler.py(lines 394-402)src/agents/extensions/models/litellm_model.py(lines 820-827)This duplication made maintenance harder and could lead to inconsistencies if one instance was updated but not the other.
Solution
Added a new classmethod
ChatCmplHelpers.clean_gemini_tool_call_id()that:tool_call_idand optionalmodelparameterChanges
src/agents/models/chatcmpl_helpers.pyclean_gemini_tool_call_id()method (new lines 104-121)src/agents/models/chatcmpl_stream_handler.pysrc/agents/extensions/models/litellm_model.pyChatCmplHelpersimportContext
LiteLLM adds a
"__thought__"suffix to Gemini tool call IDs to track thought signatures. This suffix is removed because:provider_specific_fields)Testing
🤖 Generated with Claude Code