Hello ppant,
I've just copied your code to the Console pane for the random multispectral project (had to perform chunk variable initialization, though), and it worked as expected without the reported error.
I have used 1.7.3 release version. Please check, if you observe the error on your side in the latest release version. I can also suggest to check, if the problem persists after Metashape re-start.