keras/src/tree/optree_impl.py/lists_to_tuples
def lists_to_tuples(structure):

    def sequence_fn(instance, args):
        if isinstance(instance, list):
            return tuple(args)
        return _sequence_like(instance, args)

    return pack_sequence_as(
        structure, flatten(structure), sequence_fn=sequence_fn
    )