0
predict = [0.1,0.2]
payload = {
    "instances":    [
        {
            "features": predict
        }
    ]
}
response = linear_regressor.predict(json.dumps(payload))
predictions = json.loads(response)
print(json.dumps(predictions, indent=2))

The above code is able to invoke the endpoint which is the linear-learner endpoint and give the below result.

{
  "predictions": [
    {
      "score": 0.13421717286109924
    }
  ]
}

But when I try to invoke the endpoint using below lambda function,

import json 
import io
import boto3 

client = boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))

    #data = json.loads(json.dumps(event))
    #payload = data['data']
    #print(payload)

    response = client.invoke_endpoint(EndpointName='linear-learner-2019-12-12-16-xx-xx-xxx',
                                  ContentType='application/json',
                                  Body=json.dumps(event))
    return response

I get the following error,

    An error occurred during JSON serialization of response: 
<botocore.response.StreamingBody object at 0x7fc941e7e828> is not JSON serializable

The input for the lambda function is the same,

{
  "instances": [
    {
      "features": [
        0.1,
        0.2
      ]
    }
  ]
}

Not sure what is causing the issue here. Any help would be appreciated, Thanks in advance.

6
  • Could you try using boto3.client('sagemaker-runtime') instead? Commented Dec 29, 2019 at 19:46
  • @velociraptor11, Yes I tried that, still same error. I believe the problem is in the JSON format that I am sending to the endpoint, not able to find any documentation/sample request format. Commented Dec 30, 2019 at 11:44
  • Reason I asked is because your code seems just about right. I am passing json.dumps() to the Body of invoke endpoint and it's no problem at all. Try removing "Content Type" from the function call. So only client.invoke_endpoint(EndpointName='linear-learner-2019-12-12-16-xx-xx-xxx', Body=json.dumps(event)) I think it could be due to the fact that you are passing a string and the client call is expecting a json Commented Dec 30, 2019 at 13:54
  • No luck with removing the Content Type as well,still getting same error.Are you using linear-learner endpoint as well in your lambda. Commented Dec 30, 2019 at 16:51
  • I'm not. I am using the MxNet custom deploy container as well as a Tensorflow serving container. I think best way is to raise an issue in the Github page and maybe they can help you from there. They typically reply pretty fast Commented Dec 30, 2019 at 17:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.