PydanticUserError with defer_build=True and pydantic 2.12 in multiprocess forkserver workers
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện lỗi forkserver với các phiên bản Python, openai, pydantic và multiprocess được báo cáo. Theo dõi openai/_models.py qua construct(), construct_type() và _get_extra_fields_type(), sau đó kiểm tra pydantic/_internal/_mock_val_ser.py quanh _get_built(). Công việc được hoàn tất khi quá trình giải tuần tự hóa chat completion vẫn đáng tin cậy giữa các worker forkserver với cả hai thiết lập DEFER_PYDANTIC_BUILD, đồng thời có coverage hồi quy cho lỗi này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug Report
Summary
openai SDK 2.7.1 with pydantic 2.12.4 raises intermittent PydanticUserError ("Pydantic models should inherit from BaseModel, BaseModel cannot be instantiated directly") when deserializing chat completion responses in multiprocess forkserver worker processes.
Environment
openai==2.7.1pydantic==2.12.4pydantic-core==2.41.5- Python 3.12
- Using
multiprocess(dill-based fork ofmultiprocessing) withforkserverstart method - Backend: vLLM serving
client.chat.completions.create()responses
Reproduction
The error occurs when construct() calls _get_extra_fields_type(cls) which accesses cls.__pydantic_core_schema__["type"]. This triggers MockCoreSchema._get_built() → _attempt_rebuild() → model_rebuild(raise_errors=False, _parent_namespace_depth=5).
The hardcoded _parent_namespace_depth=5 walks 5 frames up the call stack to resolve forward references. When the call stack at that depth doesn't contain the openai types module namespace, model_rebuild silently fails and pydantic raises the error.
In a forkserver setup with ~700 workers processing millions of requests:
- With
DEFER_PYDANTIC_BUILD=true(default): ~0.6% error rate, growing over time. The lazy rebuild retries on each request, mostly succeeding. - With
DEFER_PYDANTIC_BUILD=false: ~9.5% error rate. Eager build fails at import time for ~9.5% of forkserver workers, and since the failure is cached, ALL requests for those workers fail.
Traceback
openai/_models.py:231 construct()
→ openai/_models.py:405 _construct_field()
→ openai/_models.py:570 construct_type() (iterating choices list)
→ openai/_models.py:561 construct_type() → type_.construct(**value)
→ openai/_models.py:236 construct() → _get_extra_fields_type()
→ openai/_models.py:414 _get_extra_fields_type() → schema["type"] == "model"
→ pydantic/_internal/_mock_val_ser.py:41 __getitem__() → _get_built()
→ pydantic/_internal/_mock_val_ser.py:58 _get_built() → raises PydanticUserError
Related Issues
- #1306 (
DEFER_PYDANTIC_BUILDcauses issues) - pydantic/pydantic#11849 (race condition with model instantiation)
Suggested Fix
Explicitly calling ChatCompletion.model_rebuild() (and related response types) before creating forkserver workers resolves the issue, since the correctly-built schema is then inherited by all worker processes. The SDK could do this internally — either eagerly at import time regardless of defer_build, or by catching the PydanticUserError in _get_extra_fields_type and calling model_rebuild() as a fallback.
- Ngôn ngữ chính
- Python
- Star
- 31.7k
- Fork
- 6.8k
- Merge trung bình
- 1 ngày 5 giờ
- Pull request đã merge (30 ngày)
- 128
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openai/openai-python
-
sdk-breaking-change v4
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
openai/openai-python#3837 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openai/openai-python#3556 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openai/openai-python#2927 · 7 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug openapi
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#2502 · 7 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
openai/openai-python#2486 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openai/openai-python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
mozilla/bedrock#17413 · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
instance instance add
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
searxng/searx-instances#943 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug tools
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
lance-format/lance#9655 ·
Maintainer thường phản hồi trong vòng 2 ngày