{"record":{"id":"3de756827bae4a1c","repo":"stanfordnlp/CoreNLP","slug":"could-not-find-a-valid-edge-to-remove","errorCode":null,"errorMessage":"Could not find a valid edge to remove","messagePattern":"Could not find a valid edge to remove","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/naturalli/Util.java","lineNumber":278,"sourceCode":"            //                                   \"\\n  Next edge found: \" + candidate);\n          } else {\n            // either or both could be wrong, so we don't try to\n            // figure out which to keep\n            original = candidate;\n          }\n        }\n        if (toKeep == null) {\n          toKeep = candidate;\n        } else if (toKeep.getRelation().toString().startsWith(\"conj\") && candidate.getRelation().toString().matches(\".subj.*|.obj.*\")) {\n          toKeep = candidate;\n        } else if (!candidate.isExtra() &&\n                   !(candidate.getRelation().toString().startsWith(\"conj\") && toKeep.getRelation().toString().matches(\".subj.*|.obj.*\"))) {\n          toKeep = candidate;\n        }\n      }\n      if (!verifyRemoval(tree, incomingEdges, toKeep, toKeep.getDependent())) {\n        if (original == null) {\n          throw new RuntimeException(\"Could not find a valid edge to remove\");\n        }\n        toKeep = original;\n      }\n      List<SemanticGraphEdge> removeEdges = new ArrayList<>();\n      for (SemanticGraphEdge candidate : incomingEdges) {\n        if (candidate != toKeep) {\n          removeEdges.add(candidate);\n        }\n      }\n      removeEdges.forEach(tree::removeEdge);\n      extraEdges.addAll(removeEdges);\n    }\n\n    // Add apposition edges (simple coref)\n    for (SemanticGraphEdge extraEdge : new ArrayList<>(extraEdges)) {  // note[gabor] prevent concurrent modification exception\n      for (SemanticGraphEdge candidateAppos : tree.incomingEdgeIterable(extraEdge.getDependent())) {\n        if (candidateAppos.getRelation().toString().equals(\"appos\")) {\n          extraEdges.add(new SemanticGraphEdge(extraEdge.getGovernor(), candidateAppos.getGovernor(), extraEdge.getRelation(), extraEdge.getWeight(), extraEdge.isExtra()));","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/naturalli/Util.java#L260-L296","documentation":"Util.cleanTree repeatedly deletes nodes from a dependency graph while keeping it a tree; for each removal it picks the best candidate edge to keep (toKeep) and verifies the removal via verifyRemoval. If no verified edge can be removed and there is no fallback (original == null), it throws RuntimeException \"Could not find a valid edge to remove\" — the graph cannot be reduced to a well-formed tree under the given constraints.","triggerScenarios":"Calling Util.cleanTree on a SemanticGraph where every candidate removal fails verifyRemoval (e.g. edges whose relations are conj/subj/obj protected patterns) and no original edge is cached — typically an ill-formed or cyclic input graph that is not actually a tree.","commonSituations":"Feeding parser output containing cycles or re-entrancies into natural-login openie preprocessing; universal-Dependencies graphs with enhanced/collapsed edges; malformed conllu input.","solutions":["Verify the input graph passes isTree()/isDag() checks before calling cleanTree","Inspect the graph for cycles or protected edges blocking all removals and fix the parse","Provide/ensure the original edge fallback path so cleanTree can backtrack instead of throwing"],"exampleFix":"// before\nUtil.cleanTree(graph);\n// after\nif (!SemgraphicUtil.isTree(graph)) { /* fix or skip */ }\nUtil.cleanTree(graph);","handlingStrategy":"validation","validationCode":"if (!SemgraphicUtil.isTree(tree) || SemgraphicUtil.isCyclic(tree)) { /* repair or skip before cleanTree */ }","typeGuard":null,"tryCatchPattern":"try { Util.cleanTree(tree); } catch (RuntimeException e) { /* skip sentence or rebuild graph */ }","preventionTips":["Validate parse output is a tree before natural-logic preprocessing","Avoid feeding enhanced/collapsed UD graphs directly into cleanTree"],"tags":["java","dependency-graph","runtime-exception"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}