Skip to content

Retry translation API requests when rate limited#82

Draft
desrosj wants to merge 16 commits intomainfrom
add/retry-exponential-backoff-for-translation-requests
Draft

Retry translation API requests when rate limited#82
desrosj wants to merge 16 commits intomainfrom
add/retry-exponential-backoff-for-translation-requests

Conversation

@desrosj
Copy link
Member

@desrosj desrosj commented Jul 29, 2025

Proposed changes

This implements an exponential back off for retrying translation API requests when encountering 429 errors indicating rate limiting.

Fixes #80.

Note: to avoid merge conflicts, I've included the changes from #81. That should be considered first. If not merged, then this should be updated to remove the related changes.

Type of Change

Production

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Dependency update
  • Refactoring / housekeeping (changes to files not directly related to functionality)

Development

  • Tests
  • Dependency update
  • Environment update / refactoring
  • Documentation Update

Visual

Checklist

  • I have read the CONTRIBUTING doc
  • I have viewed my change in a web-browser
  • Linting and tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@desrosj desrosj requested a review from mr-vara July 29, 2025 14:07
@desrosj desrosj self-assigned this Jul 29, 2025
Copilot AI review requested due to automatic review settings July 29, 2025 14:07

This comment was marked as outdated.

@desrosj desrosj requested a review from Copilot July 29, 2025 14:23

This comment was marked as outdated.

@desrosj desrosj requested a review from Copilot July 29, 2025 14:55

This comment was marked as outdated.

@desrosj desrosj requested a review from Copilot July 29, 2025 15:12

This comment was marked as outdated.

@desrosj desrosj requested a review from Copilot July 29, 2025 15:18

This comment was marked as outdated.

@desrosj desrosj requested a review from Copilot July 29, 2025 15:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements exponential backoff retry logic for translation API requests when encountering 429 rate limit errors from the Azure AI translation service.

  • Adds configurable retry attempts with exponential backoff for handling rate limit errors
  • Improves error handling and logging to provide better feedback on API failures
  • Ensures translation operations can recover from temporary rate limiting issues

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
.github/workflows/reusable-translations.yml Adds retry-attempts input parameter for configuring retry behavior
.github/scripts/translate.py Implements retry logic with exponential backoff and enhanced error handling
Comments suppressed due to low confidence (1)

.github/scripts/translate.py:30

  • [nitpick] The variable name max_retries is confusing because it's used as the maximum number of attempts (including the first attempt), not the maximum number of retries. Consider renaming to max_attempts for clarity.
    max_retries = RETRY_ATTEMPTS

error_data = response.json().get("error", {})
error_message = error_data.get("message", "")
error_code = error_data.get("code", "")
except (ValueError, KeyError, json.JSONDecodeError):
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

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

The json module is not imported but json.JSONDecodeError is referenced. Either import the json module at the top of the file or use requests.exceptions.JSONDecodeError if available.

Suggested change
except (ValueError, KeyError, json.JSONDecodeError):
except (ValueError, KeyError, json.JSONDecodeError): # Ensure json.JSONDecodeError is explicitly referenced

Copilot uses AI. Check for mistakes.
Comment on lines +32 to +42
try:
response = session.post(url, headers=headers, json=payload)
response.raise_for_status()
return [item["translations"][0]["text"] for item in response.json()]
except requests.exceptions.HTTPError:
status_code = response.status_code

try:
error_data = response.json().get("error", {})
error_message = error_data.get("message", "")
error_code = error_data.get("code", "")
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

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

The response variable is accessed inside the except block, but it may not be defined if the exception occurs before the response is assigned. Move the response assignment outside the try block or handle the case where response is undefined.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@desrosj
Copy link
Member Author

desrosj commented Jul 31, 2025

I've opened #90 to add unit tests. We should be able to introduce a test that confirms this new behavior.

@desrosj desrosj marked this pull request as draft September 18, 2025 18:57
@desrosj
Copy link
Member Author

desrosj commented Sep 18, 2025

Moving this to draft until #90 is merged so tests can be added.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translation workflow occasionally hits rate limiting

1 participant