pytorch / serve

Serve, optimize and scale PyTorch models in production
https://pytorch.org/serve/
Apache License 2.0
4.16k stars 844 forks source link

Handle invalid input intelligently ! #2459

Open xuweidongkobe opened 1 year ago

xuweidongkobe commented 1 year ago

🚀 The feature

Given a custom service that expects JSON as input, If the input is invalid JSON such as { "image": "I missed a quote} it will return { "code": 500, "type": "InternalServerException", "message": "Worker died." } and reload model. Is something can do to verify the accuracy of input to avoid such direct returns and model reload when it cannot be modified input?

Motivation, pitch

Is something can do to verify the accuracy of input to avoid such direct returns and model reload when it cannot be modified input?

Alternatives

No response

Additional context

No response

arnavmehta7 commented 1 year ago

You can have a custom exception.

xuweidongkobe commented 1 year ago

You can have a custom exception.

This error occurred before entered my service script, and I cannot make up for a custom exception.@arnavmehta7 Can you tell me how to make a useful custom exception? Thank you.

arnavmehta7 commented 1 year ago

@xuweidongkobe Are you setting the headers then set to Application/json? Any snippets?

xuweidongkobe commented 1 year ago

@xuweidongkobe Are you setting the headers then set to Application/json? Any snippets?

In the code, I use the default head and input data format. In postman test, I am set input data in Application/json format.@arnavmehta7

agunapal commented 1 year ago

@xuweidongkobe Here is an example where we pass a list in the request body and the handler is reading the list. You can use a similar logic to check if its a valid json and show appropriate error message https://github.com/pytorch/serve/blob/master/examples/image_classifier/near_real_time_video/near_real_time_video_handler.py#L20-L21

xuweidongkobe commented 1 year ago

When the request body does not in the valid JSON format, an error was occurred before entering the preprocess function. @agunapal

Gauravkumar1502 commented 1 year ago

What if we consider using the following code snippet?

import json
try:
    input_data = json.loads(input_json)
    # Process input_data here
except json.JSONDecodeError as e:
    return {
        "code": 500,
        "type": "InternalServerException",
        "message": "Worker died."
    }