Evaluators¶
This module provides pre-built evaluators for common evaluation tasks.
As the task can have arbitrary input and output types these evaluators in general coerce those to strings and use string-based evaluation methods (LLM judges, regex checks, etc).
It is, however, pretty easy to create your own custom evaluators by inheriting from BaseEvaluator and implementing the run() method. See Create custom evaluators for a tutorial on how to do this.
LLMJudge¶
ragpill.evaluators.LLMJudge
dataclass
¶
LLMJudge(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, rubric, model=_get_default_judge_llm(), include_input=False)
Bases: BaseEvaluator
The LLMJudge evaluator uses a language model to judge whether an output meets specified rubric.
A rubric usually is one of the following: - A fact that the output should contain or not contain (rubric="Output must contain the fact that Paris is the capital of France.") - About the style of the output (rubric="Output should be in a formal tone." or "Output should be in German")
Note: Avoid complex instructions in the rubric, as the model may not follow them reliably. Instead, try to break it down into multiple instances of the LLMJudge.
metadata
property
¶
Build metadata from evaluator fields.
the default in BaseEvaluator is overridden because excluding the not pickleable model field seems impossible
from_csv_line
classmethod
¶
Create an LLMJudge from a CSV line.
This method is used by the CSV testset loader to instantiate the evaluator.
See load_testset for more details.
For LLMJudge, the check parameter is treated as the rubric text. If check is a JSON object with a 'rubric' key, that value is used. Otherwise, the entire check string is used as the rubric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result |
required |
tags
|
set[str]
|
Comma-separated tags string |
required |
check
|
str
|
Rubric text or JSON with 'rubric' key |
required |
get_llm
|
Callable[[], Model]
|
Callable that returns a Model instance (defaults to get_default_judge_llm) |
_get_default_judge_llm
|
**kwargs
|
Any
|
Additional parameters (can include 'model' to override default) |
{}
|
Note: The model parameter must be provided. It should come from: - Dependency injection (e.g., a module-level or class-level settings object) - The check column as JSON: {"rubric": "...", "model": "openai:gpt-4o"} - An additional CSV column named 'model'
Source code in src/ragpill/evaluators.py
run
async
¶
Evaluate the output against the rubric using an LLM judge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
EvaluatorContext[object, object, EvaluatorMetadata]
|
The evaluator context containing inputs, output, and metadata. |
required |
Returns:
| Type | Description |
|---|---|
EvaluationReason
|
The evaluation result with the judge's reasoning. |
Source code in src/ragpill/evaluators.py
RegexInSourcesEvaluator¶
ragpill.evaluators.RegexInSourcesEvaluator
dataclass
¶
RegexInSourcesEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, evaluation_function, custom_reason_true='Evaluation function returned True.', custom_reason_false='Evaluation function returned False.', pattern)
Bases: SourcesBaseEvaluator
Evaluator to check if a regex pattern is found in any of the source document's content. The documents are retrieved from mlflow trace and include documents from retriever, tool, and reranker spans.
Both the pattern and document contents are normalized before matching via
_normalize_text, which applies:
- Case-folding - all text is lowercased (
str.casefold), so matching is always case-insensitive. Using the(?i)flag is therefore redundant. - Unicode NFKC - compatibility characters are unified
(e.g.
UF₆↔UF6). - Whitespace collapsing - runs of whitespace become a single space.
- Quote normalization - curly quotes, guillemets, primes, etc. are
replaced with a straight single quote
'. - Markdown subscript stripping - e.g.
UF~6~→UF6. - Trailing period stripping.
Tip: Use inline regex flags to modify matching behavior:
(?s)pattern- Dotall mode (.matches newlines, useful for multi-line content)(?m)pattern- Multiline mode (^and$match line boundaries)(?ms)pattern- Combine multiple flags
Example
from_csv_line
classmethod
¶
Create a RegexInSourcesEvaluator from a CSV line.
This method is used by the CSV testset loader to instantiate the evaluator.
See load_testset for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result |
required |
tags
|
set[str]
|
Comma-separated tags string |
required |
check
|
str
|
Regex pattern to search for in document contents |
required |
**kwargs
|
Any
|
Additional attributes for the evaluator |
{}
|
Source code in src/ragpill/evaluators.py
RegexInDocumentMetadataEvaluator¶
ragpill.evaluators.RegexInDocumentMetadataEvaluator
dataclass
¶
RegexInDocumentMetadataEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, evaluation_function, custom_reason_true='Evaluation function returned True.', custom_reason_false='Evaluation function returned False.', metadata_key, pattern)
Bases: SourcesBaseEvaluator
Evaluator to check if a regex pattern is found in a specific metadata field of any document retrieved from mlflow trace.
The documents are retrieved from mlflow trace and include documents from retriever, tool, and reranker spans.
Note: For creating from csv, requires 'check' to be a JSON string with 'pattern' and 'key' fields. Then checks if any document in the used sources has metadata[key] matching the regex pattern.
Both the pattern and metadata values are normalized before matching via
_normalize_text, which applies case-folding (str.casefold),
Unicode NFKC, whitespace collapsing, and quote normalization. Because text
is already case-folded, the (?i) flag is redundant.
Inline regex flags still work:
(?s)pattern- Dotall mode (.matches newlines, useful for multi-line metadata values)(?m)pattern- Multiline mode (^and$match line boundaries)(?ms)pattern- Combine multiple flags
Example
from_csv_line
classmethod
¶
Create a RegexInDocumentMetadataEvaluator from a CSV line.
This method is used by the CSV testset loader to instantiate the evaluator.
See load_testset for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result |
required |
tags
|
set[str]
|
Comma-separated tags string |
required |
check
|
str
|
json with 2 keys: "pattern" and "key". Regex pattern to search for in document metadata key. |
required |
**kwargs
|
Any
|
Additional attributes for the evaluator |
{}
|
Source code in src/ragpill/evaluators.py
RegexInOutputEvaluator¶
ragpill.evaluators.RegexInOutputEvaluator
dataclass
¶
RegexInOutputEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, pattern)
Bases: BaseEvaluator
Check whether a regex pattern matches the stringified output.
Both the pattern and the output are normalized before matching via
_normalize_text, which applies case-folding (str.casefold),
Unicode NFKC, whitespace collapsing, and quote normalization.
Because text is already case-folded, the (?i) flag is redundant.
CSV usage examples
check="error|failure"check='{"pattern": "success"}'
from_csv_line
classmethod
¶
Create a RegexInOutputEvaluator from a CSV line.
Source code in src/ragpill/evaluators.py
run
async
¶
Check whether the regex pattern matches the normalized task output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
EvaluatorContext[object, object, EvaluatorMetadata]
|
The evaluator context containing inputs, output, and metadata. |
required |
Returns:
| Type | Description |
|---|---|
EvaluationReason
|
The evaluation result indicating whether the pattern matched. |
Source code in src/ragpill/evaluators.py
LiteralQuoteEvaluator¶
ragpill.evaluators.LiteralQuoteEvaluator
dataclass
¶
Bases: SourcesBaseEvaluator
Verify that all markdown quotes in the output appear literally in source documents.
This evaluator ensures citations are accurate by checking that any text quoted
in markdown blockquotes (lines starting with >) actually appears in the
retrieved source documents. This is particularly valuable for RAG systems where
accuracy of quoted material is critical.
The evaluator:
- Extracts all markdown blockquotes (lines starting with
>) from the output - Cleans quotes by removing quotation marks and normalizing whitespace
- Verifies each quote appears literally (ignoring whitespace) in source documents
- Reports any missing quotes with their referenced filenames when available
Only lines starting with > (after leading whitespace) are considered markdown
quotes. Regular quoted text like "this" or 'this' is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result (default: True) |
True
|
tags
|
set[str] | None
|
Set of tags for categorizing this evaluator |
None
|
attributes
|
dict[str, Any] | None
|
Additional attributes for the evaluator |
None
|
Example
from ragpill.evaluators import LiteralQuoteEvaluator
# Create evaluator
evaluator = LiteralQuoteEvaluator(
expected=True,
tags={"quotation", "accuracy"}
)
# Output with markdown quote
output = '''
The report states:
> "'no longer outstanding at this stage' does not mean 'resolved'."
(File: [report.txt](link), Paragraph: 38)
'''
# The evaluator will verify this quote exists in the source documents
Markdown Quote Format
The evaluator recognizes standard markdown blockquotes:
Note
- Whitespace differences between quotes and source text are ignored
- Quotation marks (
",',',',",") are stripped before comparison - File references in format
(File: [filename](...))are extracted and included in error messages - Empty quotes (after cleaning) are skipped
- Quotes must appear literally in source documents (no fuzzy matching)
See Also
SourcesBaseEvaluator:
Base class that retrieves source documents from MLflow traces
RegexInSourcesEvaluator:
Similar evaluator using regex patterns instead of literal quotes
Source code in src/ragpill/evaluators.py
from_csv_line
classmethod
¶
Create a LiteralQuoteEvaluator from a CSV line.
This method is used by the CSV testset loader to instantiate the evaluator.
See load_testset for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result |
required |
tags
|
set[str]
|
Comma-separated tags string |
required |
check
|
str
|
Not used for this evaluator (can be empty) |
required |
**kwargs
|
Any
|
Additional attributes for the evaluator |
{}
|
Source code in src/ragpill/evaluators.py
run
async
¶
Override run to have access to both output and documents.
Source code in src/ragpill/evaluators.py
HasQuotesEvaluator¶
ragpill.evaluators.HasQuotesEvaluator
dataclass
¶
HasQuotesEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, min_quotes=1, max_quotes=-1)
Bases: BaseEvaluator
Check if the output contains a minimum (and optionally maximum) number of markdown quotes.
This evaluator verifies that the output includes at least a specified number
of markdown blockquotes (lines starting with >). Useful for ensuring responses
include citations, evidence, or quoted material.
Only lines starting with > (after leading whitespace) are considered markdown
quotes. Regular quoted text like "this" or 'this' is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_quotes
|
int
|
Minimum number of quotes required (default: 1) |
1
|
max_quotes
|
int
|
Maximum number of quotes allowed (default: -1, meaning no maximum) |
-1
|
expected
|
bool | None
|
Expected evaluation result (default: True) |
None
|
tags
|
set[str]
|
Set of tags for categorizing this evaluator |
set()
|
attributes
|
dict[str, Any]
|
Additional attributes for the evaluator |
dict()
|
Example
from ragpill.evaluators import HasQuotesEvaluator
# Require at least 2 quotes
evaluator = HasQuotesEvaluator(
min_quotes=2,
expected=True,
tags={"quotation", "format"}
)
# Require between 2 and 5 quotes
evaluator = HasQuotesEvaluator(
min_quotes=2,
max_quotes=5,
expected=True,
tags={"quotation", "format"}
)
# This output has 2 quotes and will pass
output = '''
The report states two key points:
> "First important point."
And also:
> "Second important point."
'''
Note
- Multi-line quotes (consecutive lines with
>) are counted as one quote - Empty quotes (only whitespace after
>) are not counted - The evaluator passes if min_quotes <= num_quotes <= max_quotes (or no max if max_quotes=-1)
- Set expected=False to verify that quotes are NOT within the specified range
See Also
LiteralQuoteEvaluator:
Verifies quotes appear literally in source documents
BaseEvaluator:
Base class for all evaluators
from_csv_line
classmethod
¶
Create a HasQuotesEvaluator from a CSV line.
This method is used by the CSV testset loader to instantiate the evaluator.
See load_testset for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
bool
|
Expected evaluation result |
required |
tags
|
set[str]
|
Comma-separated tags string |
required |
check
|
str
|
Either an integer for min_quotes, or JSON with 'min_quotes' and optionally 'max_quotes'. If empty, defaults to min_quotes=1, max_quotes=-1. |
required |
**kwargs
|
Any
|
Additional attributes for the evaluator |
{}
|
Example
In CSV, use check="3" to require at least 3 quotes. Or use check='{"min_quotes": 2, "max_quotes": 5}' to require 2-5 quotes.
Source code in src/ragpill/evaluators.py
run
async
¶
Check if output contains the required number of quotes (within min/max bounds).
Source code in src/ragpill/evaluators.py
Base Evaluators¶
These are Evaluators that are useful to inherit from. See Create custom evaluators
SpanBaseEvaluator¶
ragpill.evaluators.SpanBaseEvaluator
dataclass
¶
SpanBaseEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False)
Bases: BaseEvaluator
Base class for evaluators that inspect the MLflow trace of a run.
Subclasses call :meth:get_trace to obtain a :class:mlflow.entities.Trace
scoped to the current run. This is populated by the Phase 1 execute layer
and passed through :class:~ragpill.eval_types.EvaluatorContext.
Why Span-Based Evaluation?
Traditional evaluators assess task inputs and outputs. For simple tasks,
that's sufficient. For complex multi-step agents, the process matters as
much as the result — RegexInSourcesEvaluator, for example, needs to
look inside retriever/tool spans to verify that certain sources were
actually used.
get_trace
¶
Return the trace associated with the current evaluation context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
EvaluatorContext[Any, Any, EvaluatorMetadata]
|
The evaluator context. |
required |
Returns:
| Type | Description |
|---|---|
Trace
|
The MLflow |
Trace
|
when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/ragpill/evaluators.py
SourcesBaseEvaluator¶
ragpill.evaluators.SourcesBaseEvaluator
dataclass
¶
SourcesBaseEvaluator(evaluation_name=uuid4(), expected=None, attributes=dict(), tags=set(), is_global=False, *, evaluation_function, custom_reason_true='Evaluation function returned True.', custom_reason_false='Evaluation function returned False.')
Bases: SpanBaseEvaluator
This base class that retrieves the sources from mlflow trace.
Note: only documents retrieved from a retriever, reranker or tool span are considered as sources.
get_documents
¶
Retrieve source documents from the run's MLflow trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
EvaluatorContext[Any, Any, EvaluatorMetadata]
|
The evaluator context; |
required |
Returns:
| Type | Description |
|---|---|
list[Document]
|
List of documents extracted from retriever, tool, and reranker |
list[Document]
|
spans in the trace. |
Source code in src/ragpill/evaluators.py
run
async
¶
Retrieve source documents and apply the evaluation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
EvaluatorContext[Any, Any, EvaluatorMetadata]
|
The evaluator context containing inputs, output, and metadata. |
required |
Returns:
| Type | Description |
|---|---|
EvaluationReason
|
The evaluation result with a custom reason message. |