python
data_error
ai_generated
true
pydantic_core._pydantic_core.ValidationError: 1 validation error for UserCreate email Field required [type=missing, input_value={'name': 'a'}, input_type=dict]
ID: python/pydantic-field-required-missing
80%Fix Rate
87%Confidence
0Evidence
2024-05-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
The required field 'email' is absent from the input dict. In Pydantic v2 a field without a default is required; missing key triggers 'missing'.
generic中文
输入字典中缺少必填字段 'email'。在 Pydantic v2 中,没有默认值的字段是必填的;键缺失会触发 missing 错误。
Workarounds
-
92% success
from typing import Optional from pydantic import BaseModel class UserCreate(BaseModel): email: Optional[str] = None -
80% success
from pydantic import BaseModel, Field class UserCreate(BaseModel): email: str = Field(default_factory=lambda: '[email protected]')
Dead Ends
Common approaches that don't work:
-
55% fail
Type checker complains and downstream code gets None where str is expected, causing AttributeError later.
-
50% fail
Empty string bypasses required validation and pollutes DB with blank emails.
-
40% fail
Silently swallows real data problems; later inserts fail with NOT NULL constraint.