ScottDuncanAI/PID-to-Knowledge-Graph
P&ID to Knowledge Graph: a Claude skill that turns P&ID PDFs (digital or scanned) into a process knowledge graph of flow, instruments and control loops.
What it solves
Piping and instrumentation diagrams (P&IDs) — the standard drawings that show how a chemical plant is plumbed and controlled — usually exist only as PDFs. The information inside them (which tank feeds which pump, what an instrument measures, how a control loop is wired) is locked in pictures and labels that a computer can't query directly. This project turns those PDFs into a structured knowledge graph that an LLM application can reason over, so you can ask questions like "what feeds this clarifier?" or "what does FIC-0502 manipulate?" and get answers straight from the drawings.
How it works
It's a Claude skill: you install it into Claude Code, drop your P&ID PDFs in a folder, and ask Claude to convert them. Claude does the visual reading (recognising symbols and tracing lines needs human-like judgement), while tested Python scripts handle the routine work — preparing pages, building the graph, and checking it.
The pipeline has four stages:
- Preparing the pages. A script inspects every page and classifies it as digital-with-searchable-text, digital-with-outlined-text, scanned, or scanned-with-OCR — each gets different handling. Pages are rotated upright, rendered at a resolution matched to the drawing's text size, and cut into overlapping tiles (blank margins are skipped).
- Claude reads each sheet. Claude reads the whole sheet, then each tile up close, recording equipment, pipes, valves, instruments, signal lines, and off-page connectors — each with its location on the sheet and a confidence level. Anything unreadable is marked as such, never guessed. Each sheet is saved separately so large drawing sets don't overload Claude.
- Building and checking the graph. Scripts assemble Claude's readings into one connected graph: pipes are split into segments between valves, off-page connectors link sheets so flow can be traced across the whole plant, instruments are attached to the exact segment or vessel they measure, and control loops are assembled from ISA-5.1 tag numbers and signal lines. A QA report lists anything doubtful (a sensor with no location, an orphaned connector), and Claude re-checks each item on the drawing until the report is clean.
- Asking questions. The results land in
work/<plant name>/kg/:summary.md(a readable plant overview for an LLM),graph.json(the full graph), andgraph.duckdb(a small database file for SQL queries, including recursive upstream/downstream searches). Every item records its source sheet, so answers can be traced back to the drawing.
Who it's for
Process and chemical engineers who have P&ID or PFD PDFs — digital or scanned — and want to make that information queryable by an LLM application. No programming is needed: you just need Claude Code and Python 3.10+ (3.12 recommended; on 3.13+ the skill works but loses the OCR cross-check and automatic page orientation). The README also points to public P&ID datasets from real permitted facilities if you want to try it without your own drawings.
Highlights
- Handles digital (CAD-exported) and scanned P&IDs, including sideways pages, outlined text, and low-contrast scans (enlarged up to 2.5× with boosted contrast)
- Captures process flow, measurement locations, and control loops — from sensor through controller to final element, including I/P relays, cascades, interlocks, and VFD drives
- Outputs a readable summary, a complete JSON graph, and a DuckDB database for precise SQL queries — no server needed
- QA report flags anything uncertain, and Claude re-checks each flagged item on the drawing until the report is clean
- Can export the graph to Neo4j or GraphML (for yEd, Gephi, or NetworkX) on larger projects
- Equipment and instruments carry the closest DEXPI class, the industry standard for exchanging P&ID data
- MIT licensed; the README warns the graph must be reviewed by a qualified engineer before use in design, operations, or safety studies like HAZOP or LOPA
Related
- Project
- Project
- Project
- Project