Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client/async): correctly retry in all cases #704

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ async def _request(
except Exception as err:
log.debug("Encountered Exception", exc_info=True)

if retries_taken > 0:
if remaining_retries > 0:
return await self._retry_request(
input_options,
cast_to,
Expand Down
21 changes: 19 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tracemalloc
from typing import Any, Union, cast
from unittest import mock
from typing_extensions import Literal

import httpx
import pytest
Expand Down Expand Up @@ -873,7 +874,14 @@ def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> Non
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
def test_retries_taken(self, client: Anthropic, failures_before_success: int, respx_mock: MockRouter) -> None:
@pytest.mark.parametrize("failure_mode", ["status", "exception"])
def test_retries_taken(
self,
client: Anthropic,
failures_before_success: int,
failure_mode: Literal["status", "exception"],
respx_mock: MockRouter,
) -> None:
client = client.with_options(max_retries=4)

nb_retries = 0
Expand All @@ -882,6 +890,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
nonlocal nb_retries
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand Down Expand Up @@ -1840,8 +1850,13 @@ async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter)
@mock.patch("anthropic._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
@pytest.mark.parametrize("failure_mode", ["status", "exception"])
async def test_retries_taken(
self, async_client: AsyncAnthropic, failures_before_success: int, respx_mock: MockRouter
self,
async_client: AsyncAnthropic,
failures_before_success: int,
failure_mode: Literal["status", "exception"],
respx_mock: MockRouter,
) -> None:
client = async_client.with_options(max_retries=4)

Expand All @@ -1851,6 +1866,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
nonlocal nb_retries
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand Down