Repository navigation
fix(graphrag): filter negative-judgment and misattributed relationship edges - #16541
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded relationship-description validation in GraphRAG subgraph generation. Invalid edges are now skipped when descriptions indicate no relationship or misattribute subjects, and the callback report includes a count of ignored invalid relationships. ChangesRelationship Validation
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant generate_subgraph
participant _relationship_looks_valid
participant subgraph
participant callback
generate_subgraph->>_relationship_looks_valid: validate relationship description
_relationship_looks_valid-->>generate_subgraph: valid / invalid
generate_subgraph->>subgraph: add edge if valid
generate_subgraph->>callback: report ignored_invalid_rels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
rag/graphrag/general/index.py (2)
683-717: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winNo test coverage for the new validation helper.
_relationship_looks_validencodes non-trivial heuristics (negative-judgment regex + subject-mismatch detection) but no unit tests are included to pin down expected behavior across the scenarios described above (acronyms, multi-segment descriptions, missing subjects, etc.).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rag/graphrag/general/index.py` around lines 683 - 717, Add unit tests for _relationship_looks_valid in rag/graphrag/general/index.py to cover the new heuristics and lock in expected behavior. Include cases for a negative-judgment description returning False, empty/missing descriptions returning True, descriptions with no subject matches returning True, acronym and partial-name endpoint matches returning True, multi-segment descriptions with mixed subject matches, and subject-mismatch cases that should be dropped. Use the _relationship_looks_valid helper directly and assert behavior around _NEGATIVE_JUDGMENT_PATTERN, _SUBJECT_PATTERN, and _GRAPH_FIELD_SEP.
655-657: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winReuse the shared graph separator constant here.
rag/graphrag/utils.pyalready definesGRAPH_FIELD_SEP, so duplicating"<SEP>"inrag/graphrag/general/index.py:657can drift from the code that merges descriptions.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rag/graphrag/general/index.py` around lines 655 - 657, The graph field separator is duplicated in the index module, which can drift from the shared value used elsewhere. Update the separator definition in the general indexing code to reuse the existing GRAPH_FIELD_SEP from rag/graphrag/utils.py instead of hardcoding "<SEP>", and make sure any helpers in the index flow that rely on _GRAPH_FIELD_SEP continue to reference the shared constant.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@rag/graphrag/general/index.py`:
- Around line 683-719: The new relationship validation flow in
_relationship_looks_valid needs explicit logging, not just the aggregate
callback count. Add logging in the validation path that records when a
relationship is dropped and why, distinguishing the _NEGATIVE_JUDGMENT_PATTERN
case from subject-to-endpoint mismatches, and include identifying details from
the rel dict such as src_id, tgt_id, and a short description snippet. Wire this
into the caller that applies _relationship_looks_valid so production debugging
can trace individual decisions without changing the current filtering behavior.
- Around line 676-717: The subject validation in _relationship_looks_valid is
too brittle and can wrongly drop valid edges when entity names are paraphrased,
abbreviated, or expanded. Update the matching logic around _SUBJECT_PATTERN and
matches_endpoint() to compare canonicalized/tokenized entity names from src_id
and tgt_id rather than raw substring checks, and keep the conservative behavior
so only clear mismatches are filtered out.
---
Nitpick comments:
In `@rag/graphrag/general/index.py`:
- Around line 683-717: Add unit tests for _relationship_looks_valid in
rag/graphrag/general/index.py to cover the new heuristics and lock in expected
behavior. Include cases for a negative-judgment description returning False,
empty/missing descriptions returning True, descriptions with no subject matches
returning True, acronym and partial-name endpoint matches returning True,
multi-segment descriptions with mixed subject matches, and subject-mismatch
cases that should be dropped. Use the _relationship_looks_valid helper directly
and assert behavior around _NEGATIVE_JUDGMENT_PATTERN, _SUBJECT_PATTERN, and
_GRAPH_FIELD_SEP.
- Around line 655-657: The graph field separator is duplicated in the index
module, which can drift from the shared value used elsewhere. Update the
separator definition in the general indexing code to reuse the existing
GRAPH_FIELD_SEP from rag/graphrag/utils.py instead of hardcoding "<SEP>", and
make sure any helpers in the index flow that rely on _GRAPH_FIELD_SEP continue
to reference the shared constant.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9156e53c-14d2-4ff4-8a6c-6e442c86261f
📒 Files selected for processing (1)
rag/graphrag/general/index.py
…p edges
generate_subgraph() only validated that src_id/tgt_id exist as nodes
before adding a relationship edge, never validating the description
content. This let two classes of bad edges into the graph:
1. Negative judgments from the LLM ("no clear relationship between
X and Y") getting persisted as real weighted edges.
2. Descriptions misattributed to the wrong entity pair during
batch/multi-entity chunk processing.
Adds a conservative _relationship_looks_valid() check that drops an
edge only when there's positive evidence of a problem, to minimize
false-positive risk. Affects both light and general GraphRAG methods,
since light's GraphExtractor reuses Extractor from this module.
8974d19 to
bb0eace
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16541 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #16540
generate_subgraph() only validated that src_id/tgt_id exist as nodes before adding a relationship edge, never validating the description content. This let two classes of bad edges into the graph:
Adds a conservative _relationship_looks_valid() check that drops an edge only when there's positive evidence of a problem, to minimize false-positive risk. Affects both light and general GraphRAG methods, since light's GraphExtractor reuses Extractor from this module.
Summary
Briefly describe what this PR aims to solve. Include background context that will help reviewers understand the purpose of the PR.