How does a generic bluetooth mouse / keyboard work with macos without installing any driver? Some generic brand bluetooth keyboard / mouse work fine on macos. Does macos have a built-in common driver api (for mouse and keyboard) that all these device use or is it some specification in the Bluetooth stack that allows this?
Yes. It's the generic  USB HID (Human Interface Device) driver/kext or Bluetooth HID profile
All operating systems have a generic HID driver  so that USB and Bluetooth keyboards, mice, and track pads will work out of the box without the need for a manufacturer supplied driver. That said, specialized keyboards and mice like those used for gaming, for example, will operate as  a generic device but, to use the  specialized functions or extra buttons, you'll need the manufacturer driver.