this is a potential memory problem

first_item = ids.next()
id_type = type(first_item)
ids = itertools.chain(first_item, ids)