-
Notifications
You must be signed in to change notification settings - Fork 160
feat(errors): expose Retry-After header on UsageLimitExceededError #166
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
Open
stihahi
wants to merge
2
commits into
tavily-ai:master
Choose a base branch
from
stihahi:feat/retry-after-on-usage-limit-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """Tests for Retry-After header propagation on 429 responses. | ||
|
|
||
| Tavily API returns a ``Retry-After`` header when it rejects a request with | ||
| ``429 Too Many Requests``. The SDK must expose that value on | ||
| ``UsageLimitExceededError`` so callers can honor the server's recommended | ||
| wait instead of falling back to a fixed backoff. | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from tavily.errors import UsageLimitExceededError, _parse_retry_after | ||
|
|
||
|
|
||
| RATE_LIMIT_BODY = {"detail": {"error": "rate limit exceeded"}} | ||
|
|
||
|
|
||
| def test_sync_429_exposes_retry_after_seconds(sync_interceptor, sync_client): | ||
| sync_interceptor.set_response(429, headers={"Retry-After": "7"}, json=RATE_LIMIT_BODY) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| sync_client.search("What is Tavily?") | ||
|
|
||
| assert exc_info.value.retry_after == pytest.approx(7.0) | ||
|
|
||
|
|
||
| def test_sync_429_retry_after_absent_is_none(sync_interceptor, sync_client): | ||
| sync_interceptor.set_response(429, json=RATE_LIMIT_BODY) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| sync_client.search("What is Tavily?") | ||
|
|
||
| assert exc_info.value.retry_after is None | ||
|
|
||
|
|
||
| def test_sync_429_retry_after_http_date(sync_interceptor, sync_client): | ||
| from email.utils import format_datetime | ||
| from datetime import datetime, timezone, timedelta | ||
|
|
||
| future = datetime.now(timezone.utc) + timedelta(seconds=30) | ||
| sync_interceptor.set_response( | ||
| 429, | ||
| headers={"Retry-After": format_datetime(future, usegmt=True)}, | ||
| json=RATE_LIMIT_BODY, | ||
| ) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| sync_client.search("What is Tavily?") | ||
|
|
||
| assert exc_info.value.retry_after is not None | ||
| assert 20 <= exc_info.value.retry_after <= 40 | ||
|
|
||
|
|
||
| def test_sync_429_retry_after_malformed_is_none(sync_interceptor, sync_client): | ||
| sync_interceptor.set_response( | ||
| 429, headers={"Retry-After": "not-a-number"}, json=RATE_LIMIT_BODY | ||
| ) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| sync_client.search("What is Tavily?") | ||
|
|
||
| assert exc_info.value.retry_after is None | ||
|
|
||
|
|
||
| def test_async_429_exposes_retry_after_seconds(async_interceptor, async_client): | ||
| async_interceptor.set_response(429, headers={"Retry-After": "12"}, json=RATE_LIMIT_BODY) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| asyncio.run(async_client.search("What is Tavily?")) | ||
|
|
||
| assert exc_info.value.retry_after == pytest.approx(12.0) | ||
|
|
||
|
|
||
| def test_async_429_retry_after_absent_is_none(async_interceptor, async_client): | ||
| async_interceptor.set_response(429, json=RATE_LIMIT_BODY) | ||
|
|
||
| with pytest.raises(UsageLimitExceededError) as exc_info: | ||
| asyncio.run(async_client.search("What is Tavily?")) | ||
|
|
||
| assert exc_info.value.retry_after is None | ||
|
|
||
|
|
||
| def test_usage_limit_error_default_retry_after_is_none(): | ||
| err = UsageLimitExceededError("boom") | ||
| assert err.retry_after is None | ||
|
|
||
|
|
||
| def test_usage_limit_error_accepts_retry_after(): | ||
| err = UsageLimitExceededError("boom", retry_after=3.5) | ||
| assert err.retry_after == 3.5 | ||
|
|
||
|
|
||
| def test_parse_retry_after_rejects_fractional_seconds(): | ||
| # RFC 7231 §7.1.3 only defines non-negative integer seconds. | ||
| assert _parse_retry_after({"Retry-After": "7.5"}) is None | ||
|
|
||
|
|
||
| def test_parse_retry_after_clamps_negative_seconds(): | ||
| assert _parse_retry_after({"Retry-After": "-10"}) == 0.0 | ||
|
|
||
|
|
||
| def test_parse_retry_after_rejects_nan_and_inf(): | ||
| assert _parse_retry_after({"Retry-After": "nan"}) is None | ||
| assert _parse_retry_after({"Retry-After": "inf"}) is None | ||
|
|
||
|
|
||
| def test_parse_retry_after_case_insensitive_lookup(): | ||
| assert _parse_retry_after({"retry-after": "5"}) == 5.0 | ||
| assert _parse_retry_after({"RETRY-AFTER": "5"}) == 5.0 | ||
|
|
||
|
|
||
| def test_parse_retry_after_past_http_date_clamps_to_zero(): | ||
| from email.utils import format_datetime | ||
| from datetime import datetime, timezone, timedelta | ||
|
|
||
| past = datetime.now(timezone.utc) - timedelta(seconds=60) | ||
| result = _parse_retry_after({"Retry-After": format_datetime(past, usegmt=True)}) | ||
| assert result == 0.0 | ||
|
|
||
|
|
||
| def test_parse_retry_after_empty_and_none(): | ||
| assert _parse_retry_after(None) is None | ||
| assert _parse_retry_after({}) is None | ||
| assert _parse_retry_after({"Retry-After": ""}) is None | ||
| assert _parse_retry_after({"Retry-After": " "}) is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.