aws / aws/sagemaker-python-sdk

Importing SageMaker SDK fails with Pydantic v2: "Field name 'json' shadows BaseModel attribute

Đã đóng
#5,320 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
component: pysdk-team type: bug
Ngôn ngữ chính
Python
Star
2.3k
Fork
1.3k
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
35

Mô tả

**Describe the bug**
I ran into the following error when trying to deploy a remote function (training job) with SageMaker using a custom docker container that I built.
Importing from sagemaker.remote_function import remote raises a Pydantic validation error at import time in recent versions of the SageMaker Python SDK. The error occurs even before any code using remote is executed.

The issue stems from models inside sagemaker_core (imported transitively by the SDK) that define fields named json, which raise the following exception under Pydantic v2:

```console
NameError: Field name "json" shadows a BaseModel attribute; use a different field name with "alias='json'"
```
The result is that the SageMaker SDK becomes unusable with Pydantic v2 due to this import-time crash

**To reproduce**
I ran into this error when trying to run a remote sagemaker training job in a custom docker container
```bash
# Create a fresh virtual environment
python -m venv venv
source venv/bin/activate

# Install SageMaker SDK and Pydantic v2
pip install "sagemaker==2.254.1" "pydantic"

# Try importing `remote` from `sagemaker.remote_function`
python - << 'EOF'
from sagemaker.remote_function import remote
EOF
```

**Expected behavior**
The import should succeed without errors, allowing use of the remote function and other SageMaker SDK components.

**Screenshots or logs**
```console
File "/usr/local/lib/python3.9/site-packages/sagemaker/jumpstart/types.py", line 20, in
from sagemaker_core.shapes import ModelAccessConfig as CoreModelAccessConfig
File "/usr/local/lib/python3.9/site-packages/sagemaker_core/main/shapes.py", line 2509, in
class MonitoringDatasetFormat(Base):
NameError: Field name "json" shadows a BaseModel attribute; use a different field name with "alias='json'".
```

**System information**
A description of your system. Please provide:
- **SageMaker Python SDK version**: 2.254.1
- **Framework version**: Pydantic V2
- **Python version**: 3.9.13
- **CPU or GPU**: CPU
- **Custom Docker image (Y/N)**: YES

**Additional context**
I resolved the issue by downgrading my SageMaker sdk version... the issue appears to be rooted in `sagemaker_core` model definitions being incompatible with the Pydantic v2 API.

Here are the other dependencies that I installed on the docker image:
```text
gensim==4.3.2
joblib==1.5.1
imbalanced-learn==0.11.0
nltk==3.8.1
numpy==1.21.5
pandas==1.4.2
psycopg2-binary==2.9.10
regex==2022.3.15
scikit-learn==1.0.2
SQLAlchemy==2.0.30
vaderSentiment==3.3.2
sagemaker==2.243.2
spacy==3.7.5
pyarrow
boto3
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với đường dẫn import trong sagemaker/jumpstart/types.py và các định nghĩa model bị lỗi trong sagemaker_core/main/shapes.py, đặc biệt là MonitoringDatasetFormat. Tái hiện sự cố bằng bản cài đặt Python 3.9 và Pydantic v2 được cung cấp, sau đó xác minh rằng việc import sagemaker.remote_function.remote thành công và các trường JSON bị ảnh hưởng vẫn có thể sử dụng được.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
machine-learning
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.