created_at
Input should be a valid datetime or date, unexpected extra characters at the end of the input [type=datetime_from_date_parsing, input_value='2025-07-10 12:58:27.856032+00', input_type=str]
For further information visit https://errors.pydantic.dev/2.11/v/datetime_from_date_parsing
my field is declared like this:
created_at: datetime = Field(default_factory=datetime.now)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, this is a common Pydantic error, and it's a bit misleading.
The input string '2025-07-10 12:58:27.856032+00' is actually a perfectly valid ISO 8601 formatted datetime string, including fractional seconds and a timezone offset (+00 for UTC). Standard Python's datetime.fromisoformat handles this format correctly.
The error datetime_from_date_parsing suggests that Pydantic's validation logic might be attempting to parse the string as a date first for some reason, finding the time and timezone components ("unexpected extra characters"), and failing before it fully attempts to parse it as a complete datetime. This can sometimes happen depending on the exact Pydantic version, input source, or internal parsing strategies.
Since the string is valid, the fix usually involves ensuring Pydantic uses a more robust parsing mechanism or explicitly handling the parsing. Here are the most common solutions:
Install python-dateutil:
Pydantic can use the dateutil library for more flexible datetime parsing if it's installed. This often resolves issues with valid but slightly complex formats like yours.
bash
pip install python-dateutil
After installing, Pydantic might automatically use dateutil.parser.parse when validating the string input for a datetime field, which is generally more forgiving and powerful than datetime.fromisoformat.
Use a Custom Validator:
You can explicitly tell Pydantic how to parse the string using a field validator.
python
from datetime import datetime
from pydantic import BaseModel, Field, field_validator, ValidationInfo
class MyModel(BaseModel):
created_at: datetime = Field(default_factory=datetime.now)
# Use v2 syntax for validators
@field_validator('created_at', mode='before') # 'before' processes input before Pydantic's default parsing
@classmethod
def parse_created_at(cls, value: any):
if isinstance(value, str):
# Try parsing using datetime.fromisoformat, or dateutil.parser if available
try:
# This is what Pydantic *should* do by default, but explicitly doing it
# in a validator can sometimes force the correct path.
# If dateutil is installed, dateutil.parser.parse is more robust
from dateutil import parser
return parser.parse(value)
except (ValueError, TypeError):
# Fallback or raise a specific error if parsing fails
raise ValueError("Invalid datetime format for created_at")
# Let Pydantic handle non-string inputs (like datetime objects)
return value
# Example Usage:
data = {'created_at': '2025-07-10 12:58:27.856032+00'}
model_instance = MyModel(**data)
print(model_instance.created_at)
# Example with default:
model_instance_default = MyModel()
print(model_instance_default.created_at)
Recommendation:
Try Option 1 (install python-dateutil) first, as it's the simplest and often resolves this specific error because dateutil is more robust at parsing various datetime string formats.
If that doesn't work, implementing a Custom Validator (Option 2) gives you explicit control over the parsing process and is a reliable way to handle specific input formats. Using dateutil.parser.parse within the validator is a good strategy.