Search code examples
pythonjsongoogle-cloud-platformgoogle-cloud-vertex-ai

How can I convert a PredictResponse to JSON?


I have a VertexAI project I want to access. I'm currently trying two approaches, via a React frontend and via a Python backend, which I would then connect to the FE. I posted a question about making requests to VertexAI from Node here.

In the python approach, I'm able to make the request and receive the correct response. However, in order for it to be accessible by the FE, I would need to convert it to JSON. I'm struggling with how to do that.

Here's the code I'm using:

# The AI Platform services require regional API endpoints.
client_options = {"api_endpoint": api_endpoint}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = PredictionServiceClient(
    client_options=client_options, credentials=credentials
)
instance = schema.predict.instance.TextClassificationPredictionInstance(
    content=content,
).to_value()
instances = [instance]
parameters_dict = {}
parameters = json_format.ParseDict(parameters_dict, Value())
endpoint = client.endpoint_path(
    project=project_id, location=compute_region, endpoint=endpoint_id
)
response = client.predict(
    endpoint=endpoint, instances=instances, parameters=parameters
)
response_dict = [dict(prediction) for prediction in response.predictions]

response_dict is printable, but I can't convert response to json using json.dumps because:

TypeError: Object of type PredictResponse is not JSON serializable

This is the error that has been plaguing me in every attempt. DuetAI simply tells me to use json.dumps.

EDIT

Here's the working code using the accepted response:

    ...
    response = client.predict(
        endpoint=endpoint, instances=instances, parameters=parameters
    )

    predictions = MessageToDict(response._pb)
    predictions = predictions["predictions"][0]

Solution

  • Since json.dumps requires serialization of your response/s, I believe it's better to use json_format.MessageToDict instead of json_format.ParseDict.

    They work quite the opposite:

    • ParseDict takes a JSON dictionary representation and merges it into a pre-existing Protobuf message object. It parses the JSON data and populates the corresponding fields in the Protobuf message.

    • MessageToDict takes a Protobuf message object and converts it into a Python dictionary representation which can then be easily serialized to JSON using json.dumps.

    I found some online resources which discuss the MessageToDict function of the protobuf library. Please check them out for more information.