New metric: structural filter-correctness for metadata-filtered retrieval
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
domesticvsinternationalregulatory 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/ContextPrecisionin an eval suite could add this alongside them to catch filter regressions (e.g. a refactor that silently drops thewhere-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_valuesis 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.
Source: vibrantlabsai/ragas