fix(main): do not retry a failed agentic follow-up under a direct retry_policy

This commit is contained in:
mateo-berri 2026-11-04 09:12:05 -08:00
parent c404cc1e56
commit ac45a34f2b
2 changed files with 27 additions and 7 deletions

View file

@ -6680,6 +6680,8 @@ def _retry_plan_after_first_error(
first_error: Exception, num_retries: int, retry_policy: RetryPolicy
) -> _RetryPlan | None:
"""The retries a direct retry_policy grants after the first error, kept for the whole call like the router's."""
if is_agentic_followup_failure(first_error):
return None
policy_retries: Final[int | None] = get_num_retries_from_retry_policy(
exception=first_error, retry_policy=retry_policy
)

View file

@ -11143,31 +11143,49 @@ def test_completion_with_retries_internal_server_error_500():
mock_sleep.assert_called_once()
async def _with_three_retries(sync_mode: bool, original_function: MagicMock) -> object:
async def _with_three_retries(
sync_mode: bool, original_function: MagicMock, retry_policy: RetryPolicy | None
) -> object:
messages: Final = [{"role": "user", "content": "hi"}]
if sync_mode:
return completion_with_retries(
model="gpt-4o", messages=messages, num_retries=3, original_function=original_function
model="gpt-4o",
messages=messages,
num_retries=3,
retry_policy=retry_policy,
original_function=original_function,
)
return await acompletion_with_retries(
model="gpt-4o", messages=messages, num_retries=3, original_function=original_function
model="gpt-4o",
messages=messages,
num_retries=3,
retry_policy=retry_policy,
original_function=original_function,
)
@pytest.mark.asyncio
@pytest.mark.parametrize("sync_mode", [True, False])
async def test_completion_with_retries_stops_at_a_failed_agentic_follow_up(sync_mode: bool):
@pytest.mark.parametrize("retry_policy", [None, RetryPolicy(DefaultRetries=2)], ids=["no-policy", "policy"])
@pytest.mark.parametrize("failures_before_follow_up", [0, 1])
async def test_completion_with_retries_stops_at_a_failed_agentic_follow_up(
sync_mode: bool, retry_policy: RetryPolicy | None, failures_before_follow_up: int
):
follow_up_failure: Final = _make_internal_server_error()
mark_agentic_followup_failure(follow_up_failure, ())
outcomes: Final = [_make_internal_server_error(), follow_up_failure, {"status": "recovered"}]
outcomes: Final = [
*(_make_internal_server_error() for _ in range(failures_before_follow_up)),
follow_up_failure,
{"status": "recovered"},
]
mock_completion: Final = MagicMock(side_effect=outcomes) if sync_mode else AsyncMock(side_effect=outcomes)
with patch("time.sleep"), patch("asyncio.sleep", new_callable=AsyncMock):
with pytest.raises(litellm.InternalServerError) as raised:
await _with_three_retries(sync_mode, mock_completion)
await _with_three_retries(sync_mode, mock_completion, retry_policy)
assert raised.value is follow_up_failure
assert mock_completion.call_count == 2
assert mock_completion.call_count == failures_before_follow_up + 1
@pytest.mark.asyncio