pydantic isn’t complaining about the type of the value you give it (it’s clearly a datetime-looking string), it’s complaining about the exact text format.
The parser used in Pydantic v2 follows the ISO-8601 rules very strictly, and according to those rules the time-zone part must contain hours and minutes:
✅ +00:00
✅ -04:30
✅ Z (shorthand for +00:00)
But you’re passing
2025-07-10 12:58:27.856032+00
^^ <-- minutes are missing
So the parser stops at the +00, doesn’t know what to do with the trailing characters, and raises
unexpected extra characters at the end of the input
How to fix
-
Make sure whatever is producing that string adds the minutes in the offset (or uses the Z suffix):
"2025-07-10 12:58:27.856032+00:00"
# or
"2025-07-10T12:58:27.856032Z"
-
Or—better still—hand Pydantic a real datetime object instead of a string:
from datetime import datetime, timezone
MyModel(created_at=datetime(2025, 7, 10, 12, 58, 27, 856032, tzinfo=timezone.utc))
-
If you don’t control the upstream data and must accept +00, add a small pre-validator:
from datetime import datetime, timezone
from pydantic import BaseModel, Field, field_validator
class MyModel(BaseModel):
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
@field_validator('created_at', mode='before')
def fix_plus_zero_offset(cls, v):
if isinstance(v, str) and v.endswith('+00'):
# Convert ...+00 -> ...+00:00
v = v + ':00'
return v
Side note: your default_factory should ideally create a timezone-aware datetime so that every value of created_at is comparable:
from datetime import datetime, timezone
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
In short: add the missing :00 (or a Z) to your timestamp, or supply an actual datetime object, and the error disappears.