P&ID to Knowledge Graph

by Scott Duncan (ScottDuncanAI) — MIT licence

A Claude skill (pid-to-kg) that reads piping and instrumentation diagrams (P&IDs) and process flow diagrams from PDF and builds a process knowledge graph an LLM can reason over: what flows where, what measures what, and how control loops are wired.

What it shows about AI in manufacturing

P&IDs are drawings, not data. Answering “what feeds this clarifier?” or “what does FIC-0502 manipulate?” normally means a person tracing lines across many sheets. This project turns the drawing set into a queryable graph, so the same questions become a query:

  • Speed — the manual tracing of equipment, pipes, valves and instruments across sheets is done by the model and scripts; each sheet is saved before the next is read, so a large set can be processed unattended. The README gives no timing or accuracy figures.
  • Insight — the graph connects process flow (upstream and downstream), measurement (which instrument measures which variable) and control (sensor to controller to final element, including cascades, interlocks and VFD drives), so questions that span sheets become one query.
  • Context for other AI — the output is described as a context layer for chemical-manufacturing LLM applications, grounded so every item records its source sheet.

How it works

  1. Page preparation (scripts) — each page is classified as digital, outlined text or scanned; pages are rotated upright, rendered at a resolution chosen from text size, cut into overlapping tiles, and blank tiles are skipped. OCR cross-checks tags.
  2. Sheet reading (Claude) — Claude reads each sheet and its tiles and records equipment, pipe runs, valves, instruments, signal lines and off-page connectors, each with a location and confidence level.
  3. Graph building and QA (scripts) — pipes are split into segments, off-page connectors link the sheets, instruments attach to what they measure, and loops are assembled from ISA-5.1 tags and signal lines. A QA report flags problems and Claude rechecks them until it is clean.
  4. Querying — the outputs support questions directly.

The design split in the README: “Claude reads the drawings; Python scripts do everything else.” Visual judgement goes to the model; preparation, graph building and checking go to tested scripts. This is a reusable pattern for any document-to-structure task.

Outputs

  • summary.md — a readable plant overview intended as LLM context
  • qa_report.md — doubtful or unconfirmed items, each pointing to the drawing area to recheck
  • graph.json with a schema, and graph.duckdb for SQL queries including recursive upstream and downstream searches (DuckDB)
  • Optional exports to Neo4j and GraphML on request; a DEXPI class mapping that is informative, not a certified DEXPI export

Running it

Copy skills/pid-to-kg into the personal Claude skills folder, place the PDF in a project folder, open it in Claude Code and ask for the conversion, optionally naming the page range. Needs Python 3.10 or newer; on Python 3.13 and later the OCR cross-checks and automatic page orientation are unavailable. The repository had 69 stars and 3 commits when read.

Limits

  • Extraction quality depends on legibility; low-resolution scans lose small text
  • Loops come from tag numbers and drawn signal lines; logic that lives only in control narratives or cause-and-effect charts is not captured
  • Line direction comes from arrows and equipment logic, and inferred edges are marked flow_direction: inferred
  • The graph is machine-extracted and must be reviewed by a qualified engineer before use in design, operations, management of change, or safety studies such as HAZOP or LOPA
  • The README reports no accuracy results on real drawings; its tests use a synthetic two-sheet plant

Sources