private ArrayNode mergeIndexedArrays(ArrayNode target, ArrayNode source, Stack<String> path) {
        for (int i = 0; i < source.size(); i++) {
            JsonNode elem = source.get(i);
            if (!elem.isObject()) {
                throw new IllegalArgumentException(LIST_OF_DICTS_ERROR_MESSAGE);
            }
            JsonNode indexNode = elem.get("index");
            if (!indexNode.isInt()) {
                throw new IllegalArgumentException(INDEX_ERROR_MESSAGE);
            }
            int index = indexNode.asInt();
            if (index < target.size()) {
                path.push("[" + i + "]");
                merge(target.get(index), elem, path);
                path.pop();
            } else {
                for (int j = target.size(); j < index; j++) {
                    ObjectNode objectNode = ProxyUtil.MAPPER.createObjectNode();
                    objectNode.put("index", j);
                    target.add(objectNode);
                }
                target.add(elem);
            }
        }
        return target;
    }
The utility merges chunks received from a streaming response. The original reference implementation is provided
<a href="https://github.com/epam/ai-dial-sdk/blob/0.10.1/aidial_sdk/utils/merge_chunks.py">here</a>.
server/src/main/java/com/epam/aidial/core/server/util/MergeChunks.java