fix: preserve custom node/edge attributes during merge operations#2990
Open
abc-lee wants to merge 2 commits into
Open
fix: preserve custom node/edge attributes during merge operations#2990abc-lee wants to merge 2 commits into
abc-lee wants to merge 2 commits into
Conversation
_merge_nodes_then_upsert and _merge_edges_then_upsert reconstruct
node_data/edge_data from scratch with only 7 hardcoded fields,
silently discarding any custom attributes added by downstream users.
This is inconsistent with aedit_entity (which uses {**node_data, **updated_data})
and amerge_entities (which collects all keys via _merge_attributes).
Fix: start from existing node/edge dict and update with standard fields,
so custom attributes (e.g. brain_meta_*, community_id) are preserved.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
NetworkX uses Python dict keys for node identity, making node IDs case-sensitive. This causes duplicate nodes when LLM extraction returns different casing (e.g., 'Brain:Region:文档库' vs 'brain:region:文档库'). In a knowledge graph, 'Apple' and 'apple' are semantically the same entity. Add _normalize_node_id() static method that applies .lower() to all node IDs before they enter NetworkX. Applied consistently across all node/edge operations: has_node, get_node, upsert_node, delete_node, has_edge, get_edge, upsert_edge, remove_edges, and BFS entry point. This fixes the issue at the storage layer, so all upstream paths (LLM extraction via operate.py, custom injection via ainsert_custom_kg, and direct API calls) automatically benefit without any changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix data loss bug where custom node/edge attributes are silently discarded during merge operations.
The Bug
_merge_nodes_then_upsertand_merge_edges_then_upsertreconstructnode_data/edge_datafrom scratch with only 7 hardcoded fields. This silently discards any custom attributes (e.g.brain_meta_*,community_id, or any user-extended fields) added by downstream users.This is inconsistent with:
aedit_entitywhich uses{**node_data, **updated_data}patternamerge_entitieswhich explicitly collects all keys via_merge_attributesThe Fix
Instead of building a new dict from scratch:
This approach is minimal, targeted, and maintains backwards compatibility.
Testing
🤖 Generated with Claude Code