{"id":"f39cd71b4b15fbeb","repo":"aio-libs/aiohttp","slug":"unsupported-type-for-last-modified-type-value","errorCode":null,"errorMessage":"Unsupported type for last_modified: {type(value).__name__}","messagePattern":"Unsupported type for last_modified: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":279,"sourceCode":"        self, value: int | float | datetime.datetime | str | None\n    ) -> None:\n        if value is None:\n            self._headers.pop(hdrs.LAST_MODIFIED, None)\n        elif isinstance(value, (int, float)):\n            self._headers[hdrs.LAST_MODIFIED] = time.strftime(\n                \"%a, %d %b %Y %H:%M:%S GMT\", time.gmtime(math.ceil(value))\n            )\n        elif isinstance(value, datetime.datetime):\n            if value.microsecond:\n                value = value.replace(microsecond=0) + datetime.timedelta(seconds=1)\n            self._headers[hdrs.LAST_MODIFIED] = time.strftime(\n                \"%a, %d %b %Y %H:%M:%S GMT\", value.utctimetuple()\n            )\n        elif isinstance(value, str):\n            self._headers[hdrs.LAST_MODIFIED] = value\n        else:\n            msg = f\"Unsupported type for last_modified: {type(value).__name__}\"  # type: ignore[unreachable]\n            raise TypeError(msg)\n\n    @property\n    def etag(self) -> ETag | None:\n        quoted_value = self._headers.get(hdrs.ETAG)\n        if not quoted_value:\n            return None\n        elif quoted_value == ETAG_ANY:\n            return ETag(value=ETAG_ANY)\n        match = QUOTED_ETAG_RE.fullmatch(quoted_value)\n        if not match:\n            return None\n        is_weak, value = match.group(1, 2)\n        return ETag(\n            is_weak=bool(is_weak),\n            value=value,\n        )\n\n    @etag.setter","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L261-L297","documentation":"The last_modified setter on StreamResponse only accepts int, float (unix timestamps), datetime.datetime, str, or None. Any other type falls through to a TypeError. A plain datetime.date (without time) is NOT accepted and is the most common culprit.","triggerScenarios":"Assigning `resp.last_modified = some_date` where some_date is a datetime.date (not datetime.datetime), a tuple, a pd.Timestamp without datetime inheritance, or any custom object. Intended types are listed in the isinstance checks at lines 265-276.","commonSituations":"Using a column from pandas/sql that yields date objects; passing a struct_time; forgetting to convert a date to datetime before assignment.","solutions":["Convert date to datetime: resp.last_modified = datetime.datetime.combine(d, datetime.time.min).","Pass a unix timestamp (int/float) or an HTTP-date string instead.","If you already have a datetime.datetime, just assign it directly (microseconds are rounded up automatically)."],"exampleFix":"# before\nresp.last_modified = record.updated_at.date()  # date, not datetime -> TypeError\n\n# after\nimport datetime\nresp.last_modified = datetime.datetime.combine(record.updated_at.date(), datetime.time.min())","handlingStrategy":"type-guard","validationCode":"import datetime\n\ndef coerce_last_modified(value):\n    if isinstance(value, datetime.date) and not isinstance(value, datetime.datetime):\n        return datetime.datetime.combine(value, datetime.time.min)\n    return value\n\nresp.last_modified = coerce_last_modified(value)","typeGuard":"import datetime\n\ndef is_supported_last_modified(value) -> bool:\n    return value is None or isinstance(value, (int, float, datetime.datetime, str))","tryCatchPattern":null,"preventionTips":["Check the type of ORM/dataframe timestamp columns before assignment.","Remember datetime.date is NOT accepted; only datetime.datetime is.","Pass unix timestamps (int/float) when in doubt — they always work."],"tags":["http","headers","last-modified","type-check","response"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}