The game has two kinds of enchantments, StoredEnchantments for books and Enchantments for everything else. The loot table function enchant_randomly will give the wrong kind of enchantment to books if another function has already enchanted it.
The same thing happens if the function enchant_randomly is used twice, with the second enchantment being bugged.
If the function enchant_randomly is used on its own, then it gives a StoredEnchantment as intended.
If the function enchant_randomly is used, then enchant_with_levels, then enchant_with_levels won't give any enchantments at all.