fix: exclude RateLimitError from @retry
in AIHandler.chat_completion()
#1808
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.
User description
Description
This PR implements 3 changes to properly exclude
RateLimitError
from retry logic in allAIHandler.chat_completion()
Changes
@retry
library:from tenacity import retry
instead of the built-inimport retry
libraryOpenAIHandler
at pr_agent/algo/ai_handlers/openai_ai_handler.pyLangChainOpenAIHandler
at pr_agent/algo/ai_handlers/langchain_ai_handler.pyRateLimitError
from@retry
conditions:APIError
condition was absorbingRateLimitError
instances, preventing retry breakpointretry_if_not_exception_type
to includeAPIError
while explicitly excludingRateLimitError
RateLimitError(error)
->APIError(warn)
->Exception(warn)
pattern consistentlyLiteLLMAIHandler.chat_completion()
#1803APITimeoutError
handling, since it inherits fromAPIError
closes #1807
Test Results
AS-IS (Problematic)
RateLimitError
incorrectly retried 5 times despite error-level loggingTO-BE (Fixed)
RateLimitError scenario
RateLimitError
now exits immediately after single attempt as intendedAPIError (non-RateLimit) scenario
APIError
types correctly retry 5 times as expectedNormal execution
PR Type
Bug fix, Enhancement
Description
Excludes
RateLimitError
from retry logic in all AIHandlerchat_completion
methodsretry_if_not_exception_type(openai.RateLimitError)
with tenacity for precise controlStandardizes retry and exception handling across OpenAI, LiteLLM, and LangChain handlers
Removes redundant or overly broad exception handling
Changes walkthrough 📝
langchain_ai_handler.py
Refactor retry and error handling, exclude RateLimitError from retries
pr_agent/algo/ai_handlers/langchain_ai_handler.py
RateLimitError
from retries using combined predicateslitellm_ai_handler.py
Exclude RateLimitError from retries, unify error handling
pr_agent/algo/ai_handlers/litellm_ai_handler.py
RateLimitError
using tenacityopenai_ai_handler.py
Use tenacity, exclude RateLimitError from retry, refactor errors
pr_agent/algo/ai_handlers/openai_ai_handler.py
RateLimitError
from retries using combined predicates