Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Cannot use pydantic models with namedtuples #4703

Closed
dcferreira opened this issue May 5, 2024 · 0 comments · Fixed by #4709
Closed

bug: Cannot use pydantic models with namedtuples #4703

dcferreira opened this issue May 5, 2024 · 0 comments · Fixed by #4709
Labels
bug Something isn't working

Comments

@dcferreira
Copy link

dcferreira commented May 5, 2024

Describe the bug

I have a BentoML service that serves whisper, and when upgrading our codebase to pydantic 2.x I think I ran into a bug in BentoML's parsing of pydantic models into OpenAPI spec.

For the return type of my endpoint, I have a pydantic class in which some fields are named tuples from another library (such as this).
When trying to build the docker image, while the OpenAPI spec is created, I get the error:

  File "/Users/dcferreira/projects/other/bentoml_openapi_example/.venv/lib/python3.11/site-packages/_bentoml_sdk/io_models.py", line 97, in <dictcomp>
    components.update({k: Schema(**v) for k, v in defs.items()})
                          ^^^^^^^^^^^
TypeError: Schema.__init__() got an unexpected keyword argument 'prefixItems'

I believe this is the same issue as in this other library, where this prefixItems is a new thing in the OpenAPI spec.
This was working with pydantic 1.10.

To reproduce

  1. Create a service.py
from __future__ import annotations
from collections import namedtuple
import bentoml
from pydantic import BaseModel

EXAMPLE_INPUT = "world!"

MyTuple = namedtuple("MyTuple", ["foo", "bar"])

class OtherParams(BaseModel):
  foo: str = "foo"
  bar: str = "bar"
  tuple1: tuple[str, str] = ("foo", "bar")
  tuple2: MyTuple = MyTuple(foo="foo", bar="bar")

EXAMPLE_PARAMS = OtherParams()

@bentoml.service()
class HelloWorld:
  def __init__(self) -> None:
      self.val = "hello "

  @bentoml.api
  def run(self, text: str = EXAMPLE_INPUT, params: OtherParams = EXAMPLE_PARAMS) -> str:
      print(params.dict())
      return self.val + text
  1. Run bentoml serve service:HelloWorld
  2. Send any request to the server
  3. It will crash with the above error

As an alternative, you can replace steps 2-4 with running bentoml build . if you use this bentofile.yaml

service: 'service:HelloWorld'
labels:
  owner: bentoml-team
  project: gallery
include:
  - '*.py'
python:
  packages:
    - pydantic

If you remove the tuple2 from the pydantic class above, everything works as expected.

Expected behavior

No response

Environment

Environment variable

BENTOML_DEBUG=''
BENTOML_QUIET=''
BENTOML_BUNDLE_LOCAL_BUILD=''
BENTOML_DO_NOT_TRACK=''
BENTOML_CONFIG=''
BENTOML_CONFIG_OPTIONS=''
BENTOML_PORT=''
BENTOML_HOST=''
BENTOML_API_WORKERS=''

System information

bentoml: 1.2.12
python: 3.11.4
platform: macOS-14.4.1-arm64-arm-64bit
uid_gid: 501:20

pip_packages
aiohttp==3.9.5
aiosignal==1.3.1
annotated-types==0.6.0
anyio==4.3.0
appdirs==1.4.4
asgiref==3.8.1
attrs==23.2.0
bentoml==1.2.12
build==1.2.1
cattrs==23.1.2
certifi==2024.2.2
circus==0.18.0
click==8.1.7
click-option-group==0.5.6
cloudpickle==3.0.0
deepmerge==1.1.1
Deprecated==1.2.14
frozenlist==1.4.1
fs==2.4.16
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
importlib-metadata==6.11.0
inflection==0.5.1
Jinja2==3.1.3
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
multidict==6.0.5
numpy==1.26.4
nvidia-ml-py==11.525.150
opentelemetry-api==1.20.0
opentelemetry-instrumentation==0.41b0
opentelemetry-instrumentation-aiohttp-client==0.41b0
opentelemetry-instrumentation-asgi==0.41b0
opentelemetry-sdk==1.20.0
opentelemetry-semantic-conventions==0.41b0
opentelemetry-util-http==0.41b0
packaging==24.0
pathspec==0.12.1
pip-requirements-parser==32.0.1
pip-tools==7.4.1
prometheus_client==0.20.0
psutil==5.9.8
pydantic==2.7.1
pydantic_core==2.18.2
Pygments==2.18.0
pyparsing==3.1.2
pyproject_hooks==1.1.0
python-dateutil==2.9.0.post0
python-json-logger==2.0.7
python-multipart==0.0.9
PyYAML==6.0.1
pyzmq==26.0.3
rich==13.7.1
schema==0.7.7
simple-di==0.1.5
six==1.16.0
sniffio==1.3.1
starlette==0.37.2
tomli_w==1.0.0
tornado==6.4
typing_extensions==4.11.0
uvicorn==0.29.0
watchfiles==0.21.0
wrapt==1.16.0
yarl==1.9.4
zipp==3.18.1
@dcferreira dcferreira added the bug Something isn't working label May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant