Skip to content

Python: Fix the auto_retry sample so it retries 429 errors - #10068

Open
Sohom chakraborty (sohom-cs) wants to merge 1 commit into
microsoft:mainfrom
sohom-cs:fix/auto-retry-sample-429
Open

Sohom chakraborty (sohom-cs) wants to merge 1 commit into
microsoft:mainfrom
sohom-cs:fix/auto-retry-sample-429

Conversation

@sohom-cs

Copy link
Copy Markdown

Motivation & Context

None of the three approaches in python/samples/02-agents/auto_retry.py retries a 429. They retry on retry_if_exception_type(openai.RateLimitError), but the chat clients raise ChatClientException with the RateLimitError on __cause__, so the predicate never matches. Reproduction in #9904.

Description & Review Guide

  • What are the major changes? Add is_rate_limit_error(), which matches a ChatClientException whose __cause__ is an openai.RateLimitError, and use it through retry_if_exception in all three approaches. The docstrings now say what is retried.
  • What is the impact of these changes? Sample only. With FoundryChatClient on 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.
  • What do you want reviewers to focus on? Whether checking __cause__ is how you want the sample to show this. The predicate requires ChatClientException, so a bare RateLimitError is not retried.

Related Issue

Fixes #9904

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

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

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

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-cs

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

1 inactive deployment
github-app-auth — 983a2710 Deployed Nov 3, 2026 by sohom-cs via add_label #27022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: auto_retry.py sample never retries a 429: the client raises ChatClientException, not RateLimitError

2 participants