I am trying to use the API's AdaptiveMtTranslateRequest
to translate documents, and the documentation suggests that it is possible to pass a list of strings as the content (e.g., multiple sentences in a single request). However, when I attempt to do this, I get an error.
Here’s my code:
/** Translates using AdaptiveMt. */
private static void adaptiveMtTranslate(
TranslationServiceClient translationServiceClient,
String projectId,
String location,
String datasetId,
List<String> content) {
String adaptiveMtDatasetName = String.format("projects/%s/locations/%s/adaptiveMtDatasets/%s",
projectId, location, datasetId);
AdaptiveMtTranslateRequest request = AdaptiveMtTranslateRequest.newBuilder()
.setParent(LocationName.of(projectId, location).toString())
.setDataset(adaptiveMtDatasetName)
.addAllContent(content)
.build();
AdaptiveMtTranslateResponse response = translationServiceClient.adaptiveMtTranslate(request);
for (AdaptiveMtTranslation translation : response.getTranslationsList()) {
System.out.println(translation.getTranslatedText());
}
}
When I pass a List<String>
with multiple entries as the content
parameter, I get the following error:
{
"error": {
"code": 400,
"message": "Request should not have more than 1 entries",
"status": "INVALID_ARGUMENT"
}
}
I’ve referred to the official API documentation and the Java client library documentation, and both seem to indicate that passing a list of strings is supported.
Am I missing something, or is this a limitation in the current implementation of the API's AdaptiveMtTranslateRequest
? If passing multiple strings isn't supported, is there a recommended way to batch translations without sending one request per sentence?
I suggest using a single string and then call it in loop to translate multiple strings. It’s because of the limitation of AdaptiveMtTranslation of handling a single string in the content field. Though it’s not directly indicated in the documents, error 400 is about the number of entries, which should be 1 only.
On Google side, there is a ‘feature request’ that you can file but there is no timeline on when it can be done. You can request this so that they can check if they can update to support multiple strings without calling a single string in loop.