Judge Chain
Failure policy shared by every judge fallback chain.
A chain tries its judges in order and returns the first usable result. This module decides what a failed attempt means, so the wrapper loops and the mixed GEval loop agree:
- A failure whose
failure_scopeis JUDGE_CALL or JUDGE_SETUP belongs to the judge that was tried, so it is recorded and the next judge is tried. JUDGE_CALL is a failed call to that judge; JUDGE_SETUP is a wrong setup of that judge, such as its credentials or model. - A failure whose scope is TEST_CASE or RUN stops the chain: no other judge can fix it, so it propagates at once.
- When no judge succeeds and every attempt failed with the same error class and code, the primary judge's failure
is re-raised as itself, so a caller can catch that class. Otherwise the attempts become
EvalJudgesExhaustedErrorwith every attempt as a child. That failure stops the run only when every attempt was a JUDGE_SETUP failure; seeretry_policy.failure_scope.
Trying the next judge is a fallback, never a resubmission of the timed-out request. When a fallback judge succeeds,
the failed attempts go to the collector of collect_fallback_attempts with the model ID of each failed judge, so the
result can show that a fallback judge gave the score.
FailedAttempt
Bases: NamedTuple
One failed judge attempt in a chain that a fallback judge completed.
Attributes:
| Name | Type | Description |
|---|---|---|
model_id |
str | None
|
The model ID of the judge that failed, if known. |
error |
BaseError
|
The normalized failure of that judge. |
JudgeChain(*, operation)
Runs one judge fallback chain: collects the failed attempts and raises the chain's final failure.
Create one per chain run and call run. A loop that cannot use run calls handle from its
except Exception block, succeeded when a judge returned, and raise_final after the loop when no judge
returned. Cancellation stays outside
the chain because it is not an Exception.
Attributes:
| Name | Type | Description |
|---|---|---|
attempts |
list[BaseError]
|
The normalized failure of each failed attempt, in order. |
Initializes the collector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
str
|
Static name of the chain's operation, e.g. "judge.generate". |
required |
handle(error, model_id)
Normalizes a failed attempt, then stops the chain or records the attempt once.
Recording goes through error_reporter.record, which never raises, so a failing log handler cannot stop
the chain from trying the next judge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The failure caught by the loop. |
required |
model_id
|
str | None
|
The model ID of the judge that failed, if known. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
|
BaseError
|
The normalized failure when its scope is TEST_CASE. No other judge can fix either one. |
raise_final(*, media_skips=())
Raises the failure of a chain in which no judge returned a usable result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
media_skips
|
Sequence[str]
|
Model IDs of judges skipped because they cannot take the row's media. Defaults to an empty tuple. |
()
|
Raises:
| Type | Description |
|---|---|
EvalConfigurationError
|
If no judge was attempted. |
BaseError
|
The single failed attempt, or the primary judge's failure when every attempt failed with the same error class and code and no judge was skipped for media. It is re-raised as itself. |
EvalJudgesExhaustedError
|
If the attempts failed in different ways; they are its children and the last one is its cause. |
run(invokers, call)
async
Tries each judge in order until one returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
invokers
|
Sequence[InvokerT]
|
The judges, primary first. |
required |
call
|
Callable[[InvokerT], Awaitable[ResultT]]
|
One attempt with one judge. |
required |
Returns:
| Type | Description |
|---|---|
tuple[InvokerT, ResultT]
|
tuple[InvokerT, ResultT]: The winning judge and its result. |
Raises:
| Type | Description |
|---|---|
BaseError
|
A failure no other judge can fix, or the single failed attempt. |
EvalJudgesExhaustedError
|
If two or more judges failed. |
succeeded()
Reports the failed attempts of a chain in which a judge returned a usable result.
Call it once, when the chain returns. Nothing is reported when the first judge succeeded or when no collector is active.
collect_fallback_attempts()
Collects the failed attempts of each judge chain that a fallback judge completed inside this block.
The collector is local to the current task and the tasks it starts, so concurrent metric evaluations do not share it.
Yields:
| Type | Description |
|---|---|
list[FailedAttempt]
|
list[FailedAttempt]: The failed attempts, in the order the chains completed. |
shared_failure(failures)
Return the first failure when every failure has the same error class and code, else None.
A caller can then catch the class of a failure that every judge shared, for example ProviderAuthError for a
wrong key, also when a fallback judge or a panel of judges is configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
failures
|
Sequence[BaseError]
|
The judges' normalized failures, primary first. |
required |
Returns:
| Type | Description |
|---|---|
BaseError | None
|
BaseError | None: The first failure, or None if there is no failure or the failures differ. |