Hey, I have a BentoML service with an API endpoin...
# ask-for-help
a
Hey, I have a BentoML service with an API endpoint for text summarization that uses Pydantic for request validation. The request parameters include
text
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:
Copy 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'}
j
Why would you want to do that? You may have to then intercept this on the middleware layer
a
The main reason for wanting to customize the error message is to prevent exposing the use of Pydantic for validation. This could help avoid giving attackers specific details about the service implementation. Could you guide me on how to implement this as middleware?
Hey, Can someone please help me with configuring Pydantic error response in BentoML? Currently, the error looks similar to shown below.
Copy code
{
  "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?
j
intercepting pydantic error can only be achieved by middleware for now. @Frost Ming is that right?
f
Yes, and it will be a bit troublesome. You need to parse the response, modify it, and then resend it. BentoML is not a full-featured web framework and doesn’t provide many abilities for that(hooks, error handlers, etc.)