I am creating a tool using arcpy to get a web map as json and print it through a layout template. I could get the aprx through the code snippet below, but the tool's outcome legend differs from ArcGIS Pro as shown in the images. The problem happens when there is a group layer as well as labels for sub-items. So the legend doesn't include second group layer names and instead makes duplicate labels for sub-items.
result = arcpy.mp.ConvertWebMapToArcGISProject(webmap_json, layout_template, "Layers Map Frame")
aprx = result.ArcGISProject
layout = aprx.listLayouts()[0]
layout.exportToPDF(out_pdf=output)
Output from the tool with duplicate labels | The actual layer in ArcGIS Pro |
 |  |
I tried to get the aprx file and save it. the created aprx file has this wired behavior of duplicating labels/layer names on printing legend too when I want to use a sample layout. I have also attached the settings for the legend part of the layout. I checked different settings scenarios with Group layer name, layer name and Label.
Any help or hint would be appreciated.
ArcGIS pro version: 2.6