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:
retry_count reaches max_retries (6)
- The
> max_retries check does not trigger
- Code continues to backoff +
continue
- Loop condition
6 < 6 is false, loop exits
- Falls through to
response.choices[0].message with the last invalid response
- 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.
When API retries are exhausted due to invalid responses (empty/null
choices) or repeated errors, the agent crashes withIndexErrorinstead of returning a proper error.Root cause
File:
run_agent.py, lines 2095 and 2326Two retry exhaustion checks use
>instead of>=:Since the while loop condition is
retry_count < max_retries, the maximum value ofretry_countinside the loop after increment is exactlymax_retries. The checkretry_count > max_retriesis therefore dead code - it can never be true.When retries are exhausted:
retry_countreachesmax_retries(6)> max_retriescheck does not triggercontinue6 < 6is false, loop exitsresponse.choices[0].messagewith the last invalid responseIndexError(empty choices) orTypeError(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:
Suggested fix
Change
>to>=at both locations so the check triggers whenretry_countequalsmax_retries.