run-llama/llama_index · error · ImportError
Please install networkx to visualize the graph: `pip install
Error message
Please install networkx to visualize the graph: `pip install networkx`
What it means
KnowledgeGraphIndex.get_networkx_graph() lazily imports networkx and, if missing, raises ImportError suggesting 'pip install networkx'. The method builds an nx.Graph from the graph store's rel_map (subjects, depth 1, up to `limit`) for visualization; networkx is an optional dependency only needed on this path.
Source
Thrown at llama-index-core/llama_index/core/indices/knowledge_graph/base.py:357
all_ref_doc_info[ref_node.node_id] = ref_doc_info
return all_ref_doc_info
def get_networkx_graph(self, limit: int = 100) -> Any:
"""
Get networkx representation of the graph structure.
Args:
limit (int): Number of starting nodes to be included in the graph.
NOTE: This function requires networkx to be installed.
NOTE: This is a beta feature.
"""
try:
import networkx as nx
except ImportError:
raise ImportError(
"Please install networkx to visualize the graph: `pip install networkx`"
)
g = nx.Graph()
subjs = list(self.index_struct.table.keys())
# add edges
rel_map = self._graph_store.get_rel_map(subjs=subjs, depth=1, limit=limit)
added_nodes = set()
for keyword in rel_map:
for path in rel_map[keyword]:
subj = keyword
for i in range(0, len(path), 2):
if i + 2 >= len(path):
break
if subj not in added_nodes:View on GitHub (pinned to afd0fef371)
Solutions
- pip install networkx in the environment running the visualization.
- Add networkx to an optional dev/visualization extras group so production images stay slim.
- Guard the call: import networkx yourself first (or try/except ImportError) and show a graceful message instead of crashing the app.
Example fix
# before g = kg_index.get_networkx_graph() # ImportError without networkx # after pip install networkx g = kg_index.get_networkx_graph()
Defensive patterns
Strategy: validation
Validate before calling
import importlib.util
if importlib.util.find_spec("networkx") is None:
raise SystemExit("networkx required for graph visualization: pip install networkx")
g = kg_index.get_networkx_graph() Try / catch
try:
g = kg_index.get_networkx_graph()
except ImportError:
g = None # visualization unavailable; degrade gracefully Prevention
- Keep networkx in a 'viz' extras group and install it where graphs are inspected.
- Guard debug/visualization endpoints with an import check.
- Never let an optional visualization crash the main query path.
When it happens
Trigger: Calling kg_index.get_networkx_graph() or get_networkx_graph(limit=N) without networkx installed; visualization/inspection tooling over a KnowledgeGraphIndex on a base install of llama-index-core.
Common situations: Notebook exploration of a KG index on a slim environment; adding a 'view graph' debug endpoint to a service deployed from a minimal Docker image; local dev working (networkx present globally) but CI/prod failing.
Related errors
- WandbCallbackHandler is not installed. Please install it usi
- OpenInferenceCallbackHandler is not installed. Please instal
- ArizePhoenixCallbackHandler is not installed. Please install
- HoneyHiveCallbackHandler is not installed. Please install it
- PromptLayerHandler is not installed. Please install it using
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/f6581fe793bd9d12.
Report an issue: GitHub.