ragas icon indicating copy to clipboard operation
ragas copied to clipboard

New metric: structural filter-correctness for metadata-filtered retrieval

Open ghubuser-dnt opened this issue 2 months ago • 9 comments

Describe the Feature Ragas has no metric that checks whether a metadata where-clause filter on retrieval (jurisdiction, tenant, category, access-tier, etc.) actually does what it's supposed to — i.e. that it structurally cannot return chunks outside the allowed set. I'd like to propose a FilterCorrectness-style metric (working name) in ragas.metrics.collections that takes the metadata of the retrieved chunks plus the filter that was supposed to constrain them, and returns whether any chunk violates it — deterministic, non-LLM, a per-query structural assertion rather than an output-quality score.

Rough shape (open to bikeshedding on the exact signature):

async def ascore(
    self,
    retrieved_metadatas: list[dict],  # one dict per retrieved chunk
    filter_field: str,                # e.g. "jurisdiction", "tenant_id"
    allowed_values: str | list[str],  # what the where-clause should have enforced
) -> MetricResult:
    ...

Why is the feature important for you? Any RAG system with metadata-filtered/scoped retrieval needs to verify the filter is actually enforced, and today there's no metric-level way to do that — you'd have to hand-write a one-off assertion per project.

Concrete cases:

  • Multi-tenant SaaS RAG: verifying tenant A's query never returns tenant B's chunks, even under semantically similar queries that would otherwise rank well.
  • Jurisdiction-isolated legal/compliance RAG: e.g. a system scoping retrieval to domestic vs international regulatory sources per query — cross-jurisdiction leakage is a correctness bug, not a quality nuance.
  • Access-tier / RBAC-scoped retrieval: confirming a "public-only" filter never surfaces internal-only chunks.
  • Regression testing: teams already using Faithfulness / ContextPrecision in an eval suite could add this alongside them to catch filter regressions (e.g. a refactor that silently drops the where-clause) the same way those metrics catch factual regressions.

Ragas has strong output-quality and context-relevance coverage, but nothing checks the retrieval filter mechanism itself for structural leakage — this is a gap distinct from anything currently in collections/.

Additional context Distinct from #2283 (Risk-Control suite, tracking #2279): that suite scores the abstain/answer decision quality — whether the system correctly refuses to answer under low confidence. This proposal is about retrieval-filter structural correctness — whether the where- clause itself can leak results across categories — a different property checked before generation even happens, and it doesn't touch the same code paths or scope.

Open to discussing:

  • Whether this should be a single boolean/pass-fail-style MetricResult, or return a leak count/ratio (e.g. "2/5 retrieved chunks violated the filter") — leaning toward the latter since a reason string naming the leaking values is more actionable.
  • Whether filter_field/allowed_values is the right generic shape, or whether it should accept an arbitrary predicate callable instead of a field+allowed-values pair, for filters more complex than single-field equality/membership.

Happy to build against whatever interface makes sense — this issue is meant to open the discussion, not present a finished design.

ghubuser-dnt avatar Sep 30 '26 20:09 ghubuser-dnt

Hi, @ghubuser-dnt I'd like to work on this. To answer the two open questions: I'd lean toward a leak-ratio result (e.g. 2/5 chunks violated the filter) with a reason string naming the offending values, since that's strictly more informative than a boolean and still trivially truthy/falsy for pass/fail checks. For v1 I'd keep the filter_field + allowed_values shape (matches how ToolCallF1 etc. keep inputs simple) and leave an arbitrary-predicate variant for a follow-up, rather than blocking this on a more general design. Happy to adjust based on your take — will open a draft PR once you give a direction.

Faizan21-Analyst avatar Oct 01 '26 11:10 Faizan21-Analyst

Hi @Faizan21-Analyst — thanks, that's useful, and matches roughly where I was leaning (leak-ratio + reason string over a bare boolean, filter_field/allowed_values for v1 with the predicate-callable version left for a follow-up rather than blocking on it).

I'd like to pick this one up myself though — it's part of something I'm working through end-to-end. Planning to open a draft PR once there's maintainer alignment on the shape above. Appreciate you digging into the open questions either way!

ghubuser-dnt avatar Oct 01 '26 18:10 ghubuser-dnt

No problem at all, glad the proposal was useful — looking forward to seeing it land. I'll keep an eye out for other issues to pick up.

Faizan21-Analyst avatar Oct 01 '26 20:10 Faizan21-Analyst

Thanks for the offer, @Faizan21-Analyst, much appreciated. I'll tag you for a look once I have a PR up, if you'd like to review it. Waiting on a maintainer's view on scope before I start.

ghubuser-dnt avatar Oct 03 '26 14:10 ghubuser-dnt

Friendly bump on this. To keep review easy, I’d scope the first PR to a single metric under the new collections architecture, supporting equality and $in filters only, with tests and a short docs page. Happy to adjust the scope or naming if you’d prefer something different. If I don’t hear otherwise, I’ll open a small draft PR in about a week so there’s something concrete to react to.

ghubuser-dnt avatar Oct 09 '26 15:10 ghubuser-dnt

One distinction worth keeping explicit: this checks violations in the returned chunks, but cannot establish that the filter could never return an out-of-scope chunk. I'd report the checked count alongside the violation count and define empty results and missing metadata separately, so retrieving nothing or omitting the field doesn't look like evidence that filtering works.

GioiaZheng avatar Oct 19 '26 09:10 GioiaZheng

@GioiaZheng Thanks, that’s a fair distinction. A checked count alongside violations, with empty results and missing metadata handled separately, is the right way to frame it, and the docs should state that the metric only inspects returned chunks. I’m pausing on this repo for now since it looks inactive, so I won’t be opening a PR here.

ghubuser-dnt avatar Oct 19 '26 11:10 ghubuser-dnt

Hi — I see the original author paused and won't open a PR. I'd like to pick this up.

Proposed v1 scope (matching the thread):

  • one metric under the collections architecture
  • equality + $in filters only
  • result: leak-ratio + reason (offending values), plus checked/violation counts
  • empty retrieval and missing metadata handled as distinct cases (not as "perfect filter")
  • unit tests + short docs page stating this only inspects returned chunks

I'll open a draft PR once the implementation and tests are ready. Happy to adjust naming/scope.

L00PLeGeNd avatar Oct 27 '26 10:10 L00PLeGeNd

Draft PR up: https://github.com/vibrantlabsai/ragas/pull/3071 — matches the v1 scope discussed here (leak-ratio + reason, equality/$in, empty/missing-metadata as distinct non-perfect cases). @Faizan21-Analyst @ghubuser-dnt welcome to take a look if you'd like.

L00PLeGeNd avatar Oct 27 '26 11:10 L00PLeGeNd