feat(retry): add retry_policies.rate_limit() convenience policy#2995
Open
RhythrosaLabs wants to merge 1 commit intoopenai:mainfrom
Open
feat(retry): add retry_policies.rate_limit() convenience policy#2995RhythrosaLabs wants to merge 1 commit intoopenai:mainfrom
RhythrosaLabs wants to merge 1 commit intoopenai:mainfrom
Conversation
Closes openai#782. Add a ready-made `rate_limit()` helper to `_RetryPolicies` that retries on HTTP 429 responses only, honouring `Retry-After` / `Retry-After-Ms` headers when present and falling back to the configured backoff schedule when they are absent. Before this change users had to compose policies manually: retry_policies.any( retry_policies.http_status([429]), retry_policies.retry_after(), ) This combination has subtle semantics: `http_status([429])` ignores the header while `retry_after()` fires on any error that carries a header, not just 429s. Neither alone captures the intended "retry 429s, wait the prescribed cool-down" contract. After this change: retry_policies.rate_limit() The implementation checks `context.normalized.status_code == 429`, then reads the delay from `context.normalized.retry_after` (header parsed by the runner) and falls back to `context.provider_advice.retry_after` when the normalised value is absent. This ensures the Retry-After header is consumed only for the specific error it accompanies. Changes ------- * src/agents/retry.py – new `rate_limit()` method on `_RetryPolicies` * tests/test_model_retry.py – 8 new tests (6 unit + 2 integration) * examples/basic/rate_limit_retry.py – minimal example (new file) * docs/models/index.md – new row in the ready-made helpers table
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #782.
Adds
retry_policies.rate_limit()— a ready-made helper that retries on HTTP 429 responses only, honouringRetry-After/Retry-After-Msheaders when present and falling back to the configured backoff schedule when absent.Motivation
Issue #782 reports that there is no concise way to express "retry rate-limit errors, wait the prescribed cool-down". The existing primitives require users to manually compose:
This is both verbose and subtly wrong:
http_status([429])fires on 429 but ignores theRetry-Afterheader entirely.retry_after()honours the header but triggers on any error that carries one, not just 429s.Neither alone captures the intended contract. Users need domain knowledge of the internals to compose them correctly.
What this PR adds
The policy:
RetryDecision(retry=False)for anything other than HTTP 429.RetryDecision(retry=True, delay=<seconds>)when aRetry-Afterheader was parsed (viacontext.normalized.retry_after) or surfaced by provider advice — so the runner waits the prescribed cool-down.RetryDecision(retry=True, delay=None)when no header is present — the runner falls back to the configured backoff schedule.Can be combined with
provider_suggested()for deeper provider integration:Changes
src/agents/retry.pyrate_limit()method on_RetryPoliciestests/test_model_retry.pyget_response_with_retry)examples/basic/rate_limit_retry.pydocs/models/index.mdTests
New tests cover:
delay=None)normalized.retry_afterdelay when header is presentprovider_advice.retry_afterwhen normalized value is absentnormalized.retry_aftertakes precedence overprovider_advice.retry_afterget_response_with_retryrun with 429+Retry-After: 2header →sleep(2.0)calledChecklist
_mark_retry_capabilities