Skip to content

[ai] api: Add auto_retry_rate_limits option to handle 429 errors. - #946

Open
Aditya-XR wants to merge 1 commit into
zulip:mainfrom
Aditya-XR:auto-retry-rate-limits-773
Open

Aditya-XR wants to merge 1 commit into
zulip:mainfrom
Aditya-XR:auto-retry-rate-limits-773

Conversation

@Aditya-XR

@Aditya-XR Aditya-XR commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes: #773

Completes #908 and addresses @alexmv's review feedback:

  • Makes rate-limit retries opt-in via auto_retry_rate_limits: bool = False on Client.__init__ so existing callers that handle RATE_LIMIT_HIT responses themselves are not blocked by time.sleep().
  • Leaves retries unincremented on 429 responses since rate limits are expected backpressure with a server-provided Retry-After delay rather than transient network/502 failures, though we can cap consecutive 429 retries if preferred.

How did you test this PR?

  • Added unit tests in zulip/tests/test_rate_limit.py covering default auto_retry_rate_limits=False behavior, Retry-After sleep + retry with verbose output, and fallback to 1.0s when Retry-After is missing or malformed.
  • ./tools/test-zulip, mypy, ruff, and ./tools/lint
$ PYTHONPATH=zulip python3 -m unittest discover -v -s zulip/tests -p test_rate_limit.py
test_rate_limit_auto_retry_enabled (test_rate_limit.TestRateLimitRetry.test_rate_limit_auto_retry_enabled) ... ok
test_rate_limit_invalid_or_missing_retry_after_header (test_rate_limit.TestRateLimitRetry.test_rate_limit_invalid_or_missing_retry_after_header) ... ok
test_rate_limit_no_retry_by_default (test_rate_limit.TestRateLimitRetry.test_rate_limit_no_retry_by_default) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.005s

OK
Self-review checklist
  • Self-reviewed the changes for clarity and maintainability
    (variable names, code reuse, readability, etc.).

Communicate decisions, questions, and potential concerns.

  • Explains differences from previous plans (e.g., issue description).
  • Highlights technical choices and bugs encountered.
  • Calls out remaining decisions and concerns.
  • Automated tests verify logic where appropriate.

Individual commits are ready for review (see commit discipline).

  • Each commit is a coherent idea.
  • Commit message(s) explain reasoning and motivation for changes.

Completed manual review and testing of the following:

  • Visual appearance of the changes.
  • Responsiveness and internationalization.
  • Strings and tooltips.
  • End-to-end functionality of buttons, interactions and flows.
  • Corner cases, error conditions, and easily imagined bugs.

Add an opt-in auto_retry_rate_limits parameter (defaulting to False)
to Client.__init__ so callers can automatically sleep for the duration
specified in the Retry-After response header and retry when the server
returns HTTP 429 (RATE_LIMIT_HIT).

Fixes: zulip#773

Co-authored-by: Soham Panda <cozyat@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 11:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle RATE_LIMIT_HIT (Rate limit exceeded) errors

3 participants