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

Cog and Pydantic v2 #2110

Closed
andrea-mucci opened this issue Jan 15, 2025 · 5 comments
Closed

Cog and Pydantic v2 #2110

andrea-mucci opened this issue Jan 15, 2025 · 5 comments

Comments

@andrea-mucci
Copy link

andrea-mucci commented Jan 15, 2025

I need to use a library that have as requirement pydantic 2.6.1 and cog return:

  • cog version: cog version 0.13.6 (built 2024-12-03T16:31:25Z)
  • python: 3.12

Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/cog/server/worker.py", line 435, in _load_predictor return load_predictor_from_ref(self._predictor_ref) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/cog/server/runner.py", line 229, in _handle_done f.result() File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result raise self._exception cog.server.exceptions.FatalWorkerException: Predictor errored during setup: cannot import name 'TypeAdapter' from 'pydantic' (/usr/local/lib/python3.12/site-packages/pydantic/__init__.cpython-312-x86_64-linux-gnu.so)

I'vfe seen that have more issues related with that problem but i've not seen any solution

@nickstenning
Copy link
Member

Could you share your cog.yaml? It seems likely that the version of pydantic that's actually installed is not 2.6.1, as pydantic==2.6.1 definitely has TypeAdapter:

$ pip install pydantic==2.6.1
Collecting pydantic==2.6.1
  Downloading pydantic-2.6.1-py3-none-any.whl.metadata (83 kB)
Requirement already satisfied: annotated-types>=0.4.0 in ./lib/python3.12/site-packages (from pydantic==2.6.1) (0.7.0)
Collecting pydantic-core==2.16.2 (from pydantic==2.6.1)
  Downloading pydantic_core-2.16.2-cp312-cp312-macosx_11_0_arm64.whl.metadata (6.5 kB)
Requirement already satisfied: typing-extensions>=4.6.1 in ./lib/python3.12/site-packages (from pydantic==2.6.1) (4.12.2)
Downloading pydantic-2.6.1-py3-none-any.whl (394 kB)
Downloading pydantic_core-2.16.2-cp312-cp312-macosx_11_0_arm64.whl (1.7 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 20.8 MB/s eta 0:00:00
Installing collected packages: pydantic-core, pydantic
  Attempting uninstall: pydantic-core
    Found existing installation: pydantic_core 2.1.2
    Uninstalling pydantic_core-2.1.2:
      Successfully uninstalled pydantic_core-2.1.2
  Attempting uninstall: pydantic
    Found existing installation: pydantic 2.0.2
    Uninstalling pydantic-2.0.2:
      Successfully uninstalled pydantic-2.0.2
Successfully installed pydantic-2.6.1 pydantic-core-2.16.2

[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: pip install --upgrade pip
$ python
Python 3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 16.0.0 (clang-1600.0.26.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pydantic import TypeAdapter
>>> print("OK")
OK

@andrea-mucci
Copy link
Author

Thank you for your answer

build:
  gpu: false
  python_version: "3.12"
  python_packages:
    - "syrius-sdk"
    - "boto3"
predict: "predict.py:Predictor"

the cog file is very simple
the syrius-sdk dependency has pydantic as a dependency and need a version at leat of 2.6.1

@nickstenning
Copy link
Member

If you're right that the syrius-sdk package depends on pydantic>=2.6.1, then part of the problem here is that that package doesn't correctly declare its dependencies:

$ pkginfo -f requires_dist syrius_sdk-0.2.60-py3-none-any.whl
requires_dist: ['pydantic', 'httpx', 'black; extra == "dev"', 'bumpver; extra == "dev"', 'isort; extra == "dev"', 'pip-tools; extra == "dev"', 'pytest; extra == "dev"']

As you can see, it declares a dependency on pydantic, without a version specifier, which is satisfied by the pydantic 1.x which is installed by default in the cog image.

In order to get pydantic>2 installed in your cog image, I'd recommend doing something like this in your cog.yaml:

build:
  gpu: false
  python_version: "3.12"
  python_packages:
    - "syrius-sdk"
    - "boto3"
    - "fastapi>0.100.0,<0.111.0"
    - "pydantic>2"
predict: "predict.py:Predictor"

You need to specify fastapi>0.100.0,<0.111.0 as well to make sure you get a version of fastapi that's compatible with pydantic>2.

@nickstenning
Copy link
Member

I also opened #2112 to track one part of this on our side.

@andrea-mucci
Copy link
Author

Thank you so much!!!

This solution work!!!

If you're right that the syrius-sdk package depends on pydantic>=2.6.1, then part of the problem here is that that package doesn't correctly declare its dependencies:

$ pkginfo -f requires_dist syrius_sdk-0.2.60-py3-none-any.whl
requires_dist: ['pydantic', 'httpx', 'black; extra == "dev"', 'bumpver; extra == "dev"', 'isort; extra == "dev"', 'pip-tools; extra == "dev"', 'pytest; extra == "dev"']
As you can see, it declares a dependency on pydantic, without a version specifier, which is satisfied by the pydantic 1.x which is installed by default in the cog image.

In order to get pydantic>2 installed in your cog image, I'd recommend doing something like this in your cog.yaml:

build:
gpu: false
python_version: "3.12"
python_packages:
- "syrius-sdk"
- "boto3"
- "fastapi>0.100.0,<0.111.0"
- "pydantic>2"
predict: "predict.py:Predictor"
You need to specify fastapi>0.100.0,<0.111.0 as well to make sure you get a version of fastapi that's compatible with pydantic>2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants