When returning variables in cloud function I get: TypeError: Object of type RepeatedComposite is not JSON serializable

Viewed 34

For some context I am building a cloud function to handle calls to a deployed Vertex AI model. Everything seems to work fine until it comes to returning the actual value.

When I copy and paste that exact dictionary {'fox':[0.002073,..]} as such: Example code that works:

@functions_framework.http
def hello_get(request):
    # Set CORS headers for the preflight request
    ...
    # Get prediction using Vertex AI endpoint
    instance = get_prediction(request_json['instances'])
    "Example output is a list of 512 float values"

    output = zip(request_json['instances'], instance)
    sorted_output = sorted(output, key=itemgetter(1), reverse=True)
    f_output = dict(sorted_output) # not used!!
    return {'fox': [0.00207372033, -0.0550017245, -0.040824689...]}

It doesn't crash, yet if I have the variable assigned such as:

out = {'fox': [0.00207372033, -0.0550017245, -0.040824689...]}
return out

I get the error "TypeError: Object of type RepeatedComposite is not JSON serializable"

Example code that causes failure:

@functions_framework.http
def hello_get(request):
    # Set CORS headers for the preflight request
    ...
    # Get prediction using Vertex AI endpoint
    instance = get_prediction(request_json['instances'])
    "Example output is a list of 512 float values"

    output = zip(request_json['instances'], instance)
    sorted_output = sorted(output, key=itemgetter(1), reverse=True)
    f_output = dict(sorted_output)
    return f_output

This makes absolutely no sense to me, I was hoping someone could help.

My environment is:

functions-framework==3.2.0
flask==2.1.0
google-cloud-error-reporting==1.6.0
google-cloud-aiplatform==1.16.1
1st Gen Cloud Function, 1gb, unauthenticated access, trigger HTTP

OR even more oddly this works as well:

@functions_framework.http
def hello_get(request):
    # Set CORS headers for the preflight request
    ...
    # Get prediction using Vertex AI endpoint
    instance = get_prediction(request_json['instances'])
    "Example output is a list of 512 float values"

    output = zip(request_json['instances'], instance)
    sorted_output = sorted(output, key=itemgetter(1), reverse=True)

    f_output = dict(sorted_output)
    # wrapping dict in str
    return str(f_output)

This returns the desired output dictionary {'fox':[0.002,...]}, but as a string.

0 Answers
Related