Merge multiple extraction results into one graph. directed=True produces a DiGraph that preserves edge direction (source→target). directed=False (default) produces an undirected Graph for backward compatibility. dedup=True (default) runs entity deduplication before building the graph.
(
extractions: list[dict],
*,
directed: bool = False,
dedup: bool = True,
dedup_llm_backend: str | None = None,
root: str | Path | None = None,
protected_ids: "set[str] | None" = None,
)
| 2607 | |
| 2608 | |
| 2609 | def build( |
| 2610 | extractions: list[dict], |
| 2611 | *, |
| 2612 | directed: bool = False, |
| 2613 | dedup: bool = True, |
| 2614 | dedup_llm_backend: str | None = None, |
| 2615 | root: str | Path | None = None, |
| 2616 | protected_ids: "set[str] | None" = None, |
| 2617 | ) -> nx.Graph: |
| 2618 | """Merge multiple extraction results into one graph. |
| 2619 | |
| 2620 | directed=True produces a DiGraph that preserves edge direction (source→target). |
| 2621 | directed=False (default) produces an undirected Graph for backward compatibility. |
| 2622 | dedup=True (default) runs entity deduplication before building the graph. |
| 2623 | dedup_llm_backend: if set (e.g. "gemini", "claude", or "kimi"), uses LLM to resolve |
| 2624 | ambiguous pairs in the 75–92 Jaro-Winkler score zone. |
| 2625 | root: if given, absolute source_file paths are made relative to root (#932). |
| 2626 | protected_ids: optional set of node IDs to protect from being collapsed with |
| 2627 | other protected nodes during incremental merge (#3477). |
| 2628 | |
| 2629 | With dedup disabled, extractions are merged in order and the last node's |
| 2630 | attributes win (NetworkX add_node overwrites). With dedup enabled, nodes |
| 2631 | sharing an ID use a deterministic survivor and retain missing attributes |
| 2632 | from duplicate records of the same source entity. Genuine cross-file ID |
| 2633 | collisions remain isolated and are reported. |
| 2634 | """ |
| 2635 | from graphify.dedup import deduplicate_entities |
| 2636 | combined: dict = {"nodes": [], "edges": [], "hyperedges": [], "input_tokens": 0, "output_tokens": 0} |
| 2637 | for ext in extractions: |
| 2638 | combined["nodes"].extend(ext.get("nodes", [])) |
| 2639 | combined["edges"].extend(ext.get("edges", [])) |
| 2640 | combined["hyperedges"].extend(ext.get("hyperedges", [])) |
| 2641 | combined["input_tokens"] += ext.get("input_tokens", 0) |
| 2642 | combined["output_tokens"] += ext.get("output_tokens", 0) |
| 2643 | _root = str(Path(root).resolve()) if root else None |
| 2644 | _dedup_collapsed = 0 |
| 2645 | _dedup_remap: dict[str, str] = {} |
| 2646 | if dedup and combined["nodes"]: |
| 2647 | # Numeric ids must be str before dedup, which keys on them and would |
| 2648 | # raise TypeError in _pick_winner's regex search (#2326). build_from_json |
| 2649 | # coerces too, but that runs after dedup — too late for this path. |
| 2650 | _coerce_non_string_ids(combined) |
| 2651 | _repair_extraction_field_types(combined) |
| 2652 | # Fold legacy node field aliases before dedup (#2194): dedup runs BEFORE |
| 2653 | # build_from_json and keys on `label`, so a `name`/`file`/`path` alias node |
| 2654 | # would be invisible to it and only label-dedup one build later, after |
| 2655 | # build_from_json's own fold has healed the persisted graph.json. |
| 2656 | for n in combined["nodes"]: |
| 2657 | if isinstance(n, dict): |
| 2658 | _fold_node_aliases(n) |
| 2659 | # Normalize source_file and definition_file to the build root before |
| 2660 | # deduplication (#3472), so exact-ID collision checks and same-file |
| 2661 | # attribute merging operate on canonical repo-relative paths rather |
| 2662 | # than false-flagging absolute paths from semantic subagents as |
| 2663 | # different files. |
| 2664 | if "source_file" in n: |
| 2665 | n["source_file"] = _norm_source_file(n["source_file"], _root) |
| 2666 | if "definition_file" in n: |