{"record":{"id":"d13ce08b7a4e1cd0","repo":"apache/beam","slug":"bad-timestamp-value-for-message-s-s","errorCode":null,"errorMessage":"Bad timestamp value for message %s: %s","messagePattern":"Bad timestamp value for message (.+?): (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/direct/transform_evaluator.py","lineNumber":738,"sourceCode":"        try:\n          timestamp = Timestamp(micros=int(rfc3339_or_milli) * 1000)\n        except ValueError:\n          try:\n            timestamp = Timestamp.from_rfc3339(rfc3339_or_milli)\n          except ValueError as e:\n            raise ValueError('Bad timestamp value: %s' % e)\n        if timestamp.precision() > Timestamp.MICROS_PRECISION:\n          # Element timestamps are limited to microsecond resolution, so\n          # ignore sub-microsecond digits, as the Dataflow service does.\n          timestamp = timestamp.to_precision(\n              Timestamp.MICROS_PRECISION, allow_lossy_conversion=True)\n      else:\n        if message.publish_time is None:\n          raise ValueError('No publish time present in message: %s' % message)\n        try:\n          timestamp = Timestamp.from_utc_datetime(message.publish_time)\n        except ValueError as e:\n          raise ValueError('Bad timestamp value for message %s: %s', message, e)\n\n      return timestamp, parsed_message\n\n    # Because of the AutoAck, we are not able to reread messages if this\n    # evaluator fails with an exception before emitting a bundle. However,\n    # the DirectRunner currently doesn't retry work items anyway, so the\n    # pipeline would enter an inconsistent state on any error.\n    sub_client = self._get_subscriber_client(self._applied_ptransform)\n    response = sub_client.pull(\n        subscription=self._sub_name, max_messages=10, timeout=30)\n    results = [_get_element(rm.message) for rm in response.received_messages]\n    ack_ids = [rm.ack_id for rm in response.received_messages]\n    if ack_ids:\n      sub_client.acknowledge(subscription=self._sub_name, ack_ids=ack_ids)\n\n    return results\n\n  def finish_bundle(self) -> TransformResult:","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/direct/transform_evaluator.py#L720-L756","documentation":"When converting the message's publish_time datetime via Timestamp.from_utc_datetime, a failure is re-raised as ValueError('Bad timestamp value for message %s: %s'). Note the raise uses %s formatting with a tuple-style arg list in the source, so the message string itself may not interpolate correctly, but the cause is an unparseable publish_time datetime.","triggerScenarios":"A Pub/Sub message (read without timestamp_attribute) whose publish_time is set to a value Timestamp.from_utc_datetime rejects — e.g. a naive datetime, out-of-range year, or a non-datetime object placed in publish_time.","commonSituations":"Manual message construction with naive datetimes (no tzinfo) or datetime values outside Timestamp's representable range; mocks supplying wrong types for publish_time; data injected from other systems without normalization.","solutions":["Set publish_time to a timezone-aware UTC datetime: datetime.now(timezone.utc).","Clamp datetime values to the range Timestamp supports (avoid year 1 / far-future values).","Use timestamp_attribute with a valid RFC 3339 string to bypass publish_time parsing.","Inspect the wrapped cause (e) to see exactly why from_utc_datetime rejected the value."],"exampleFix":"// before\nmsg.publish_time = datetime(1000, 1, 1)  # naive, out of range\n// after\nmsg.publish_time = datetime.now(timezone.utc)","handlingStrategy":"validation","validationCode":"from datetime import datetime, timezone\nif msg.publish_time is not None:\n    assert msg.publish_time.tzinfo is not None, 'publish_time must be timezone-aware UTC'","typeGuard":"def is_valid_publish_time(pt) -> bool:\n    from datetime import datetime, timezone\n    return isinstance(pt, datetime) and pt.tzinfo is not None and datetime(1970, 1, 1, tzinfo=timezone.utc) <= pt","tryCatchPattern":"try:\n    read_pubsub()\nexcept ValueError as e:\n    if 'Bad timestamp value for message' in str(e):\n        quarantine_message_and_log(e)","preventionTips":["Only assign timezone-aware UTC datetimes to publish_time.","Avoid datetimes outside Timestamp's supported range in mocks.","Prefer timestamp_attribute with RFC 3339 strings for full control."],"tags":["python","apache-beam","pubsub","timestamp"],"backgroundTag":"invalid-date-format","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}