I think I found the problem
Just change the last two lines:
material.node_tree.links.new(bakeGroup.outputs['shaderIn'], mainShader.outputs[0])
material.node_tree.links.new(material_output.inputs[0], bakeGroup.inputs['shaderOut'])
To
material.node_tree.links.new(dummy_group_node.inputs["shaderIn"], mainShader.outputs[0])
material.node_tree.links.new(material_output.inputs[0], dummy_group_node.outputs['shaderOut'])
Now heres the explanation
You are using bakeGroup to link, the problem is here, bakeGroup is a NodeTree instead of node.
the Group node itself is the dummy_group_node when you created it above
So You just need to change bakeGroup to dummy_group_node
I hope this helps