From ac45a34f2b84e587c10421e560e51c3fe02be1f5 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:12:05 -0800 Subject: [PATCH] fix(main): do not retry a failed agentic follow-up under a direct retry_policy --- litellm/main.py | 2 ++ tests/unit/test_main.py | 32 +++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 8b2f046592b..c878da7cd0b 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -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 ) diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 7427c6bec41..3afc337a524 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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