Reputation: 25
I am running this code to translate
translate = boto3.client(service_name='translate',
aws_access_key_id="secret",
aws_secret_access_key="secret",
region_name='eu-central-1',
use_ssl=True)
translate.translate_text(Text=data,SourceLanguageCode="de",TargetLanguageCode="en").get("TranslatedText")
Code runs properly for most of the test but suddenly throws the following error:
An error occurred (ThrottlingException) when calling the TranslateText operation (reached max retries: 4): Rate exceeded
How to handle this exception?
Upvotes: 2
Views: 3973
Reputation: 173
I know this is a python question, but it seems this solution is universal to almost all AWS SDK's (including boto - here is a list of compatible SDK's with this solution: https://docs.aws.amazon.com/sdkref/latest/guide/feature-retry-behavior.html#feature-retry-behavior-sdk-compat)
If you specify retryMode
and maxAttempts
then it seems that the SDK handles timeouts and retries automatically.
This is from the javascript SDK, but should work the same with python according to the list above, though the configuration-keys could be named differently:
const { TranslateClient } = require("@aws-sdk/client-translate");
const translateClient = new TranslateClient({
region: "eu-central-1",
});
const { TranslateClient } = require("@aws-sdk/client-translate");
const translateClient = new TranslateClient({
region: "eu-central-1",
maxAttempts: 20, // How many attempts to make (default is 3)
retryMode: "ExponentialBackoff", // Couldn't find any explanations for which retryModes is available through the docs but "ExponentialBackoff" is mentioned in the link above and seems to work š¤·āāļø
});
Upvotes: 0
Reputation: 5651
Ran into a similar issue and did not want to use asynchronous batch requests, because I'm using multiple translation APIs and would rather keep the code the same for all.
Here are payload limits for different translation APIs. This may impact throttling (e.g. calling the Amazon Translate API multiple times per second with a small payload worked just fine):
Amazon's limit is clearly the lowest.
Now for the throttling limits on Amazon Translate:
Amazon Translate scales to serve customer operational traffic. If you encounter sustained throttling, contact AWS Support.
The default throttling limits for AWS GovCloud (US) are set to 5000 Bytes per 10 seconds per language pair and 10 transactions per second per language pair. You can request an increase for any of the limits using the Amazon Translate service limits increase form.
For my use case (translating HTML larger than 5,000 bytes after splitting it into chunks), ended up implementing a simple wait of 15 seconds between API calls to avoid hitting the throttling limits. In my tests, translating a payload of 2K to 5K bytes 200 times, with a sleep time of 15 seconds between calls, all ran successfully (whereas with a wait time of just 11 seconds I'd still get some ThrottlingException
.)
If I were to code it better, I'd probably implement retries with exponential backoff like Marcin suggested. Or would request a limit increase to make my life easier and my experience more consistent across APIs.
Upvotes: 2
Reputation: 7787
This question is a few months old now but the solution here worked well for me and let me avoid needing to write my own backoff code:
import boto3
from botocore.config import Config
config = Config(retries=dict(max_attempts=10))
region = "us-east-1"
translate = boto3.client(
service_name="translate",
region_name=region,
use_ssl=True,
config=config,
)
Even in us-east-1 I seem to hit a few retries, and it's much slower than Google Cloud Translate (which I also hit from Python in the same script), but it works.
Upvotes: 0
Reputation: 7424
Have you considered using the asynchronous call start_text_translation_job()
instead of the synchronous call translate_text()
? Then you would have a much higher limit, instead of 5000 bytes, you would have 1,000,000 characters * 1,000,000 documents * 10 batches: https://docs.aws.amazon.com/translate/latest/dg/what-is-limits.html#limits-throttling
Synchronous Real-Time Translation Limits:
Description Limit
Character encoding UTF-8
Maximum document size (UTF-8 characters) 5,000 bytes
Asynchronous Batch Translation Limits:
Description Limit
Character encoding UTF-8
Maximum number of characters per document 1,000,000
Maximum size per document 20 MB
Maximum number of documents in batch 1,000,000
Maximum size of total documents in batch 5 GB
Maximum number of parallel batch translation jobs 10
The code for the asynchronous start_text_translation_job()
call can be find here:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/translate.html
response = client.start_text_translation_job(
JobName='string',
InputDataConfig={
'S3Uri': 'string',
'ContentType': 'string'
},
OutputDataConfig={
'S3Uri': 'string'
},
DataAccessRoleArn='string',
SourceLanguageCode='string',
TargetLanguageCodes=[
'string',
],
TerminologyNames=[
'string',
],
ClientToken='string'
)
Upvotes: 0
Reputation: 36
This link has answers to throttling exceptions https://docs.aws.amazon.com/translate/latest/dg/what-is-limits.html#limits-throttling The service scales up the fleet based on the traffic pattern. I wonder at what TPS you hit the throttling limits.
&
Have you also tried a different region like eu-west-1?
Upvotes: 2