How to get depth data using python api
Ok. I finally got the depth data by directly connecting the z data to the viewer node:

# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')    

# create output node
v = tree.nodes.new('CompositorNodeViewer')   
v.use_alpha = False

# Links
links.new(rl.outputs['Z'], v.inputs[0]) # link Z to output

# render
bpy.ops.render.render()


And then I output the information into an npz file to be used within python

# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels
print(len(pixels)) # size is always width * height * 4 (rgba)

# copy buffer to numpy array for faster manipulation
arr = np.array(pixels[:])
fname2 = "depth.npz" 
with open(fname2, "wb") as f:
    np.savez(f, arr)