"len(categories[0].keys()) == 1" is an odd test, and brittle. What about this?:

if categories and 'categoryName' not in categories[0]: