Implementation:Explodinggradients Ragas QuotedSpans Module
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Metrics, Citation |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The quoted_spans module provides a lightweight citation alignment metric that measures what fraction of quoted spans in model-generated answers can be found verbatim in the provided source passages.
Description
This module extracts quoted text spans from answers using a regular expression that supports both straight and curly quote characters. Each extracted span is checked against concatenated source passages using substring matching after optional whitespace normalization and case folding. The primary function quoted_spans_alignment iterates over parallel lists of answers and sources, producing a score in the range [0, 1] together with raw matched and total counts. The helper _extract_quoted_spans filters spans by a configurable minimum word length to avoid spurious matches.
Usage
Call quoted_spans_alignment with matched lists of answers and their corresponding source passages. The function returns a dictionary containing the alignment score, the number of matched spans, and the total number of spans considered.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/metrics/quoted_spans.py L27-124
|
| Signature (quoted_spans_alignment) | def quoted_spans_alignment(answers: Sequence[str], sources: Sequence[Sequence[str]], *, casefold: bool = True, min_len: int = 3) -> Dict[str, float]
|
| Signature (_extract_quoted_spans) | def _extract_quoted_spans(answer: str, *, min_len: int = 3) -> Sequence[str]
|
| Import | from ragas.metrics.quoted_spans import quoted_spans_alignment
|
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
answers |
Sequence[str] |
List of model-generated answers (length N) |
sources |
Sequence[Sequence[str]] |
List of source passage lists (length N), one per answer |
casefold |
bool |
Whether to normalize text by lower-casing before matching (default True) |
min_len |
int |
Minimum number of words required for a quoted span to be considered (default 3) |
Outputs
| Key | Type | Description |
|---|---|---|
citation_alignment_quoted_spans |
float |
Fraction of quoted spans found in sources (0.0-1.0) |
matched |
float |
Number of spans that matched a source passage |
total |
float |
Total number of quoted spans considered |
Usage Examples
from ragas.metrics.quoted_spans import quoted_spans_alignment
answers = [
'The report states "revenue increased by 15 percent" and "costs remained stable".',
]
sources = [
["The annual report notes that revenue increased by 15 percent over the prior year."]
]
result = quoted_spans_alignment(answers, sources)
print(result["citation_alignment_quoted_spans"]) # 0.5 (1 of 2 spans matched)
print(result["matched"]) # 1.0
print(result["total"]) # 2.0
from ragas.metrics.quoted_spans import _extract_quoted_spans
spans = _extract_quoted_spans('She said "hello world example" and "ok"', min_len=3)
print(spans) # ['hello world example']
Related Pages
- MetricResult_Class - Result wrapper used by metrics
- MetricValidators_Module - Validation framework for metric outputs
- Validation_Module - Dataset-level validation utilities