Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 93d6eeb

Browse files
stainless-app[bot]stainless-bot
authored andcommittedOct 7, 2024
fix(client): avoid OverflowError with very large retry counts (#676)
1 parent bbf9fc7 commit 93d6eeb

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed
 

‎src/anthropic/_base_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ def _calculate_retry_timeout(
690690
if retry_after is not None and 0 < retry_after <= 60:
691691
return retry_after
692692

693-
nb_retries = max_retries - remaining_retries
693+
# Also cap retry count to 1000 to avoid any potential overflows with `pow`
694+
nb_retries = min(max_retries - remaining_retries, 1000)
694695

695696
# Apply exponential backoff, but not more than the max.
696697
sleep_seconds = min(INITIAL_RETRY_DELAY * pow(2.0, nb_retries), MAX_RETRY_DELAY)

‎tests/test_client.py

+2
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ class Model(BaseModel):
804804
[3, "", 0.5],
805805
[2, "", 0.5 * 2.0],
806806
[1, "", 0.5 * 4.0],
807+
[-1100, "", 7.8], # test large number potentially overflowing
807808
],
808809
)
809810
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))
@@ -1768,6 +1769,7 @@ class Model(BaseModel):
17681769
[3, "", 0.5],
17691770
[2, "", 0.5 * 2.0],
17701771
[1, "", 0.5 * 4.0],
1772+
[-1100, "", 7.8], # test large number potentially overflowing
17711773
],
17721774
)
17731775
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))

0 commit comments

Comments
 (0)
Please sign in to comment.