{"record":{"id":"f6581fe793bd9d12","repo":"run-llama/llama_index","slug":"please-install-networkx-to-visualize-the-graph-p","errorCode":null,"errorMessage":"Please install networkx to visualize the graph: `pip install networkx`","messagePattern":"Please install networkx to visualize the graph: `pip install networkx`","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/indices/knowledge_graph/base.py","lineNumber":357,"sourceCode":"\n            all_ref_doc_info[ref_node.node_id] = ref_doc_info\n        return all_ref_doc_info\n\n    def get_networkx_graph(self, limit: int = 100) -> Any:\n        \"\"\"\n        Get networkx representation of the graph structure.\n\n        Args:\n            limit (int): Number of starting nodes to be included in the graph.\n\n        NOTE: This function requires networkx to be installed.\n        NOTE: This is a beta feature.\n\n        \"\"\"\n        try:\n            import networkx as nx\n        except ImportError:\n            raise ImportError(\n                \"Please install networkx to visualize the graph: `pip install networkx`\"\n            )\n\n        g = nx.Graph()\n        subjs = list(self.index_struct.table.keys())\n\n        # add edges\n        rel_map = self._graph_store.get_rel_map(subjs=subjs, depth=1, limit=limit)\n\n        added_nodes = set()\n        for keyword in rel_map:\n            for path in rel_map[keyword]:\n                subj = keyword\n                for i in range(0, len(path), 2):\n                    if i + 2 >= len(path):\n                        break\n\n                    if subj not in added_nodes:","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/indices/knowledge_graph/base.py#L339-L375","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ng = kg_index.get_networkx_graph()  # ImportError without networkx\n\n# after\npip install networkx\ng = kg_index.get_networkx_graph()","handlingStrategy":"validation","validationCode":"import importlib.util\n\nif importlib.util.find_spec(\"networkx\") is None:\n    raise SystemExit(\"networkx required for graph visualization: pip install networkx\")\ng = kg_index.get_networkx_graph()","typeGuard":null,"tryCatchPattern":"try:\n    g = kg_index.get_networkx_graph()\nexcept ImportError:\n    g = None  # visualization unavailable; degrade gracefully","preventionTips":["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."],"tags":["llama-index","knowledge-graph","networkx","optional-dependency","visualization"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}