keras/src/tree/optree_impl.py/_yield_sorted_items
def _yield_sorted_items(iterable):
    # TODO: Support attrs library
    if isinstance(iterable, collections.abc.Mapping):
        # Iterate through dictionaries in a deterministic order by sorting the
        # keys. Notice this means that we ignore the original order of
        # `OrderedDict` instances. This is intentional, to avoid potential bugs
        # caused by mixing ordered and plain dicts (e.g., flattening a dict but
        # using a corresponding `OrderedDict` to pack it back).
        for key in sorted(iterable):
            yield key, iterable[key]
    elif optree.is_namedtuple(iterable):
        for field in iterable._fields:
            yield (field, getattr(iterable, field))
    else:
        for item in enumerate(iterable):
            yield item