fix(agentic_rag): honor similarity_threshold in deduplicate_chunks - #832
Open
AbirAbbas wants to merge 1 commit into
Open
fix(agentic_rag): honor similarity_threshold in deduplicate_chunks#832AbirAbbas wants to merge 1 commit into
AbirAbbas wants to merge 1 commit into
Conversation
deduplicate_chunks accepted a similarity_threshold parameter but never used it -- deduplication was a plain exact-match lookup against a set of normalized chunk texts, so near-duplicate chunks (the ones overlapping chunk windows and multi-pass retrieval actually produce) always survived regardless of the threshold passed in. Compare each candidate against the chunks already kept using a word-overlap (Jaccard) text_similarity helper and drop it when the score meets the threshold. Identical text scores 1.0, so the default threshold of 0.9 still removes exact duplicates as before. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
deduplicate_chunksin the agentic_rag example accepted asimilarity_thresholdparameter but never read it. Deduplication was a plain exact-match lookup against asetof normalized chunk texts, so only byte-identical (after lowercase + whitespace normalization) chunks were ever dropped. Near-duplicates — exactly what overlapping chunk windows and the multi-pass retrieval loop inmain.pyproduce — always survived, no matter what threshold the caller passed.The fix adds a small
text_similarityhelper (word-overlap / Jaccard, in the style of the neighbouringkeyword_match_score) and compares each candidate chunk against the chunks already kept, dropping it when the score meets the threshold. Identical text scores1.0, so the default0.9still removes exact duplicates exactly as before — the change is a strict superset of the old behavior.Independently implements the fix reported in #829 by @andrewwhitecdw (whose patch we couldn't accept because the CLA was not signed). Closes the underlying issue.
Validation contract
similarity_thresholdare treated as duplicates; the later one is removed.0.9, exact/normalized-identical chunks are still removed and distinct chunks are still kept.chunk_id,score,text) survive —main.pyre-hydrates the result withRankedChunk(**c), so dropping keys would break the caller.Test plan
examples/**has no pytest infrastructure and no CI gate:sdk-python.ymlis path-filtered tosdk/python/**,make lintonly lintssdk/python, andcoverage.ymlexplicitly documents that examples-only PRs skip. (The onetest_*.pyunderexamples/python_agent_nodes/is a runnable demo script, not a pytest module.) Rather than add a test file nothing would execute, the contract above was verified by driving the function directly with a throwaway script —embedding_manageris stubbed sincededuplicate_chunksneeds no embeddings:Note the first three cases: the same pair of chunks is kept or dropped purely as a function of the threshold — which is the behavior that was impossible before this change.
Lint, using the ruff version CI pins in
sdk-python.yml:uvx ruff@0.15.22 check examples/python_agent_nodes/agentic_rag/skills.py— All checks passed!uvx ruff@0.15.22 format --check examples/python_agent_nodes/agentic_rag/skills.py— 1 file already formattedpython3 -m py_compileon the changed file — OK🤖 Generated with Claude Code