From ray import serve causes ImportError: cannot import name 'Schema' from 'pydantic'

Hey :wave:

When following the getting started guide (Getting Started Guide — Ray 1.13.0) specifically the Scalable Model Serving section, from ray import serve fails with ImportError: cannot import name ‘Schema’ from ‘pydantic’.

Virtualenv

Python:         3.9.13
Implementation: CPython

ray version
ray = {extras = ["serve"], version = "^1.13.0"}

import requests

from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier

from ray import serve

serve.start()

# Train model.
iris_dataset = load_iris()
model = GradientBoostingClassifier()
model.fit(iris_dataset["data"], iris_dataset["target"])


@serve.deployment(route_prefix="/iris")
class BoostingModel:
    def __init__(self, model):
        self.model = model
        self.label_list = iris_dataset["target_names"].tolist()

    async def __call__(self, request):
        payload = (await request.json())["vector"]
        print(f"Received http request with data {payload}")

        prediction = self.model.predict([payload])[0]
        human_name = self.label_list[prediction]
        return {"result": human_name}


# Deploy model.
BoostingModel.deploy(model)

# Query it!
sample_request_input = {"vector": [1.2, 1.0, 1.1, 0.9]}

response = requests.get("http://localhost:8000/iris", json=sample_request_input)

print('response', response.text)

Gives the following error.

Traceback (most recent call last):
  File "/Users/me/Developer/ray_demo/serve.py", line 9, in <module>
    from ray import serve
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/ray/serve/__init__.py", line 2, in <module>
    from ray.serve.api import (
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/ray/serve/api.py", line 14, in <module>
    from fastapi import APIRouter, FastAPI
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/fastapi/__init__.py", line 5, in <module>
    from .applications import FastAPI
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/fastapi/applications.py", line 3, in <module>
    from fastapi import routing
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/fastapi/routing.py", line 6, in <module>
    from fastapi import params
  File "/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/fastapi/params.py", line 4, in <module>
    from pydantic import Schema
ImportError: cannot import name 'Schema' from 'pydantic' (/Users/me/Library/Caches/pypoetry/virtualenvs/yes-6KwWb3XD-py3.9/lib/python3.9/site-packages/pydantic/__init__.cpython-39-darwin.so)

This appears to be a problem with how dependencies are managed i.e. via poetry using pyenv from ray import serve works as expected.

I couldn’t reproduce this with a virtualenv on Python 3.9.12.

This appears to be a problem with how dependencies are managed i.e. via poetry using pyenv from ray import serve works as expected.

Could you elaborate on what you mean by this?

hey @bveeramani ,

Yes, when I install ray via poetry the pydantic dependency causes the above error.

However, when I install with pyenv all works as expected.

Therefore the issue is with Poetry and not Ray.

Ah, gotcha. Makes sense