Sagemaker linear learner: Cannot invoke endpoint - Problem with JSON format

0

Below is my Lambda function that i used to invoke the endpoint, but I am facing the following error,

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(json.dumps(event))
    
    response = client.invoke_endpoint(EndpointName='linear-learner-2019-12-12-16-20-56-788',
                                  ContentType='application/json',
                                  Body=(json.dumps(event)))
    return response

OUTPUT:

Function Logs:
START RequestId: 7f4c7589-b70f-4af8-834c-89a1a1fbe5e5 Version: $LATEST
Received event: {
  "instances": [
    {
      "features": [
        0.1,
        0.2
      ]
    }
  ]
}
{"instances": [{"features": [0.1, 0.2]}]}
An error occurred during JSON serialization of response: <botocore.response.StreamingBody object at 0x7f53918e2828> is not JSON serializable

Please help me resolve the issue, you can see the JSON input passed to the function. Not sure what is going wrong here. I even checked the cloudwatch logs not able to identify the origin of the issue.

Thanks in advance,
Arun

Edited by: NMAK on Dec 24, 2019 2:36 AM

Edited by: NMAK on Dec 24, 2019 2:37 AM

NMAK
asked 4 years ago584 views
1 Answer
0
import json 
import io
import boto3 

client = boto3.Session().client(service_name='sagemaker-runtime',region_name='us-east-1')

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-20-56-788',
                                  ContentType='application/json',
                                  Body= json.dumps(event))
                                  
    print(response)
    result = response['Body']
    res = result.read()
    res = json.loads(res)
    print(res)
    print(res['predictions'][0])

The error was in the response format and on how to read them. Above code resolve the issue.

NMAK
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions