python data_error ai_generated true

pydantic.errors.DatetimeError: value is not a valid datetime

ID: python/pydantic-datetime-timezone-naive

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

A datetime field with timezone-aware requirement receives a naive datetime (no timezone info).

generic

中文

带有时区感知要求的日期时间字段接收到没有时区信息的朴素日期时间。

Workarounds

  1. 95% success
    Use timezone-aware datetime: datetime.now(timezone.utc)
  2. 85% success
    Allow naive datetime by removing tz constraint from Field

Dead Ends

Common approaches that don't work:

  1. 90% fail

    utcnow returns naive datetime, not timezone-aware.

  2. 80% fail

    This makes it naive, still fails.