python data_error ai_generated true

pydantic.errors.EmailError: value is not a valid email address

ID: python/pydantic-email-format-invalid

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

Using EmailStr type, the input string does not conform to email format (e.g., missing @, invalid domain).

generic

中文

使用 EmailStr 类型时,输入字符串不符合电子邮件格式(例如缺少 @、域名无效)。

Workarounds

  1. 95% success
    Use EmailStr from pydantic: email: EmailStr
  2. 90% success
    Sanitize input before validation: @field_validator('email', mode='before') def clean(cls, v): return v.strip()

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Custom regex may not capture all email edge cases, leading to false positives.

  2. 70% fail

    Empty string is not a valid email, even if optional.