{"record":{"id":"d4653104bf3f6d18","repo":"openai/openai-python","slug":"invalid-datetime-format","errorCode":null,"errorMessage":"invalid datetime format","messagePattern":"invalid datetime format","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/openai/_utils/_datetime_parse.py","lineNumber":93,"sourceCode":"\n    Raise ValueError if the input is well formatted but not a valid datetime.\n    Raise ValueError if the input isn't well formatted.\n    \"\"\"\n    if isinstance(value, datetime):\n        return value\n\n    number = _get_numeric(value, \"datetime\")\n    if number is not None:\n        return _from_unix_seconds(number)\n\n    if isinstance(value, bytes):\n        value = value.decode()\n\n    assert not isinstance(value, (float, int))\n\n    match = datetime_re.match(value)\n    if match is None:\n        raise ValueError(\"invalid datetime format\")\n\n    kw = match.groupdict()\n    if kw[\"microsecond\"]:\n        kw[\"microsecond\"] = kw[\"microsecond\"].ljust(6, \"0\")\n\n    tzinfo = _parse_timezone(kw.pop(\"tzinfo\"))\n    kw_: Dict[str, Union[None, int, timezone]] = {k: int(v) for k, v in kw.items() if v is not None}\n    kw_[\"tzinfo\"] = tzinfo\n\n    return datetime(**kw_)  # type: ignore\n\n\ndef parse_date(value: Union[date, StrBytesIntFloat]) -> date:\n    \"\"\"\n    Parse a date/int/float/string and return a datetime.date.\n\n    Raise ValueError if the input is well formatted but not a valid date.\n    Raise ValueError if the input isn't well formatted.","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_utils/_datetime_parse.py#L75-L111","documentation":"parse_datetime implements the SDK's ISO-8601 datetime parsing. After asserting the value is a str (not a number) it matches against a strict datetime regex; a mismatch means the string is not a recognizable ISO-8601 datetime (e.g. missing timezone where required, wrong separators, or epoch-style strings). The ValueError signals malformed datetime input, typically when server responses change shape or when local code feeds non-ISO strings into SDK model construction.","triggerScenarios":"A response/model field typed as datetime receives a string like '2024/01/01', 'Jan 1 2024', an epoch-seconds string ('1700000000'), or any string not matching the ISO-8601 regex (date + optional time + timezone component).","commonSituations":"API returns a new or non-standard timestamp format after a version change; constructing models from your own JSON with locale-formatted dates; tests with hand-written date strings; mixing datetime with fields documented as date-only.","solutions":["Normalize the input to ISO-8601 (datetime.isoformat()) before constructing the model","If the value is epoch seconds, pass it as int/float so the unix-seconds path is used instead of regex parsing","If the string is date-only, use the date-typed field/parse_date instead of datetime","Validate incoming strings with datetime.fromisoformat or a regex before assignment"],"exampleFix":"# before\nm = MyModel(created_at=\"Jan 5 2024 10:00\")\n\n# after\nfrom datetime import datetime\nm = MyModel(created_at=datetime(2024,1,5,10,0).isoformat())","handlingStrategy":"validation","validationCode":"from datetime import datetime\ntry:\n    datetime.fromisoformat(value.replace(\"Z\", \"+00:00\"))\nexcept ValueError:\n    raise ValueError(f\"not ISO-8601: {value!r}\")","typeGuard":"import re\nISO_DT_RE = re.compile(r\"^\\d{4}-\\d{2}-\\d{2}([T ]\\d{2}:\\d{2}(:\\d{2}(\\.\\d+)?)?(Z|[+-]\\d{2}:?\\d{2})?)?$\")\ndef is_iso_datetime(v: str) -> bool: return bool(ISO_DT_RE.match(v))","tryCatchPattern":"try:\n    parse_datetime(s)\nexcept ValueError:\n    s = datetime.fromtimestamp(int(s)).isoformat() if s.isdigit() else fallback(s)","preventionTips":["Always emit datetime.isoformat() from producers","Document expected timestamp format in API contracts","Validate at ingestion before model construction"],"tags":["valueerror","datetime","iso8601","parsing"],"backgroundTag":"invalid-datetime-format","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}