nad
nad

Reputation: 2850

Parsing boto3 invoke_endpoint response from AWS SageMaker

I have a Sagemaker endpoint that I can infer to from boto3 client and get response.

Per boto3 doc, the Body of the response result is a Byte object StreamingBody type. I convert it to a dictionary

    response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
response_body = response['Body']
dict_response = response_body.read().decode('utf-8')
print(dict_response)

The above code gives me a response like below (stripped down for this post)

I need to retrieve the array from the "floatVal" key. How do I do that?

{
  "outputs": {
    "score": {
      "dtype": "DT_FLOAT",
      "floatVal": [
        0.00012408883776515722,
        ...........
        -0.8316119909286499,
        -0.24423488974571228
      ],
      "tensorShape": {
        "dim": [
          {
            "size": "1"
          },
          {
            "size": "1024"
          }
        ]
      }
    }
  },
  "modelSpec": {
    "version": "1",
    "name": "generic_model",
    "signatureName": "serving_default"
  }
}

Upvotes: 4

Views: 2790

Answers (1)

nad
nad

Reputation: 2850

Actually the dict_response is not really a dictionary here, rather a string type. So I had to convert the dict_response to an actual dictionary and then I could retrieve the floatVal key.

Updated code

response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
response_body = response['Body']
response_str = response_body.read().decode('utf-8')
response_dict = eval(response_str)
print(response_dict['outputs']['score']['floatVal'])

Upvotes: 4

Related Questions