bentoml / BentoML

The easiest way to serve AI apps and models - Build Model Inference APIs, Job queues, LLM apps, Multi-model pipelines, and more!
https://bentoml.com
Apache License 2.0
7.13k stars 791 forks source link

JSON descriptor with pydantic model returns dictionary instead of pydantic model object (RC0) #2577

Closed ALee008 closed 2 years ago

ALee008 commented 2 years ago

Describe the bug When using JSON() descriptor with a pydantic model I expect a pydantic model object to be passed as parameter to the decorated function, a dictionary is returned instead. This seems to be an issue in RC0 because it was working in the latest alpha release 1.0.0a7.

To Reproduce

Expected behavior dummy is a pydantic model which has the attribute dict.

Environment:

Additional context In venv/Lib/site-packages/bentoml/_internal/io_descriptors/json.py I tested a change in row 177 by removing the .dict() method call. See my inline comment below. This solved this particular issue.

    async def from_http_request(self, request: Request) -> JSONType:
        json_str = await request.body()
        if self._pydantic_model is not None and self._validate_json:
            try:
                import pydantic
            except ImportError:
                raise MissingDependencyException(
                    "`pydantic` must be installed to use `pydantic_model`"
                ) from None
            try:
                pydantic_model = self._pydantic_model.parse_raw(json_str)
                return pydantic_model  # !!! <-- removed `.dict()` method call to return a pydantic object !!!
            except pydantic.ValidationError as e:
                raise BadInput(f"Json validation error: {e}") from None
        else:
            try:
                return json.loads(json_str)
            except json.JSONDecodeError as e:
                raise BadInput(f"Json validation error: {e}") from None
aarnphm commented 2 years ago

Hi @ALee008, I believe that this is the expected behaviour to use pydantic to convert the payload to json. We are currently working on documentation on this behaviour. I will try to get back to you ASAP after discussing with the team.

P/S: I believe this https://github.com/bentoml/BentoML/commit/e85d3697f946219c7d5853c093d7cf14dc1f1a82#diff-dc32bfb232459ae49509822c85ca5b705e1e92764f5db50364674db397ea0ba5R172-R173 updates the behaviour of pydantic model, which is included in rc0 releases.

aarnphm commented 2 years ago

Hi @ALee008, the behaviour has been fixed in #2578 and merged on main. We will work on better documents this behaviour.

ALee008 commented 2 years ago

@aarnphm , much appreciated! Thank you guys for your quick responses. You are doing great work!