Skip to content

RateLimitError incorrectly retried due to APIError inheritance in @retry decorator #1807

@KangmoonSeo

Description

@KangmoonSeo

Git provider

Github Cloud

System Info

model used: o4-mini with no credit (for raising openai.RateLimitError in completion)
deployment type: cli

Bug details

Description

relates #1802

  • Current retry decorator configuration in AIHandler classes causes RateLimitError to be incorrectly retried
  • The retry condition retry_if_exception_type(openai.APIError) catches RateLimitError since it inherits from APIError
  • This breaks the intended behavior where rate limit errors should not be retried
# pr_agent/algo/ai_handlers/litellm_ai_handler.py

@retry(
    retry=retry_if_exception_type((openai.APIError, openai.APIConnectionError, openai.APITimeoutError)), # No retry on RateLimitError
    stop=stop_after_attempt(OPENAI_RETRIES)
)
async def chat_completion(self, ...):
    # ...

Current Behavior (Problematic)

  • used o4-mini API with no credit, for raising openai.RateLimitError in completion
    • image
  • RateLimitError gets retried 5 times despite being logged at error level
  • The @retry decorator's openai.APIError condition absorbs all scenarios including rate limits

Issue Details

  • The retry decorator comment explicitly states # No retry on RateLimitError
  • However, since RateLimitError inherits from APIError:
    # openai/_exceptions.py
     
    class APIError(OpenAIError):
    ...
    class APIStatusError(APIError):
    ...
    class RateLimitError(APIStatusError):
        status_code: Literal[429] = 429
    ...
  • The retry condition retry_if_exception_type(openai.APIError) catches RateLimitError instances
  • This causes the retry mechanism to incorrectly retry rate limit errors up to OPENAI_RETRIES times

Expected Behavior

  • RateLimitError should be raised immediately without retry attempts
  • Only non-rate-limit APIError subtypes should trigger retries

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions