Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud Translation API (AdaptiveMtTranslateRequest): Cannot Pass a List of Strings as Content

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:

 

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?

0 0 56
0 REPLIES 0