Repository navigation
Python: Fix the auto_retry sample so it retries 429 errors - #10068
Open
Sohom chakraborty (sohom-cs) wants to merge 1 commit into
Open
Sohom chakraborty (sohom-cs) wants to merge 1 commit into
Sohom chakraborty (sohom-cs) wants to merge 1 commit into
Conversation
All three approaches in samples/02-agents/auto_retry.py retried on retry_if_exception_type(openai.RateLimitError). Chat clients raise ChatClientException and keep the RateLimitError on __cause__, so the predicate never matched and a 429 was raised on the first attempt. Add an is_rate_limit_error() predicate that checks the cause, and use it in all three approaches.
Sohom chakraborty (sohom-cs)
temporarily deployed
to
github-app-auth
November 3, 2026 22:07 — with
GitHub Actions
Inactive
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the update to handle wrapped rate-limit errors across all three retry implementations.
No open issues 🎉
What changed in this PR
Fixes the Python auto-retry sample for #9904 by recognizing OpenAI rate-limit errors wrapped in ChatClientException, allowing the sample’s retry layer to handle 429 responses.
Changes:
- Adds a shared predicate that checks the exception type and its
__cause__. - Applies the predicate to all three retry implementations.
- Updates documentation to explain wrapped errors and retry behavior.
| File | Description |
|---|---|
python/samples/02-agents/auto_retry.py |
Corrects rate-limit detection across all retry approaches and clarifies documentation. |
Review effort: Balanced
This branch was previously deployed
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.
Motivation & Context
None of the three approaches in
python/samples/02-agents/auto_retry.pyretries a 429. They retry onretry_if_exception_type(openai.RateLimitError), but the chat clients raiseChatClientExceptionwith theRateLimitErroron__cause__, so the predicate never matches. Reproduction in #9904.Description & Review Guide
is_rate_limit_error(), which matches aChatClientExceptionwhose__cause__is anopenai.RateLimitError, and use it throughretry_if_exceptionin all three approaches. The docstrings now say what is retried.FoundryChatClienton a mock transport (429, then 200; SDK retries off), each approach now retries and returns the response instead of raising on the first 429. A 400 is still not retried. There are no tests for these samples, so none are added. Not tested against a live rate limit or with streaming.__cause__is how you want the sample to show this. The predicate requiresChatClientException, so a bareRateLimitErroris not retried.Related Issue
Fixes #9904
AI Assistance
AI assistance details: Claude Code helped with the issue analysis, the reproduction, the change, and this description. I ran the reproduction before and after the change.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.