Skip to content

[BUG]: generated API with create_api does not annotate the data model correctly / not at all #3796

@pascal456

Description

@pascal456

pycaret version checks

Issue Description

The datamodel that is created with
pycaret.regression.compare_models
is not annotated correctly. Therefore the resulting API file does not work without errors.

Reproducible Example

create file test_model.py with content:

from pycaret.datasets import get_data
from pycaret.regression import create_api
from pycaret.regression import compare_models
from pycaret.regression import setup

data = get_data("insurance")
experiment = setup(data, target="charges", session_id=123)
best = compare_models(exclude=["lightgbm"])
create_api(best, "test_model")

Expected Behavior

The call

python test_model.py

would start the API

Actual Results

running

python test_model.py

returns:

Transformation Pipeline and Model Successfully Loaded
Traceback (most recent call last):
  File "/workspaces/bebefam-exploration/test_model.py", line 16, in <module>
    input_model = create_model("test_model_input", **{'age': 36, 'sex': 'male', 'bmi': 27.549999237060547, 'children': 3, 'smoker': 'no', 'region': 'northeast'})
  File "/home/vscode/.local/lib/python3.10/site-packages/pydantic/main.py", line 1413, in create_model
    return meta(__model_name, resolved_bases, namespace, __pydantic_reset_parent_namespace__=False, **kwds)
  File "/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 104, in __new__
    private_attributes = inspect_namespace(
  File "/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py", line 370, in inspect_namespace
    raise PydanticUserError(
pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `age = 36`. All model fields require a type annotation; if `age` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.

For further information visit https://errors.pydantic.dev/2.4/u/model-field-missing-annotation

Installed Versions

System: python: 3.10.13 (main, Sep 20 2023, 11:11:20) [GCC 12.2.0] executable: /usr/local/bin/python machine: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.36

PyCaret required dependencies:
pip: 23.3
setuptools: 68.2.2
pycaret: 3.1.0
IPython: 8.16.1
ipywidgets: 8.1.1
tqdm: 4.66.1
numpy: 1.23.5
pandas: 1.5.3
jinja2: 3.1.2
scipy: 1.10.1
joblib: 1.3.2
sklearn: 1.2.2
pyod: 1.1.1
imblearn: 0.11.0
category_encoders: 2.6.2
lightgbm: 4.1.0
numba: 0.58.1
requests: 2.31.0
matplotlib: 3.7.3
scikitplot: 0.3.7
yellowbrick: 1.5
plotly: 5.18.0
plotly-resampler: Not installed
kaleido: 0.2.1
schemdraw: 0.15
statsmodels: 0.14.0
sktime: 0.21.1
tbats: 1.1.3
pmdarima: 2.0.4
psutil: 5.9.6
markupsafe: 2.1.3
pickle5: Not installed
cloudpickle: 3.0.0
deprecation: 2.1.0
xxhash: 3.4.1
wurlitzer: 3.0.3

PyCaret optional dependencies:
shap: 0.43.0
interpret: Not installed
umap: Not installed
ydata_profiling: 4.6.1
explainerdashboard: 0.4.3
autoviz: Not installed
fairlearn: Not installed
deepchecks: Not installed
xgboost: 2.0.1
catboost: Not installed
kmodes: Not installed
mlxtend: Not installed
statsforecast: Not installed
tune_sklearn: Not installed
ray: Not installed
hyperopt: Not installed
optuna: Not installed
skopt: Not installed
mlflow: Not installed
gradio: Not installed
fastapi: 0.104.0
uvicorn: 0.23.2
m2cgen: Not installed
evidently: Not installed
fugue: Not installed
streamlit: Not installed
prophet: Not installed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions