Skip to content

Retry exhaustion falls through to IndexError instead of returning error #222

@Farukest

Description

@Farukest

When API retries are exhausted due to invalid responses (empty/null choices) or repeated errors, the agent crashes with IndexError instead of returning a proper error.

Root cause

File: run_agent.py, lines 2095 and 2326

Two retry exhaustion checks use > instead of >=:

while retry_count < max_retries:    # loop exits when retry_count == max_retries
    ...
    retry_count += 1
    ...
    if retry_count > max_retries:   # 6 > 6 is False - never triggers
        return error

Since the while loop condition is retry_count < max_retries, the maximum value of retry_count inside the loop after increment is exactly max_retries. The check retry_count > max_retries is therefore dead code - it can never be true.

When retries are exhausted:

  1. retry_count reaches max_retries (6)
  2. The > max_retries check does not trigger
  3. Code continues to backoff + continue
  4. Loop condition 6 < 6 is false, loop exits
  5. Falls through to response.choices[0].message with the last invalid response
  6. Crashes with IndexError (empty choices) or TypeError (null choices)

This affects both invalid response retries (line 2095) and API error retries (line 2326).

Reproduction

Any scenario where the API returns invalid responses for all retry attempts will crash. Common causes:

  • Rate limiting returning empty choices
  • Provider outage returning malformed responses
  • Network issues causing repeated failures

Suggested fix

Change > to >= at both locations so the check triggers when retry_count equals max_retries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions