Generating knowledge graphs from text: a practical guide with kg-gen and NetworkX
A new tutorial shows how to automatically extract entities, predicates, and relations from text documents, dialogues, and multiple sources. Using kg-gen and Lit

Extracting structured information from text is one of the key tasks in natural language processing. A knowledge graph allows you to represent information as a network of entities and their relationships, opening up possibilities for deep analysis, search, and automated reasoning. A new tutorial shows how to automatically create such graphs from text, dialogues, and multiple sources using the kg-gen tool.
Why Knowledge Graphs Matter
A knowledge graph is not just a pretty visualization. It is a structured representation of information that a machine can analyze and use to answer complex questions. For example, if you process several documents about a company, the graph will show relationships between people, projects, and investments. Analysts will be able to quickly see who works with whom, which projects overlap, and where new connections exist. A classic example is Google's knowledge panel search. When you search for an actor, the system immediately provides not only films, but also people associated with them, awards, and biography. All of this is built on the basis of graphs.
How kg-gen Works
The tutorial begins with environment setup. You will need kg-gen dependencies and LLM configuration through LiteLLM. This is convenient because it allows you to work with different models—OpenAI, Anthropic, local models—without rewriting code. Next comes a step-by-step breakdown of the process:
- Loading text or documents
- Passing text to an LLM with a prompt to extract entities, predicates, and relationships
- Building a graph based on the obtained data
- Exporting to a format understood by NetworkX and visualizers
The key idea is that the LLM does the heavy lifting of understanding text, and then the results are transformed into a graph that can be analyzed programmatically.
From Simple Text to Large Documents
With simple texts, everything works straightforwardly: one phrase → several entities and one or two relationships. But what if the document is hundreds of pages long? Chunking comes to the rescue—dividing text into overlapping chunks. Each chunk is processed separately, and then the graphs are merged into a single whole. An additional technique is clustering. After building a large knowledge graph, you can apply community detection algorithms. This will highlight groups of nodes that are often connected to each other but weakly connected to the rest of the graph. It is easier for a person to understand a graph if it is divided into logical subsystems.
Analysis and Interactive Visualization
When the graph is built, NetworkX comes into play—a Python library for graph analysis. It allows you to compute various metrics: node centrality (who is most important?), shortest paths, graph density, number of cycles. These metrics help understand the structure of information. But dry numbers are not always enough. The tutorial shows how to build interactive visualizations that can be explored in a browser. Users can click on nodes, see neighbors, filter by relationship types, and highlight paths between entities of interest.
What This Means
Automatic knowledge graph generation from text is becoming more accessible and practical. This approach is useful for corporate analytics systems—processing business emails, contracts, and reports. Recommendation systems can use graphs to find hidden connections between users and products. The tutorial demonstrates that modern LLM tools have made such tasks accessible even for small teams.