Skip to content

fix(graphrag): filter negative-judgment and misattributed relationship edges - #16541

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
afrozenpeach:fix/graphrag-negative-judgment-edges
Jul 6, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
afrozenpeach:fix/graphrag-negative-judgment-edges

Conversation

@afrozenpeach

Copy link
Copy Markdown
Contributor

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:

  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.

Summary

Briefly describe what this PR aims to solve. Include background context that will help reviewers understand the purpose of the PR.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 1, 2026
@coderabbitai

coderabbitai Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c81780f8-45e1-4e8f-9224-7b0a6b8153ae

📥 Commits

Reviewing files that changed from the base of the PR and between 8974d19 and bb0eace.

📒 Files selected for processing (1)
  • rag/graphrag/general/index.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • rag/graphrag/general/index.py

📝 Walkthrough

Walkthrough

Added 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.

Changes

Relationship Validation

Layer / File(s) Summary
Validation helper
rag/graphrag/general/index.py
Adds separator handling, regex patterns, and _relationship_looks_valid(rel: dict) to reject relationship descriptions with negative judgments or subject mismatches.
Edge filtering and reporting
rag/graphrag/general/index.py
Counts invalid relationships in generate_subgraph, skips rejected edges after endpoint checks, and emits a callback message when invalid relationships were ignored.

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
Loading

Poem

I sniff the graph with whiskers keen,
For “no relation” lines unseen.
If names don’t match, I nibble free—
Pop! Bad edges hop away from me. 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: filtering invalid relationship edges by negative judgment or misattribution.
Description check ✅ Passed The PR description includes the required Summary section and explains the bug, impact, and proposed fix.
Linked Issues check ✅ Passed The change matches #16540 by filtering negative-judgment and misattributed edges in generate_subgraph for both light and general modes.
Out of Scope Changes check ✅ Passed The diff appears focused on the requested relationship validation logic without unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
rag/graphrag/general/index.py (2)

683-717: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

No test coverage for the new validation helper.

_relationship_looks_valid encodes 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 win

Reuse the shared graph separator constant here. rag/graphrag/utils.py already defines GRAPH_FIELD_SEP, so duplicating "<SEP>" in rag/graphrag/general/index.py:657 can 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

📥 Commits

Reviewing files that changed from the base of the PR and between 42a0faa and 8974d19.

📒 Files selected for processing (1)
  • rag/graphrag/general/index.py

Comment thread rag/graphrag/general/index.py Resolved
Comment thread rag/graphrag/general/index.py Resolved
…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.
@afrozenpeach
afrozenpeach force-pushed the fix/graphrag-negative-judgment-edges branch from 8974d19 to bb0eace Compare July 1, 2026 18:50
@yingfeng yingfeng added the ci Continue Integration label Jul 2, 2026
@yingfeng
yingfeng marked this pull request as draft July 2, 2026 01:28
@yingfeng
yingfeng marked this pull request as ready for review July 2, 2026 01:28
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (6e82e27) to head (bb0eace).
⚠️ Report is 70 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8
wangq8 merged commit 8fb8e41 into infiniflow:main Jul 6, 2026
10 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: GraphRAG relationship extraction writes negative-judgment and misattributed edges into the graph (light + general modes)

3 participants