Graphify-Labs/graphify · error · ValueError

communities dict is empty — refusing to clear wiki/. Run `gr

Error message

communities dict is empty — refusing to clear wiki/. Run `graphify extract .` or `graphify cluster-only .` first.

What it means

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.

Source

Thrown at graphify/wiki.py:243

    output_dir: str | Path,
    community_labels: dict[int, str] | None = None,
    cohesion: dict[int, float] | None = None,
    god_nodes_data: list[dict] | None = None,
) -> int:
    """Generate a Wikipedia-style wiki from the graph.

    Writes:
      - index.md            — agent entry point, catalog of all articles
      - <CommunityName>.md  — one article per community
      - <GodNodeLabel>.md   — one article per god node

    Returns the number of articles written (excluding index.md).
    """
    out = Path(output_dir)
    out.mkdir(parents=True, exist_ok=True)

    if not communities:
        raise ValueError(
            "communities dict is empty — refusing to clear wiki/. "
            "Run `graphify extract .` or `graphify cluster-only .` first."
        )

    # Filter stale node IDs that exist in communities but not in G.
    # Analysis JSON can drift from the graph after dedup / re-extract / update.
    # NetworkX 3.x returns DegreeView({}) for missing nodes instead of raising,
    # which crashes sorted() with TypeError; G.neighbors()/G.nodes[] also raise.
    import sys as _sys
    _g_nodes = set(G.nodes)
    _orig_total = sum(len(ns) for ns in communities.values())
    communities = {cid: [n for n in nodes if n in _g_nodes] for cid, nodes in communities.items()}
    communities = {cid: nodes for cid, nodes in communities.items() if nodes}
    _kept_total = sum(len(ns) for ns in communities.values())
    if _kept_total < _orig_total:
        print(
            f"wiki: dropped {_orig_total - _kept_total} stale node ID(s) not in graph "
            f"({len(communities)} communities remaining)",

View on GitHub (pinned to 7fe58b0b0f)

Solutions

  1. Run `graphify extract .` (full LLM extraction) or `graphify cluster-only .` (AST-only) to regenerate .graphify_analysis.json with real communities
  2. 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
  3. 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

Example fix

# before
analysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text())
to_wiki(G, analysis['communities'], 'graphify-out/wiki')  # ValueError: communities dict is empty

# after
analysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text())
communities = analysis.get('communities') or {}
if not communities:
    raise SystemExit('run `graphify extract .` first — no communities to publish')
to_wiki(G, communities, 'graphify-out/wiki')
Defensive patterns

Strategy: validation

Validate before calling

import json
from pathlib import Path

analysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text(encoding='utf-8'))
communities = analysis.get('communities') or {}
if not communities:
    raise SystemExit('no communities — run `graphify extract .` or `graphify cluster-only .` first')

to_wiki(G, communities, 'graphify-out/wiki')

Type guard

def has_communities(analysis: dict) -> bool:
    """True when analysis carries at least one non-empty community."""
    return bool(analysis.get('communities')) and any(
        analysis['communities'].values()
    )

Try / catch

try:
    to_wiki(G, communities, 'graphify-out/wiki')
except ValueError as e:
    if 'communities dict is empty' in str(e):
        raise SystemExit('build communities first: `graphify extract .`') from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Graphify-Labs/graphify@7fe58b0b0f (2026-08-14). Data as JSON: /api/errors/4f7ae32ecf1877be. Report an issue: GitHub.