PydanticUserError with defer_build=True and pydantic 2.12 in multiprocess forkserver workers
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
まず、報告されている Python、openai、pydantic、multiprocess のバージョンで forkserver の失敗を再現します。openai/_models.py を construct()、construct_type()、_get_extra_fields_type() の順に追跡し、その後 pydantic/_internal/_mock_val_ser.py の _get_built() 周辺を調査します。両方の DEFER_PYDANTIC_BUILD 設定で forkserver ワーカー間の chat completion のデシリアライゼーションが信頼性を保ち、この失敗に対する回帰テストのカバレッジがあることを完了条件とします。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 31.8k
- フォーク
- 7.3k
- 平均マージ
- 1日 1時間
- マージ済み PR(30日)
- 137
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openai/openai-python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
openai/openai-python#4022 · コメント 19 件 ·
メンテナーはふだん 1 日以内に返信
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providers対応中かも @mohmedmm が 11 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
openai/openai-python#4017 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Querystring drops explicit empty-string scalar values対応中かも @sylvesterkaczmarek が 33 日前に担当しました。 オープンsdk-breaking-change v4
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
openai/openai-python#3837 ·
メンテナーはふだん 1 日以内に返信
-
Define + export `ServiceTiers` string literal対応中かも @SparshGarg999 が 60 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
openai/openai-python#3556 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Empty OPENAI_BASE_URL prevents fallback to default API endpoint対応中かも @Sehastrajit-S が 26 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
openai/openai-python#2927 · コメント 6 件 ·
メンテナーはふだん 1 日以内に返信
openai/openai-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 62/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 70/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
topoteretes/cognee#5773 ·
メンテナーはふだん 1 日以内に返信
-
MiKTeX: a failed build reports no errors because texify --quiet switches the log to file:line: styleオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
SublimeText/LaTeXTools#1718 ·