Custom Evaluators
This lesson explains when custom evaluators are needed and shows how code-based and prompt-based evaluators are written and made reliable.
Learning outcomes
After completing this lesson, you will be able to:
- Identify requirements built-in evaluators cannot measure.
- Write a code-based evaluator for the SDK.
- Describe the parts of a prompt-based evaluator.
- Explain how custom evaluators are validated.
When built-in evaluators are not enough
Built-in evaluators measure general properties. Applications also have specific rules: an invoice extractor's JSON must match a schema and its totals must add up; a support assistant must cite a document ID that exists; a banking assistant must include a regulatory disclaimer when it discusses investments; a summary must stay under 120 words. These requirements are what custom evaluators measure.
Code-based evaluators
In the Azure AI Evaluation SDK, a code-based evaluator is any callable, usually a class with __call__, that takes named inputs and returns a dictionary of results. It is used with evaluate() exactly like a built-in evaluator, with its own column mapping.
import json, re
class CitationEvaluator:
# Checks that every citation in the response refers to a document that was retrieved.
def __call__(self, *, response: str, context: str, **kwargs):
cited = set(re.findall(r"\[(doc-\d+)\]", response))
available = set(re.findall(r"\[(doc-\d+)\]", context))
valid = bool(cited) and cited <= available
return {"citation_valid": 1.0 if valid else 0.0, "citations": len(cited)}
result = evaluate(data="eval/datasets/support_v4.jsonl", target=ask_assistant,
evaluators={"citations": CitationEvaluator()},
evaluator_config={"citations": {"column_mapping": {
"response": "${target.response}", "context": "${target.context}"}}})Custom code-based evaluators registered in a Foundry project (preview) follow a similar contract: a grade function that receives the sample and item and returns a score from 0 to 1, run in a sandbox with limits on code size, run time and memory and no network access, so they must be self-contained.
Prompt-based evaluators
A prompt-based evaluator asks a judge model to score a response against a rubric that you write. The rubric should define each score level precisely, give an example where useful, and require structured output, such as JSON with a score and a reason, so results can be parsed and aggregated.
system:
You assess whether a support answer follows Contoso's refund policy.
Score 1 if the answer promises a refund the policy does not allow.
Score 3 if the answer is compliant but omits a required step.
Score 5 if the answer is compliant and includes every required step.
Return JSON: {"score": <1-5>, "reason": "<one sentence>"}
user:
Policy: {{policy}}
Answer: {{response}}Foundry also supports rubric-based evaluators (preview) that combine weighted criteria into a normalized score, and prompt-based custom evaluators registered in the project, which can then be used in cloud, scheduled and continuous evaluations like built-in ones.
Making custom evaluators reliable
A custom evaluator becomes a release gate, so it must be trustworthy. The following practices apply to both kinds.
- Test the evaluator on hand-labelled examples, including clear passes, clear failures and borderline cases, before relying on it.
- Measure agreement between a prompt-based evaluator and human labels, and refine the rubric until agreement is acceptable.
- Version evaluators in Git with the datasets and thresholds they are used with.
- Prefer code where a rule can be checked deterministically, and keep judge prompts for qualities that need judgement.
- Return a reason or diagnostic fields, so failures can be investigated without rerunning.
Measuring a business rule
A financial assistant must include the sentence "This is not financial advice" whenever an answer mentions a specific fund, and must never recommend buying or selling. The team wants both rules checked on every pull request at minimal cost.
The disclaimer rule can be checked deterministically: a code-based evaluator detects fund names from a list and checks for the exact sentence. The recommendation rule needs judgement, because recommendations can be phrased in many ways, so it uses a prompt-based evaluator with a precise rubric, validated against labelled examples. Both run in the pull request evaluation with thresholds that fail the check.
Using a judge prompt for the disclaimer would spend judge calls on a rule that code checks exactly. Relevance or coherence would not detect either rule. Relying on content filters would not detect recommendations, which are not harmful content.
A response must always be valid JSON that matches a schema. Should this be measured with a code-based or prompt-based evaluator?
Show answer
A code-based evaluator. Schema validity is a deterministic rule, so code checks it exactly, repeatably and at negligible cost, while a judge model might be inconsistent.
Exam Tips
Remember for the exam
- Write custom evaluators for application-specific rules and qualities.
- Code-based evaluators check rules deterministically; prompt-based evaluators apply a rubric with a judge.
- In the SDK, a custom evaluator is a callable returning a dictionary, used with evaluate() and column mapping.
- Validate evaluators against human labels and version them with datasets and thresholds.