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: pydantic/pathlib patching causes LookupError: <ContextVar name='request_directory' at 0x172a18860> #4728

Open
jamt9000 opened this issue May 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jamt9000
Copy link

jamt9000 commented May 13, 2024

Describe the bug

We were seeing strange test failures with exceptions coming from bentoml code in tests unrelated to bentoml. It turns out that this was from the behaviour of import bentoml adding items to pydantic._internal. _std_types_schema which can cause unexpected validation issues. It seems that the pathlib validator tries to access a request_directory contextvar which doesn't exist.

Traceback (most recent call last):
  File "repro.py", line 20, in <module>
    myfunc(S3URI(url="s3://asdf"))
  File ".venv/lib/python3.11/site-packages/pydantic/deprecated/decorator.py", line 55, in wrapper_function
    return vd.call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pydantic/deprecated/decorator.py", line 149, in call
    m = self.init_model_instance(*args, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pydantic/deprecated/decorator.py", line 146, in init_model_instance
    return self.model(**values)
           ^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/pydantic/main.py", line 176, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
  File ".venv/lib/python3.11/site-packages/_bentoml_sdk/validators.py", line 155, in decode
    suffix=filename, dir=request_directory.get(), delete=False
                         ^^^^^^^^^^^^^^^^^^^^^^^
LookupError: <ContextVar name='request_directory' at 0x10f82d030>

To reproduce

from pathlib import Path

import bentoml # removing this import will make it succeed
from pydantic import BaseModel, validate_arguments


class S3URI(BaseModel):
    # simplification of real world class
    url: str


CloudPath = Path | S3URI # Note the order here matters, if S3URI is first it doesn't happen


@validate_arguments
def myfunc(x: CloudPath):
    pass


myfunc(S3URI(url="s3://asdf"))

Expected behavior

I'm not sure what is happening with the contextvar issue, but I would expect that to not fail or give an informative error if there is something that should be done differently. It is quite surprising that bentoml affects unrelated code (especially in the case of testing where it creates non-determinism depending on test ordering and when bentoml was imported in other tests) so some documentation or a way to opt in to that (like the patch_all() pattern other libs like gevent use) could be good.

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.15
python: 3.11.9
platform: macOS-13.6.6-x86_64-i386-64bit
uid_gid: 501:20

pip_packages
bentoml==1.2.15
pydantic==2.7.1
pydantic_core==2.18.2
@jamt9000 jamt9000 added the bug Something isn't working label May 13, 2024
@jamt9000
Copy link
Author

Here is a workaround I found:

import bentoml

from pydantic._internal import _std_types_schema

_std_types_schema.PREPARE_METHODS = tuple(
    [x for x in _std_types_schema.PREPARE_METHODS if "bentoml" not in x.__module__]
)

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

No branches or pull requests

1 participant