-
Notifications
You must be signed in to change notification settings - Fork 704
Fix LiteLLM infinite loop issue with Ollama/Gemma3 models #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix LiteLLM infinite loop issue with Ollama/Gemma3 models #166
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring updates
This enhanced version: | ||
1. Adds validation for required fields with proper defaults | ||
2. Improves JSON parsing to handle both standard JSON and Python-style strings | ||
3. Implements comprehensive error handling to prevent crashes | ||
4. Maintains compatibility with all model formats | ||
|
||
Implementation note: | ||
This function is part of the fix for an infinite loop issue that occurs when using | ||
Ollama/Gemma3 models with LiteLLM. These models sometimes return malformed JSON in | ||
function call arguments, which can cause the system to get stuck in a loop. | ||
The robust parsing ensures that even with malformed JSON, we can still extract | ||
valid arguments and prevent failures. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While looking at the semantics/style of the rest of the codebase and the fact that you have already provided a separate file documenting these changes, I believe this portion of the function docstring is not necessary.
This enhanced version: | ||
1. Tracks consecutive calls to the same function | ||
2. Breaks potential infinite loops after a threshold | ||
3. Provides a helpful response when a loop is detected | ||
4. Maintains compatibility with the original method | ||
|
||
Implementation details: | ||
The loop detection mechanism addresses an issue that can occur with certain models | ||
(particularly Ollama/Gemma3), where the model gets stuck repeatedly calling the same | ||
function without making progress. This commonly happens when: | ||
|
||
- The model receives malformed JSON responses it cannot parse | ||
- The model gets into a repetitive pattern of behavior | ||
- The model misunderstands function results and keeps trying the same approach | ||
|
||
When the same function is called consecutively more than the threshold number of times | ||
(default: 5), the loop detection mechanism interrupts the loop and provides a helpful | ||
response to the user instead of continuing to call the model. | ||
|
||
This prevents wasted resources and improves user experience by avoiding situations | ||
where the system would otherwise become unresponsive. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While looking at the semantics/style of the rest of the codebase and the fact that you have already provided a separate file documenting these changes, I believe this portion of the function docstring is not necessary.
Problem
When using ADK with LiteLLM and certain models (particularly Ollama/Gemma3), the system can get stuck in an infinite loop under specific conditions:
This creates a poor user experience and wastes resources as the model repeatedly attempts the same operation without making progress.
Solution
This PR implements two key fixes to address the issue:
Implementation Details
Testing
The fix includes:
Documentation
Impact and Considerations
This change: