Arihant Hirawat
06/14/2024, 6:00 AMtext and name. When making a POST request with a missing name field, I receive a default Pydantic validation error. How can I modify my BentoML service to catch this validation error and return a custom error message like "Invalid request body" instead of the default Pydantic error message?
Here is my service code:
class RequestParams(BaseModel):
text: str
name: str
@bentoml.service
class Summarization:
def __init__(self) -> None:
self.pipeline = pipeline('summarization')
@bentoml.api(route='/api/v1/summarize', input_spec=RequestParams)
def summarize(self, ctx: bentoml.Context, **request_parameters):
try:
with bentoml_service_model_inferencing_duration_seconds().labels(
endpoint='/api/v1/summarize',
service_name='Summarization'
).time():
summary = self.pipeline(request_parameters['text'])
result = summary[0]['summary_text']
return {'message': result}
except Exception:
ctx.response.status_code = HTTPStatus.INTERNAL_SERVER_ERROR
logger.exception('Internal Server Error', status_code=ctx.response.status_code)
return {'message': 'Internal Server Error'}Jian Shen Yap
06/17/2024, 6:53 AMArihant Hirawat
06/17/2024, 7:45 AMArihant Hirawat
07/01/2024, 9:31 AM{
"error": "1 validation error for Input",
"detail": [
{
"type": "missing",
"loc": [
"text1"
],
"msg": "Field required",
"url": "<https://errors.pydantic.dev/2.7/v/missing>"
}
]
}
How can I configure the above error message with custom response like Invalid Request Body or basically hide the url from the error details?Jian Shen Yap
07/04/2024, 2:40 AMFrost Ming
07/04/2024, 2:43 AM