def cleanup_indices()

in aidial_sdk/utils/merge_chunks.py [0:0]


def cleanup_indices(chunk: T) -> T:
    """
    Recursively remove all "index" fields inside list elements in the given chunk.
    The chunk is modified in-place.
    """

    if isinstance(chunk, list):
        ret = []
        for elem in chunk:
            if isinstance(elem, dict) and "index" in elem:
                elem = elem.copy()
                del elem["index"]
            ret.append(cleanup_indices(elem))
        return cast(T, ret)

    if isinstance(chunk, dict):
        return cast(
            T, {key: cleanup_indices(value) for key, value in chunk.items()}
        )

    return chunk