inspect_evals_utils.scorers¶
Grader vocabulary and the free-text verdict parser for LLM-judged scorers.
The ScoreReason constants and GraderFailureMode were moved here from
inspect_evals.utils.scorers so that a standalone eval can name a grader
failure the same way the registry does. schema_tool_graded_scorer stays in
inspect_evals: it uses private inspect_ai registry APIs.
GraderFailureMode
module-attribute
¶
GraderFailureMode = Literal[
"grader_parse_failure",
"grader_refusal",
"grader_no_tool_call",
"grader_schema_mismatch",
"grader_invalid_grade",
]
How the grader failed, one level finer than ScoreReason.
All of these are grader_failed upstream. We keep the distinction under our
own metadata["grader_failure_mode"] key so that a run can be partitioned by
failure mode without parsing prose out of explanation, without claiming a
vocabulary inspect_ai has not adopted.
INVALID_RESPONSE_FORMAT
module-attribute
¶
INVALID_RESPONSE_FORMAT: Final[ScoreReason] = (
"invalid_response_format"
)
GRADER_PARSE_FAILURE
module-attribute
¶
GRADER_PARSE_FAILURE: Final[GraderFailureMode] = (
"grader_parse_failure"
)
GRADER_NO_TOOL_CALL
module-attribute
¶
GRADER_NO_TOOL_CALL: Final[GraderFailureMode] = (
"grader_no_tool_call"
)
GRADER_SCHEMA_MISMATCH
module-attribute
¶
GRADER_SCHEMA_MISMATCH: Final[GraderFailureMode] = (
"grader_schema_mismatch"
)
GRADER_INVALID_GRADE
module-attribute
¶
GRADER_INVALID_GRADE: Final[GraderFailureMode] = (
"grader_invalid_grade"
)
VerdictAnchor
module-attribute
¶
VerdictAnchor = Literal['label', 'line']
Where a verdict is read from: a labelled line, or the leading token of a line.
VerdictPosition
module-attribute
¶
VerdictPosition = Literal['first', 'last']
Which candidate line holds the verdict when the reply has more than one.
VERDICT_LABEL
module-attribute
¶
VERDICT_LABEL: Final[str] = 'ANSWER'
The label verdict_instruction asks for. Pair it with anchor="label".
VerdictMatch
dataclass
¶
A verdict read from a judge's reply.
Attributes:
| Name | Type | Description |
|---|---|---|
token |
str
|
The matched token, spelled as the caller passed it in |
start |
int
|
Offset of the token in the original text. |
end |
int
|
Offset just past the token in the original text. |
line |
str
|
The line the verdict was read from, without its line ending. |
VerdictParseFailure
dataclass
¶
A judge's reply that holds no usable verdict.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The reply as received. |
detail |
str
|
Why no verdict was read, in a few words. |
unscored
¶
unscored(**kwargs: Any) -> Score
Build the unscored Score for this failure.
The score has reason grader_failed,
metadata["grader_failure_mode"] grader_parse_failure and the
reply under metadata["grader_payload"]["grader_response"], the keys
inspect_evals' schema_tool_graded_scorer records. Keyword arguments
pass through to Score.unscored; a metadata argument is merged
over those defaults.
find_verdict
¶
find_verdict(
text: str,
tokens: Iterable[str],
*,
anchor: VerdictAnchor,
position: VerdictPosition,
label: str | None = None,
ignore_quoted: bool = False,
) -> VerdictMatch | VerdictParseFailure
Read a single-value verdict from a judge's free-text reply.
The selected line must name exactly one token. Under anchor="label"
that check scans the line from just after the separator, so a label that
is itself a token (Correct: INCORRECT) or a numbered list marker
(1. Rating: 3) does not count as a second verdict. Under
anchor="line" it scans the whole line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The judge's reply. |
required |
tokens
|
Iterable[str]
|
The verdicts the prompt offered, such as |
required |
anchor
|
VerdictAnchor
|
|
required |
position
|
VerdictPosition
|
|
required |
label
|
str | None
|
The field name the prompt asks for, required with
|
None
|
ignore_quoted
|
bool
|
Skip lines that start with |
False
|
Returns:
| Type | Description |
|---|---|
VerdictMatch | VerdictParseFailure
|
A |
VerdictMatch | VerdictParseFailure
|
no usable verdict. |
Raises:
| Type | Description |
|---|---|
ValueError
|
The token set or the label is unusable. |
parse_verdict
¶
parse_verdict(
text: str,
tokens: Mapping[str, T],
*,
anchor: VerdictAnchor,
position: VerdictPosition,
label: str | None = None,
ignore_quoted: bool = False,
) -> T | VerdictParseFailure
Read a single-value verdict from a judge's reply and map it to a value.
This is for a verdict that is one value: yes or no, a letter, one number. A judge that returns several fields (a rubric, a verdict plus a confidence) should use a structured grader, such as a tool call with a schema, rather than this parser.
On a VerdictParseFailure, a scorer that returns one value returns
failure.unscored(). A scorer whose Score.value is a dict fed by
several judges sets this judge's key to nan and keeps the other keys.
Bounded, grader-only retries for a format failure are acceptable before
unscoring, and a retry must not be served from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The judge's reply. |
required |
tokens
|
Mapping[str, T]
|
Each verdict the prompt offered, mapped to the value to return. |
required |
anchor
|
VerdictAnchor
|
As for |
required |
position
|
VerdictPosition
|
As for |
required |
label
|
str | None
|
As for |
None
|
ignore_quoted
|
bool
|
As for |
False
|
Returns:
| Type | Description |
|---|---|
T | VerdictParseFailure
|
The value mapped from the matched token, or a |
verdict_instruction
¶
verdict_instruction(
tokens: Iterable[str], *, label: str = VERDICT_LABEL
) -> str
One sentence telling the judge how to end its reply.
For ("yes", "no") it returns End your reply with a line of the form
`ANSWER: yes` or `ANSWER: no`, and nothing after it. Read the reply
with parse_verdict(..., anchor="label", label=VERDICT_LABEL,
position="last"). An eval that keeps an upstream prompt for parity
does not use this sentence; it states whatever anchor and position that
prompt implies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
Iterable[str]
|
The verdicts the judge may give, at least two. |
required |
label
|
str
|
The field name to ask for. |
VERDICT_LABEL
|
Returns:
| Type | Description |
|---|---|
str
|
The instruction sentence. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Fewer than two tokens, or an unusable token set. |