Sagemaker boto3 invoke_endpoint - I keep getting type errors for payload. using Blazingtext model endpoint

Viewed 2766

Let me frame the issue. I have trained a blazingtext model and have an endpoint deployed. Within my Notebook instance I can call model.predict and get inferences from the endpoint. I am now trying to set up a lambda and an API gateway for the endpoint. I am having trouble trying to figure out what the payload is supposed to be for Invoke_endpoint(endpoint_name = mymodel, body = payload)

I keep getting invalid payload format errors

This is what my payload looks like when testing the lambda

{"instances":"string of text"}

the documentation says the body take b'bytes or file like objects. i have tinkered around with IO with no luck. No good blogs or tutorials out there for this particular issue. Only a bunch of videos going over the cookie cutter examples that are out there.

import io
import boto3
import json
import csv




# grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= 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["instances"]
    print(data)
    #print(payload)
   
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       ContentType='application/json',
                                       Body=payload.getvalue())
    #print(response)
    #result = json.loads(response['Body'].read().decode())
    #print(result)
    #pred = int(result['predictions'][0]['score'])
    #predicted_label = 'M' if pred == 1 else 'B'
    
    return ```

"errorMessage": "An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (406) from model with message \"Invalid payload format\"
1 Answers

If your payload is what you describe, i.e.:

payload = {"instances":"string of text"}

then you can get it in the form of json string using:

json.dumps(payload)

# which gives:

'{"instances": "string of text"}'

If you want it in bate array, then you can do as follows:

json.dumps(payload).encode()

# which gives:

b'{"instances": "string of text"}'
Related