keras/src/tree/tree_api.py/assert_same_structure
def assert_same_structure(a, b, check_types=True):
    """Asserts that two structures are nested in the same way.

    Note that namedtuples with identical name and fields will not be considered
    as same structures even `check_types=False`.

    Examples:

    >>> keras.tree.assert_same_structure([(0, 1)], [(2, 3)])

    >>> Foo = collections.namedtuple('Foo', ['a', 'b'])
    >>> AlsoFoo = collections.namedtuple('Foo', ['a', 'b'])
    >>> keras.tree.assert_same_structure(Foo(0, 1), Foo(2, 3))
    >>> keras.tree.assert_same_structure(Foo(0, 1), AlsoFoo(2, 3))
    Traceback (most recent call last):
        ...
    ValueError: `a` and `b` don't have the same structure.
    ...

    Args:
        a: an arbitrarily nested structure.
        b: an arbitrarily nested structure.
        check_types: if `True` (default) types of leaves are checked as well.
    """
    return tree_impl.assert_same_structure(a, b, check_types=check_types)