A graph community is a densely connected cluster of nodes, usually found with a community-detection algorithm such as Leiden. In graph-based retrieval, it groups related entities and relationships so systems can summarize themes that no single text chunk expresses. section 12.1
Communities address global questions that vector lookup handles poorly, such as identifying recurring themes across hundreds of vendor-dispute emails. GraphRAG detects communities and precomputes hierarchical summaries, trading higher indexing cost for fast retrieval. section 12.1
A practical community-detection workflow includes:
- Build a directed NetworkX graph from extracted entities and relationships, retaining node attributes and edge weights. section 12.2.3
- Copy the graph into igraph and apply Leiden modularity optimization until convergence. section 12.2.3
- Map clustered igraph vertices back to NetworkX identifiers and store each community as a node collection. section 12.2.3
Recurring themes across disputes
A query asking, “What are the recurring themes in this year's vendor disputes?” can use community summaries to synthesize patterns across many connected documents instead of retrieving only the loudest individual examples. section 12.1
Listing 12.2 Community detection.
import igraph as ig
import leidenalg
def detect_communities(self) -> Dict[int, List[str]]:
"""Apply Leiden algorithm to detect communities."""
g_ig = ig.Graph.from_networkx(self.graph) #A
partition = leidenalg.find_partition( #B
g_ig,
leidenalg.ModularityVertexPartition,
weights='weight',
n_iterations=-1
)
self.communities = {} #C
for idx, cluster_nodes in enumerate(partition):
node_labels = [
g_ig.vs[i]['_nx_name'] for i in cluster_nodes #D
]
self.communities[idx] = node_labels
return self.communities
The listing shows Leiden community detection with igraph and leidenalg, including weighted modularity partitioning and conversion back to NetworkX node identifiers. section 12.2.3




