{"record":{"id":"c8ebe5a647e05867","repo":"openai/openai-python","slug":"invalid-type-expected-native-expected-type-str","errorCode":null,"errorMessage":"invalid type; expected {native_expected_type}, string, bytes, int or float","messagePattern":"invalid type; expected (.+?), string, bytes, int or float","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/openai/_utils/_datetime_parse.py","lineNumber":41,"sourceCode":"\n\nEPOCH = datetime(1970, 1, 1)\n# if greater than this, the number is in ms, if less than or equal it's in seconds\n# (in seconds this is 11th October 2603, in ms it's 20th August 1970)\nMS_WATERSHED = int(2e10)\n# slightly more than datetime.max in ns - (datetime.max - EPOCH).total_seconds() * 1e9\nMAX_NUMBER = int(3e20)\n\n\ndef _get_numeric(value: StrBytesIntFloat, native_expected_type: str) -> Union[None, int, float]:\n    if isinstance(value, (int, float)):\n        return value\n    try:\n        return float(value)\n    except ValueError:\n        return None\n    except TypeError:\n        raise TypeError(f\"invalid type; expected {native_expected_type}, string, bytes, int or float\") from None\n\n\ndef _from_unix_seconds(seconds: Union[int, float]) -> datetime:\n    if seconds > MAX_NUMBER:\n        return datetime.max\n    elif seconds < -MAX_NUMBER:\n        return datetime.min\n\n    while abs(seconds) > MS_WATERSHED:\n        seconds /= 1000\n    dt = EPOCH + timedelta(seconds=seconds)\n    return dt.replace(tzinfo=timezone.utc)\n\n\ndef _parse_timezone(value: Optional[str]) -> Union[None, int, timezone]:\n    if value == \"Z\":\n        return timezone.utc\n    elif value is not None:","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_utils/_datetime_parse.py#L23-L59","documentation":"The SDK's datetime/date coercion helper accepts str, bytes, int, or float and calls float(value) to convert. When the value's type is neither convertible (TypeError from float(), e.g. None, list, dict, or an arbitrary object) the helper raises this TypeError to signal that the input type is fundamentally unconvertible. It is raised from within parse_datetime/parse_date when coercing a raw value, usually because the caller passed a None or a non-scalar where a datetime-like value was expected.","triggerScenarios":"Passing None, a dict, list, datetime object already parsed, or any non-string/number object to a field the SDK model coerces into a datetime or date (e.g. constructing model objects locally from raw dicts, or datetime parsing of untyped payloads via parse_datetime/parse_date).","commonSituations":"Building request/response models manually from JSON where a timestamp field is null; copying pydantic-v1 style code that passed datetime fields as dicts; upgrading code that previously silently passed through bad values before coercion was added.","solutions":["Ensure the field is a str/bytes/int/float (e.g. an ISO-8601 string or unix seconds) before assignment","Fix the upstream data so the timestamp field is never None or a container","If the value may be absent, use a conditional/exclude the field rather than passing None","Catch TypeError at the boundary that produces the raw data and log which field is malformed"],"exampleFix":"# before\nobj = SomeModel(created=None)\n\n# after\nobj = SomeModel(created=int(time.time()))\n# or omit the field if the model allows","handlingStrategy":"validation","validationCode":"if not isinstance(value, (str, bytes, int, float)):\n    raise ValueError(f\"bad timestamp: {value!r}\")","typeGuard":"def is_coercible_ts(v: object) -> bool:\n    return isinstance(v, (str, bytes, int, float)) and not isinstance(v, bool)","tryCatchPattern":"try:\n    parse_datetime(raw)\nexcept TypeError as e:\n    raise ValueError(f\"field X must be str/int/float, got {type(raw)}\") from e","preventionTips":["Normalize timestamps at your data boundary","Never pass None for datetime-typed fields","Use .isoformat() on datetime objects before assignment"],"tags":["typeerror","datetime","coercion","validation"],"backgroundTag":"type-coercion-failed","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}