{"record":{"id":"e7d9bc0bf5afbfdc","repo":"plotly/plotly.js","slug":"failed-to-build-trace-type-hierarchy-of-trace-n","errorCode":null,"errorMessage":"Failed to build <trace.type> hierarchy of <trace.name>. Error: <e.message>","messagePattern":"Failed to build <trace\\.type> hierarchy of <trace\\.name>\\. Error: <e\\.message>","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/traces/sunburst/calc.js","lineNumber":139,"sourceCode":"            }\n        }\n\n        cd.unshift({\n            hasMultipleRoots: true,\n            id: dummyId,\n            pid: '',\n            label: ''\n        });\n    }\n\n    // TODO might be better to replace stratify() with our own algorithm\n    var root;\n    try {\n        root = d3Hierarchy.stratify()\n            .id(function(d) { return d.id; })\n            .parentId(function(d) { return d.pid; })(cd);\n    } catch(e) {\n        return Lib.warn([\n            'Failed to build', trace.type, 'hierarchy of', trace.name + '.',\n            'Error:', e.message\n        ].join(' '));\n    }\n\n    var hierarchy = d3Hierarchy.hierarchy(root);\n    var failed = false;\n\n    if(hasValues) {\n        switch(trace.branchvalues) {\n            case 'remainder':\n                hierarchy.sum(function(d) { return d.data.v; });\n                break;\n            case 'total':\n                hierarchy.each(function(d) {\n                    var cdi = d.data.data;\n                    var v = cdi.v;\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/plotly/plotly.js/blob/1d090e0b5ffb8d0fdf6e0e4ff51d3f3cf67f1314/src/traces/sunburst/calc.js#L121-L157","documentation":"src/traces/sunburst/calc.js:139 wraps d3-hierarchy's stratify() in try/catch. If stratify throws — typically because a node's pid references a nonexistent id, or ids are duplicated — Plotly warns 'Failed to build <type> hierarchy' with the underlying error message and aborts building that trace, so it renders empty.","triggerScenarios":"ids/parents arrays where a parent id has no matching entry in ids (orphan reference), duplicate id values, or pid values that don't resolve, causing d3.stratify to throw during hierarchy construction.","commonSituations":"Data generated from databases with foreign keys pointing to filtered-out rows; typos or case mismatches between parent and id strings; pagination dropping a parent row while keeping its children.","solutions":["Verify every parent value matches exactly (string equality, case included) some id in the same trace.","Check ids for duplicates and make them unique.","Sanitize server-side before plotting: validate the id set contains all parents and fail fast with a clear message."],"exampleFix":"// before\nids: ['a','a1'], parents: ['','root'] // 'root' missing from ids\n// after\nids: ['a','a1'], parents: ['','a']","handlingStrategy":"validation","validationCode":"const idSet = new Set(ids);\nconst orphans = ids.filter((_, i) => parents[i] !== '' && !idSet.has(parents[i]));\nconst dupes = ids.filter((id, i) => ids.indexOf(id) !== i);\nif (orphans.length) throw new Error('Parents not in ids: ' + [...new Set(orphans)]);\nif (dupes.length) throw new Error('Duplicate ids: ' + [...new Set(dupes)]);","typeGuard":"function hierarchyIsConsistent(ids, parents) {\n  const set = new Set(ids);\n  if (set.size !== ids.length) return false;\n  return parents.every(p => p === '' || set.has(p));\n}","tryCatchPattern":"try {\n  Plotly.newPlot(div, traces);\n} catch (e) {\n  if (/hierarchy/i.test(e.message)) sanitizeHierarchyData(trace); // fix orphan/duplicate ids then retry\n  else throw e;\n}","preventionTips":["Enforce referential integrity between parents and ids at data-preparation time.","Use stable, unique id strings (not labels) for hierarchy nodes.","Never filter hierarchy rows without re-parenting or dropping their children."],"tags":["sunburst","hierarchy","d3-stratify","broken-reference"],"backgroundTag":"dangling-parent-reference","analyzedSha":"1d090e0b5ffb8d0fdf6e0e4ff51d3f3cf67f1314","analyzedAt":"2026-09-02T22:03:39.906Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}