{"record":{"id":"4f7ae32ecf1877be","repo":"Graphify-Labs/graphify","slug":"communities-dict-is-empty-refusing-to-clear-wiki","errorCode":null,"errorMessage":"communities dict is empty — refusing to clear wiki/. Run `graphify extract .` or `graphify cluster-only .` first.","messagePattern":"communities dict is empty — refusing to clear wiki/\\. Run `graphify extract \\.` or `graphify cluster-only \\.` first\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphify/wiki.py","lineNumber":243,"sourceCode":"    output_dir: str | Path,\n    community_labels: dict[int, str] | None = None,\n    cohesion: dict[int, float] | None = None,\n    god_nodes_data: list[dict] | None = None,\n) -> int:\n    \"\"\"Generate a Wikipedia-style wiki from the graph.\n\n    Writes:\n      - index.md            — agent entry point, catalog of all articles\n      - <CommunityName>.md  — one article per community\n      - <GodNodeLabel>.md   — one article per god node\n\n    Returns the number of articles written (excluding index.md).\n    \"\"\"\n    out = Path(output_dir)\n    out.mkdir(parents=True, exist_ok=True)\n\n    if not communities:\n        raise ValueError(\n            \"communities dict is empty — refusing to clear wiki/. \"\n            \"Run `graphify extract .` or `graphify cluster-only .` first.\"\n        )\n\n    # Filter stale node IDs that exist in communities but not in G.\n    # Analysis JSON can drift from the graph after dedup / re-extract / update.\n    # NetworkX 3.x returns DegreeView({}) for missing nodes instead of raising,\n    # which crashes sorted() with TypeError; G.neighbors()/G.nodes[] also raise.\n    import sys as _sys\n    _g_nodes = set(G.nodes)\n    _orig_total = sum(len(ns) for ns in communities.values())\n    communities = {cid: [n for n in nodes if n in _g_nodes] for cid, nodes in communities.items()}\n    communities = {cid: nodes for cid, nodes in communities.items() if nodes}\n    _kept_total = sum(len(ns) for ns in communities.values())\n    if _kept_total < _orig_total:\n        print(\n            f\"wiki: dropped {_orig_total - _kept_total} stale node ID(s) not in graph \"\n            f\"({len(communities)} communities remaining)\",","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/Graphify-Labs/graphify/blob/7fe58b0b0f3873be9a21c30106b8b8527c353aa6/graphify/wiki.py#L225-L261","documentation":"graphify.wiki.to_wiki() refuses to run when the `communities` argument is an empty dict. Because to_wiki() owns the wiki/ output directory entirely (it deletes all stale *.md files at the start of each call), running it with no communities would wipe wiki/ and write only an empty index. The ValueError is a deliberate guard against that destructive no-op and names the commands that produce a communities dict.","triggerScenarios":"Calling to_wiki(G, communities={}, output_dir=...) — typically by loading .graphify_analysis.json (or the 'communities' key of a graph.json) that is missing, empty, or from a failed prior run, or by passing the result of cluster() on an empty graph.","commonSituations":"Running the wiki step before `graphify extract .` / `graphify cluster-only .` has ever succeeded; a previous extraction crashed leaving a truncated or empty analysis JSON; the communities JSON key was renamed or the file was hand-edited.","solutions":["Run `graphify extract .` (full LLM extraction) or `graphify cluster-only .` (AST-only) to regenerate .graphify_analysis.json with real communities","Check the file you load communities from: verify it parses and its 'communities' key is non-empty (json.load then bool-check) before calling to_wiki","If the analysis JSON exists but is empty, delete graphify-out/.graphify_analysis.json and re-run extraction so a stale empty artifact cannot be reused"],"exampleFix":"# before\nanalysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text())\nto_wiki(G, analysis['communities'], 'graphify-out/wiki')  # ValueError: communities dict is empty\n\n# after\nanalysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text())\ncommunities = analysis.get('communities') or {}\nif not communities:\n    raise SystemExit('run `graphify extract .` first — no communities to publish')\nto_wiki(G, communities, 'graphify-out/wiki')","handlingStrategy":"validation","validationCode":"import json\nfrom pathlib import Path\n\nanalysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text(encoding='utf-8'))\ncommunities = analysis.get('communities') or {}\nif not communities:\n    raise SystemExit('no communities — run `graphify extract .` or `graphify cluster-only .` first')\n\nto_wiki(G, communities, 'graphify-out/wiki')","typeGuard":"def has_communities(analysis: dict) -> bool:\n    \"\"\"True when analysis carries at least one non-empty community.\"\"\"\n    return bool(analysis.get('communities')) and any(\n        analysis['communities'].values()\n    )","tryCatchPattern":"try:\n    to_wiki(G, communities, 'graphify-out/wiki')\nexcept ValueError as e:\n    if 'communities dict is empty' in str(e):\n        raise SystemExit('build communities first: `graphify extract .`') from e\n    raise","preventionTips":["Treat .graphify_analysis.json as build output: verify it is fresh (same run as graph.json) before the wiki step","Fail the pipeline at extraction time if it writes an empty communities payload, so the wiki step never sees one","Never hand-create or hand-edit the analysis sidecar"],"tags":["python","validation","wiki","graphify","data-integrity"],"backgroundTag":null,"analyzedSha":"7fe58b0b0f3873be9a21c30106b8b8527c353aa6","analyzedAt":"2026-08-14T19:23:21.323Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}