PydanticUserError with defer_build=True and pydantic 2.12 in multiprocess forkserver workers
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
Inizia riproducendo il malfunzionamento di forkserver con le versioni segnalate di Python, openai, pydantic e multiprocess. Segui openai/_models.py attraverso construct(), construct_type() e _get_extra_fields_type(), quindi esamina pydantic/_internal/_mock_val_ser.py attorno a _get_built(). Il lavoro è completato quando la deserializzazione delle chat completion rimane affidabile tra i worker di forkserver con entrambe le impostazioni di DEFER_PYDANTIC_BUILD, con una copertura di regressione per il malfunzionamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 31.8k
- Fork
- 7.3k
- Merge medio
- 1g 3h
- PR unite (30g)
- 128
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
openai/openai-python#4022 · 6 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providersForse già presa @mohmedmm l’ha presa 5 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
openai/openai-python#4017 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Querystring drops explicit empty-string scalar valuesForse già presa @sylvesterkaczmarek l’ha presa 28 giorni fa. Apertasdk-breaking-change v4
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
openai/openai-python#3837 ·
I maintainer di solito rispondono entro 1 giorno
-
Define + export `ServiceTiers` string literalForse già presa @SparshGarg999 l’ha presa 54 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openai/openai-python#3556 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Empty OPENAI_BASE_URL prevents fallback to default API endpointForse già presa @Sehastrajit-S l’ha presa 21 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openai/openai-python#2927 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
epam/ai-dial-quickapps-backend#628 ·
I maintainer di solito rispondono entro 2 giorni
-
0xlau.dev 已失效,切换成 timlau.meAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 69/100
timqian/chinese-independent-blogs#2235 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
eclipse-score/coverage_tool#27 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
bojieli/ai-agent-book#1174 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
RedHatQE/mtv-api-tests#721 ·
I maintainer di solito rispondono entro 1 giorno