def unflatten(arg: dict[str, t.Any]) -> dict[str, t.Any]:
    """Unflatten nested dict/array data.

    This function takes a single argument which may either be a
    ``dict`` (or any object having a dict-like ``.items()`` or
    ``.iteritems()`` method) or a sequence of ``(key, value)`` pairs.
    The keys in the ``dict`` or sequence should must all be strings.

    Examples
    --------

    Nested ``dict``\\s::

    >>> unflatten({'foo.bar': 'val'})
    {'foo': {'bar': 'val'}}

    Nested ``list``::

    >>> unflatten({'foo[0]': 'val', 'foo[1]': 'bar'})
    {'foo': ['val', 'bar']}

    Nested ``list``\\s::

    >>> unflatten({'foo[0][0]': 'val'})
    {'foo': [['val']]}

    Lists of ``dict``\\s::

    >>> unflatten({'foo[0].bar': 'val',
    ...            'foo[1].baz': 'x'})
    {'foo': [{'bar': 'val'}, {'baz': 'x'}]}

    """
    if hasattr(arg, "items"):
        items = arg.items()
    else:
        items = arg

    data: dict[str, t.Any] = {}
    holders: list[t.Any] = []
    for flat_key, val in items:
        parsed_key = _parse_key(flat_key)
        obj = data
        for depth, (key, next_key) in enumerate(zip(parsed_key, parsed_key[1:]), 1):
            if isinstance(next_key, string_type):
                holder_type = _dict_holder
            else:
                holder_type = _list_holder

            if key not in obj:
                obj[key] = holder_type(_unparse_key(parsed_key[:depth]))
                holders.append((obj, key))
            elif not isinstance(obj[key], holder_type):
                raise ValueError(
                    "conflicting types %s and %s for key %r"
                    % (
                        _node_type(obj[key]),
                        holder_type.node_type,
                        _unparse_key(parsed_key[:depth]),
                    )
                )
            obj = obj[key]

        last_key = parsed_key[-1]
        if isinstance(obj.get(last_key), _holder):
            raise ValueError(
                "conflicting types %s and terminal for key %r"
                % (_node_type(obj[last_key]), flat_key)
            )
        obj[last_key] = val

    for obj, key in reversed(holders):
        obj[key] = obj[key].getvalue()

    return data
Unflatten nested dict/array data.

This function takes a single argument which may either be a
``dict`` (or any object having a dict-like ``.items()`` or
``.iteritems()`` method) or a sequence of ``(key, value)`` pairs.
The keys in the ``dict`` or sequence should must all be strings.

Examples
--------

Nested ``dict``\\s::

>>> unflatten({'foo.bar': 'val'})
{'foo': {'bar': 'val'}}

Nested ``list``::

>>> unflatten({'foo[0]': 'val', 'foo[1]': 'bar'})
{'foo': ['val', 'bar']}

Nested ``list``\\s::

>>> unflatten({'foo[0][0]': 'val'})
{'foo': [['val']]}

Lists of ``dict``\\s::

>>> unflatten({'foo[0].bar': 'val',
...            'foo[1].baz': 'x'})
{'foo': [{'bar': 'val'}, {'baz': 'x'}]}
src/bentoml/_internal/utils/unflatten.py