4

I am trying to implement google automl api: https://cloud.google.com/automl-tables/docs/predict

The api docs says I need to post data in following format:

{
  "payload": {
    "row": {
     "values": [value1, value2,...]
         }
      }
}

But when I to post data in same format I get the following error:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"row\" at 'payload': Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
    "@type": "type.googleapis.com/google.rpc.BadRequest",
    "fieldViolations": [
      {
        "field": "payload",
        "description": "Invalid JSON payload received. Unknown name \"row\" at 'payload': Cannot find field."
      }
    ]
      }
    ]
  }
}

Here is my curl request:

curl --location --request POST 'https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/TBLID:predict' \
--header 'Content-Type: application/json; charset=utf-8;' \
--header 'Accept-Charset: utf-8' \
--header 'Authorization: Bearer token here' \
--data-raw '{
 "payload": {
"row": {
  "values": [
    "",
    "4.900000",
    ""
  ]
}
 }
}'

I am not sure what is causing this error. Can anyone help with this problem thanks

ashok poudel
  • 703
  • 11
  • 28

2 Answers2

4

I have a project with AutoML tables and this is an example prediction request sent in which works fine and returns results without error:

{
  "payload": {
    "row": {
      "values": [
        "2",
        "test text",
        "test text",
        "21",
        "0",
        "0",
        "test text",
        "10",
        "",
        "S"
      ]
    }
  }
}

If you're not sure how to submit data or you are getting errors, go to your project in the console and to your AutoML Tables model and click "Test and Use". Then, follow these directions for getting an online prediction from console. Pay attention to the JSON Code View which will give you the exact input and let you test your JSON input as well.

To test, I submitted the following curl request (with some identifying characters changed)

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://automl.googleapis.com/v1beta1/projects/this-isnt-a-real-project-id/locations/us-central1/models/TBL123451234512345:predict

My request.json file in the same directory that I sent the request from was:

{
    "payload": {
      "row": {
        "values": [
          "yes",
          "-1",
          "primary",
          "jul",
          "51",
          "0",
          "yes",
          "no",
          "88",
          "cellular",
          "blue-collar",
          "unknown",
          "10",
          "620",
          "married"
        ]
      }
    }
  }

The response was:

{
  "payload": [
    {
      "tables": {
        "score": 0.9999906,
        "value": "no"
      }
    },
    {
      "tables": {
        "score": 9.42341e-06,
        "value": "yes"
      }
    }
  ]
}

I also repeated this same request in postman successfully below:

curl --location --request POST 'https://automl.googleapis.com/v1beta1/projects/this-isnt-a-real-project-id/locations/us-central1/models/TBL123451234512345:predict' \
--header 'Accept-Charset: utf-8' \
--header 'Authorization: Bearer abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd' \
--header 'Content-Type: application/json' \
--data-raw '{
    "payload": {
      "row": {
        "values": [
          "yes",
          "-1",
          "primary",
          "jul",
          "51",
          "0",
          "yes",
          "no",
          "88",
          "cellular",
          "blue-collar",
          "unknown",
          "10",
          "620",
          "married"
        ]
      }
    }
  }'
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • Yes I just copy pasted the json code view and hit it through postman but I get the error – ashok poudel Jul 11 '20 at 14:49
  • Something about how you are doing it in postman is what is wrong then. I have hit it through a direct curl and it works fine. Without information on exactly how you are doing the rest of what you are doing we can’t help you. Basically, this isn’t AutoML that is having a problem, it is whatever is happening between you building the JSON and the JSON arriving at the API. – MyNameIsCaleb Jul 11 '20 at 15:26
  • I updated with curl request that gets generated by postman – ashok poudel Jul 11 '20 at 15:31
  • Or you can post your curl request maybe I can figure out something from it – ashok poudel Jul 11 '20 at 15:36
  • The [sample in the docs](https://cloud.google.com/automl-tables/docs/predict#automl-tables-example-cli-curl) works for me just as given there. – MyNameIsCaleb Jul 13 '20 at 16:39
  • My curl is also similar but I wonder why it didn't worked – ashok poudel Jul 13 '20 at 16:51
  • 1
    It seems that a comma is missing between "4,900000" and "" in your request json – Orlandog Jul 13 '20 at 17:18
  • @ashokpoudel I have updated with my exact curl and request.json file as well as a raw request from postman – MyNameIsCaleb Jul 13 '20 at 21:32
  • @ashokpoudel are you sure you are using the correct URL with your correct table id as well? – MyNameIsCaleb Jul 13 '20 at 21:33
0

I replicated the Quickstart and I did some successful curl requests and your issue seems to be related to the format of your json request; therefore, I recommend the following tests:

  1. Remove the line breaks from your json, and make the request again:
curl --location --request POST 'https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/TBLID:predict' \
--header 'Content-Type: application/json; charset=utf-8;' \
--header 'Accept-Charset: utf-8' \
--header 'Authorization: Bearer token-here' \
--data-raw '{ "payload": { "row": { "values": [ "", "4.900000", "" ] } } }'
  1. Save the json in a file and make the request again:
curl --location --request POST 'https://automl.googleapis.com/v1/projects/project-id/locations/us-central1/models/TBLID:predict' \
--header 'Content-Type: application/json; charset=utf-8;' \
--header 'Accept-Charset: utf-8' \
--header 'Authorization: Bearer token-here' \
-d @request.json

If after performing them you still have the issue, I recommend you contact the GCP support so they can analyze your issue within your project.

Orlandog
  • 325
  • 1
  • 4