private JsonNode merge(JsonNode target, JsonNode source, Stack<String> path) {
        if (source == null || source.isNull()) {
            return target;
        }
        if (target == null || target.isNull()) {
            if (source.isObject()) {
                target = ProxyUtil.MAPPER.createObjectNode();
            } else if (source.isArray()) {
                target = ProxyUtil.MAPPER.createArrayNode();
            } else {
                return source;
            }
        }
        if (target.isArray() && source.isArray()) {
            return mergeArrays((ArrayNode) target, (ArrayNode) source, path);
        } else if (target.isObject() && source.isObject()) {
            return mergeObjects((ObjectNode) target, (ObjectNode) source, path);
        } else if (target.isIntegralNumber() && source.isIntegralNumber()) {
            return source;
        } else if (target.isFloatingPointNumber() && source.isFloatingPointNumber()) {
            return source;
        } else if (target.isBoolean() && source.isBoolean()) {
            return source;
        } else if (target.getNodeType() == JsonNodeType.STRING && source.getNodeType() == JsonNodeType.STRING) {
            String text = target.textValue() + source.textValue();
            return new TextNode(text);
        }

        throw new IllegalArgumentException(String.format("Can't merge %s into %s at path %s", source.asText(), target.asText(), String.join(".", path)));
    }
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