{"record":{"id":"2921f4e063a04de1","repo":"apache/beam","slug":"cannot-convert-s-to-micro-seconds","errorCode":null,"errorMessage":"cannot convert %s to micro seconds","messagePattern":"cannot convert (.+?) to micro seconds","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/utils/proto_utils.py","lineNumber":142,"sourceCode":"  # maximum allowable timestamp, so we cannot use the built-in conversion.\n  elif isinstance(result, timestamp_pb2.Timestamp):\n    result.seconds = micros // _SECONDS_TO_MICROS\n    result.nanos = (micros % _SECONDS_TO_MICROS) * _MICROS_TO_NANOS\n    return result\n  else:\n    raise RuntimeError('cannot convert the micro seconds to %s' % cls)\n\n\ndef to_micros(value: Union[duration_pb2.Duration, timestamp_pb2.Timestamp]):\n  if isinstance(value, duration_pb2.Duration):\n    return value.ToMicroseconds()\n  # Protobuf 5.x enforces a maximum timestamp value less than the Beam\n  # maximum allowable timestamp, so we cannot use the built-in conversion.\n  elif isinstance(value, timestamp_pb2.Timestamp):\n    micros = value.seconds * _SECONDS_TO_MICROS\n    return micros + (value.nanos // _MICROS_TO_NANOS)\n  else:\n    raise RuntimeError('cannot convert %s to micro seconds' % value)\n\n\ndef to_Timestamp(time: Union[int, float]) -> timestamp_pb2.Timestamp:\n  \"\"\"Convert a float returned by time.time() to a Timestamp.\n  \"\"\"\n  seconds = int(time)\n  nanos = int((time - seconds) * 10**9)\n  return timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos)\n\n\ndef from_Timestamp(timestamp: timestamp_pb2.Timestamp) -> float:\n  \"\"\"Convert a Timestamp to a float expressed as seconds since the epoch.\n  \"\"\"\n  return timestamp.seconds + float(timestamp.nanos) / 10**9\n","sourceCodeStart":124,"sourceCodeEnd":157,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/utils/proto_utils.py#L124-L157","documentation":"to_micros converts a protobuf Duration or Timestamp to integer microseconds. Passing any other object falls through the isinstance checks and raises RuntimeError, since no conversion is defined for that type.","triggerScenarios":"Calling proto_utils.to_micros(value) with a datetime, int, float, or non-Duration/non-Timestamp protobuf message.","commonSituations":"Mixing time.time() floats and protobuf timestamps; refactors changing function signatures; reading a Timestamp from one proto and passing a nested field of the wrong type.","solutions":["Convert the value to a protobuf Timestamp first (e.g. to_Timestamp(time.time()))","Pass only duration_pb2.Duration or timestamp_pb2.Timestamp values","Check the source of the value — a schema/parse change may have altered its type"],"exampleFix":"// before\nmicros = proto_utils.to_micros(time.time())\n// after\nmicros = proto_utils.to_micros(proto_utils.to_Timestamp(time.time()))","handlingStrategy":"type-guard","validationCode":"if not isinstance(value, (duration_pb2.Duration, timestamp_pb2.Timestamp)):\n    raise TypeError(f'expected Duration/Timestamp, got {type(value).__name__}')","typeGuard":"def is_micros_convertible(value):\n    return isinstance(value, (duration_pb2.Duration, timestamp_pb2.Timestamp))","tryCatchPattern":"try:\n    micros = proto_utils.to_micros(value)\nexcept RuntimeError as e:\n    logging.error('cannot convert to micros: %s', e)\n    micros = proto_utils.to_micros(proto_utils.to_Timestamp(time.time()))","preventionTips":["Convert raw floats/datetimes to protobuf Timestamp before calling","Annotate variables with the protobuf message type","Validate message types at schema boundaries"],"tags":["protobuf","timestamp","type-mismatch","python"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}