fix(sdk): add max retry limit to rate-limited API calls#6542
Open
zhoufengen wants to merge 1 commit intocomet-ml:mainfrom
Open
fix(sdk): add max retry limit to rate-limited API calls#6542zhoufengen wants to merge 1 commit intocomet-ml:mainfrom
zhoufengen wants to merge 1 commit intocomet-ml:mainfrom
Conversation
The ensure_rest_api_call_respecting_rate_limit function retried indefinitely on persistent HTTP 429 responses, which could cause callers to hang forever (e.g., when account quota is exhausted). Add a MAX_RATE_LIMIT_RETRIES cap of 10 attempts, after which the 429 ApiError is re-raised.
Comment on lines
+40
to
+45
| if attempt >= MAX_RATE_LIMIT_RETRIES - 1: | ||
| LOGGER.error( | ||
| "Rate limit retries exhausted after %d attempts", | ||
| MAX_RATE_LIMIT_RETRIES, | ||
| ) | ||
| raise |
Contributor
There was a problem hiding this comment.
When the 10th 429 retry is exhausted, this raise rethrows the original ApiError instead of converting it to opik.exceptions.OpikCloudRequestsRateLimited with headers/retry_after—should we wrap the last 429 here?
Finding type: AI Coding Guidelines | Severity: 🟠 Medium
Want Baz to fix this for you? Activate Fixer
Other fix methods
Prompt for AI Agents:
Before applying, verify this suggestion against the current code. In
sdks/python/src/opik/api_objects/rest_helpers.py around lines 40-45 inside
`ensure_rest_api_call_respecting_rate_limit`, the code currently re-raises the original
`ApiError` when the final 429 retry is exhausted (`attempt >= MAX_RATE_LIMIT_RETRIES -
1`). Refactor this branch to convert that last rate-limit failure into the standardized
`opik.exceptions.OpikCloudRequestsRateLimited`, populating it with the response
`headers` and the computed `retry_after` (use
`rate_limit.parse_rate_limit(exception.headers).retry_after()` when possible, otherwise
fall back to 1). Ensure the raised exception type matches what callers in `dataset.py`
and `annotation_queue.py` (and their existing retry guidance in
`online_message_processor.py` / `queue_consumer.py`) expect.
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
The
ensure_rest_api_call_respecting_rate_limitfunction in the Python SDK retries indefinitely on persistent HTTP 429 responses, which can cause callers to hang forever when account quota is exhausted or rate limits are misconfigured.Changes
while Trueloop with a boundedforloop capped atMAX_RATE_LIMIT_RETRIES = 10attemptsApiErrorafter exhausting all retriesTesting
retry_decorator.py) already caps at 3 attempts for other errors; this brings rate limit handling in line with that patternApiErroris raised to the callerRelated Issues
None (discovered during code review)