Issues while Invoking sagemaker endpoints with deepar algorithm
Hi,
I am getting error while invoking sagemaker endpoint for deepar algorithm using lambda function
it says that
Call Failed An error occurred (ModelError) when calling the InvokeEndpoint operation:
Received client error (400) from model with message \Unable to evaluate payload provided.
See https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEventViewer:group=/aws/sagemaker/Endpoints/live-deep-ar-v1 in account 761276118745 for more information
my code looks like below
import boto3
import math
import dateutil
import json
import os
#import numpy as np
#import sagemaker
grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
client = boto3.client(service_name='sagemaker-runtime')
def lambda_handler(event, context):
try:
print("Received event: " + json.dumps(event, indent=2))
request = json.loads(json.dumps(event))
transformed_data = [(instance['target']) for instance in request["instances"]]
payload = bytearray(transformed_data)
result = client.invoke_endpoint(EndpointName=ENDPOINT_NAME, Body= payload, ContentType='application/json')
predictions = result['Body'].read()
return {
'statusCode': 200,
'isBase64Encoded':False,
'body': json.dumps(predictions)
}
except Exception as err:
return {
'statusCode': 400,
'isBase64Encoded':False,
'body': 'Call Failed {0}'.format(err)
}
could anyone help me on this.....
Hello @sudhar09,
I think your payload is misconfigured. It should look like the example here: https://docs.aws.amazon.com/sagemaker/latest/dg/deepar-in-formats.html
From your example it looks like you could just set Body=json.dumps(event) as your payload.
Hello Jasper,
Thanks for your suggestion it works for boto3 and sagemaker sdk but throws error for lambda function as ""body": "Call Failed Object of type 'bytes' is not JSON serializable"
my input seems like this
{ "instances": [ { "start": "2019-02-01 10:00:00", "target": [120]} ], "configuration": { "num_samples": 1, "output_types": ["mean", "quantiles", "samples"], "quantiles": ["0.5", "0.9"] } }
and my lambda function below
grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME'] client = boto3.client(service_name='sagemaker-runtime')
def lambda_handler(event, context):
try:
print("Received event: " + json.dumps(event, indent=2))
request = json.loads(json.dumps(event))
result = client.invoke_endpoint(EndpointName=ENDPOINT_NAME, Body= json.dumps(request), ContentType='application/json')
predictions = result['Body'].read()
return {
'statusCode': 200,
'isBase64Encoded':False,
'body': json.dumps(predictions)
}
except Exception as err:
return {
'statusCode': 400,
'isBase64Encoded':False,
'body': 'Call Failed {0}'.format(err)
}
please correct me that where i done mistake!!
I think we are getting there!
"Call Failed Object of type 'bytes' is not JSON serializable"
Is this error actually thrown here?
'body': json.dumps(predictions)
Your predictions is actually a byte-string and not a json-object. (SageMaker doesn't know what the return type of your request is, that's why it is always a raw bytes.)
I think what you want is to use json.loads(predictions) instead.
Hi @jaheba
Worked and got results thanks for your help :)
Great, glad it works.
@jaheba I am facing similar issue, I am able to call the endpoint in sagemkaer notebook successfully.
But using the 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(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
I am facing the below error,
Function Logs:
START RequestId: 38601b24-9ac9-4510-9283-2edeba6d294a 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 0x7fe13beba828> is not JSON serializable
I hope you can help me here
what are you passing as context? how are you calling your function(i mean the code)? i am getting error and stuck with the same problem