jsongoogle-apigoogle-api-java-clientgoogle-prediction

"Invalid value for: Unable to parse" google prediction api request


I am trying to use the Google Prediction API. I have trained my model, and tested a prediction through the web page and it works great. However, I am now trying to use the java api to predict a bunch of records but I keep getting an error

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'.",
    "reason" : "invalid"
  } ],
  "message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'."

To me it seems like the json creator is not putting quotes around the features, but I am following the sample as close as possible and they don't change or modify the json factory. Here is the credential and prediction building code.

private static GoogleCredential authorize() throws Exception {

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(Collections.singleton(PredictionScopes.PREDICTION))
            .setServiceAccountPrivateKeyFromP12File(new File("p12filefromdevconsole.p12"))
            .build();
    return credential;

}

...
Prediction prediction = new Prediction.Builder(
            httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();

...
private static Output predict(Prediction prediction, String... features) throws IOException {
    Input input = new Input();
    InputInput inputInput = new InputInput();
    inputInput.setCsvInstance(Collections.<Object>singletonList(features));
    input.setInput(inputInput);
    Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute();
    return output;
}

Any thoughts what I am doing wrong?


Solution

  • After much frustration and trial and error I solved this problem by using new ArrayList(Arrays.asList(features)) and not using the Collections.singletonList(features). Here is the modified predict method. Keep in mind my original implementation came directly from the sample on Googles website :(

    private static Output predict(Prediction prediction, String... features) throws IOException {
        Input input = new Input();
        InputInput inputInput = new InputInput();
        inputInput.setCsvInstance(new ArrayList(Arrays.asList(features)));
        input.setInput(inputInput);
        Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute();
        return output;
    }