Scorer
scorer ¶
Protocol definition for scoring agent outputs.
This module defines the Scorer protocol that enables custom scoring logic for evaluating agent outputs in the evolution engine. The protocol provides both synchronous and asynchronous methods, returning a tuple of score and metadata.
| ATTRIBUTE | DESCRIPTION |
|---|---|
Scorer | Protocol for scoring agent outputs. TYPE: |
scorer_accepts_trajectory | Report whether a scorer declares the optional TYPE: |
Examples:
Implement a simple exact-match scorer:
from gepa_adk import Scorer
class ExactMatchScorer:
def score(
self,
input_text: str,
output: str,
expected: str | None = None,
) -> tuple[float, dict]:
if expected is None:
return 0.0, {"error": "Expected value required"}
match = output.strip() == expected.strip()
return (1.0 if match else 0.0), {"exact_match": match}
async def async_score(
self,
input_text: str,
output: str,
expected: str | None = None,
) -> tuple[float, dict]:
return self.score(input_text, output, expected)
Use the scorer:
scorer = ExactMatchScorer()
score, metadata = scorer.score("What is 2+2?", "4", "4")
assert score == 1.0
assert metadata["exact_match"] is True
Opt in to the execution trajectory by declaring trajectory:
from gepa_adk.domain.trajectory import ADKTrajectory
class UsedSearchScorer:
def score(
self,
input_text: str,
output: str,
expected: str | None = None,
*,
trajectory: ADKTrajectory | None = None,
) -> tuple[float, dict]:
names = [c.name for c in trajectory.tool_calls] if trajectory else []
return (1.0 if "search" in names else 0.0), {"tools": names}
async def async_score(
self,
input_text: str,
output: str,
expected: str | None = None,
*,
trajectory: ADKTrajectory | None = None,
) -> tuple[float, dict]:
return self.score(input_text, output, expected, trajectory=trajectory)
See Also
gepa_adk.adapters: Scorer implementations (e.g., CriticScorer, exact-match scorers).gepa_adk.adapters.scoring.require_tool: Wrapper that scores 0.0 when a named tool did not run.
Notes
The protocol defines both synchronous and asynchronous scoring methods to support various use cases. Score values should be normalized between 0.0 and 1.0 by convention, with higher values indicating better performance. The protocol does not enforce this range.
A scorer may also declare an optional parameter named trajectory (trajectory: ADKTrajectory | None = None, keyword-only or positional-or-keyword) on score and async_score. The evolution adapters check async_score with scorer_accepts_trajectory and send the row's ADKTrajectory as a keyword argument only when the parameter is declared. A scorer with the three-argument signature is called exactly as before. A **kwargs catch-all, a positional-only trajectory and a **trajectory catch-all do not count as a declaration.
Scorer ¶
Bases: Protocol
flowchart TD
gepa_adk.ports.scorer.Scorer[Scorer]
click gepa_adk.ports.scorer.Scorer href "" "gepa_adk.ports.scorer.Scorer"
Protocol for scoring agent outputs.
Implementations provide scoring logic that evaluates how well an agent's output matches expected results or quality criteria.
Both synchronous and asynchronous methods are defined. Implementations should provide both, though callers may use only one based on context.
Examples:
Implement a simple fixed scorer for testing:
class FixedScorer:
def score(
self,
input_text: str,
output: str,
expected: str | None = None,
) -> tuple[float, dict]:
return 0.5, {"note": "Fixed score for testing"}
async def async_score(
self,
input_text: str,
output: str,
expected: str | None = None,
) -> tuple[float, dict]:
return self.score(input_text, output, expected)
Verify protocol compliance:
from gepa_adk import Scorer
scorer = FixedScorer()
assert isinstance(scorer, Scorer) # Runtime check works
Notes
All implementations must provide both score() and async_score() methods to satisfy the protocol. Score values should be normalized between 0.0 and 1.0 by convention, with higher values indicating better performance. The protocol does not enforce this range.
An implementation may add an optional parameter named trajectory (trajectory: ADKTrajectory | None = None, keyword-only or positional-or-keyword) to both methods. The adapters send the row's trajectory only to a scorer whose async_score declares that parameter by name; see scorer_accepts_trajectory.
Source code in src/gepa_adk/ports/scorer.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
score ¶
Score an agent output synchronously.
| PARAMETER | DESCRIPTION |
|---|---|
input_text | The input provided to the agent. TYPE: |
output | The agent's generated output to score. TYPE: |
expected | Optional expected/reference output for comparison. Pass None for open-ended evaluation without expected output. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
float | A tuple of (score, metadata) where: |
dict[str, Any] |
|
tuple[float, dict[str, Any]] |
|
Examples:
Basic usage:
score, meta = scorer.score("What is 2+2?", "4", "4")
assert score == 1.0
assert meta.get("exact_match") is True
Scoring without expected output:
score, meta = scorer.score("Explain gravity", response)
# Scorer evaluates based on quality criteria, not exact match
Notes
Operations complete synchronously and block until scoring finishes. Use async_score() for I/O-bound operations like LLM calls.
Source code in src/gepa_adk/ports/scorer.py
async_score async ¶
async_score(
input_text: str,
output: str,
expected: str | None = None,
) -> tuple[float, dict[str, Any]]
Score an agent output asynchronously.
| PARAMETER | DESCRIPTION |
|---|---|
input_text | The input provided to the agent. TYPE: |
output | The agent's generated output to score. TYPE: |
expected | Optional expected/reference output for comparison. Pass None for open-ended evaluation without expected output. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
float | A tuple of (score, metadata) where: |
dict[str, Any] |
|
tuple[float, dict[str, Any]] |
|
Examples:
Async usage:
score, meta = await scorer.async_score("Explain gravity", response)
print(f"Quality: {score:.2f} - {meta.get('feedback')}")
Concurrent scoring:
import asyncio
tasks = [
scorer.async_score(input, output, expected)
for input, output, expected in batch
]
scores = await asyncio.gather(*tasks)
Notes
Operations run asynchronously and can be executed concurrently. Prefer this method for I/O-bound scoring operations such as LLM-based evaluation or external API calls.
Source code in src/gepa_adk/ports/scorer.py
scorer_accepts_trajectory ¶
Report whether a scorer declares a trajectory parameter.
| PARAMETER | DESCRIPTION |
|---|---|
scorer | Object with an TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True when the signature of |
bool | named |
bool | keyword or keyword-only). False when it does not, when the name |
bool | belongs to a positional-only parameter or a |
bool | catch-all, when the only catch-all is |
bool |
|
Examples:
from gepa_adk.ports.scorer import scorer_accepts_trajectory
class ThreeArg:
async def async_score(self, input_text, output, expected=None):
return 0.0, {}
class WithTrajectory:
async def async_score(
self, input_text, output, expected=None, *, trajectory=None
):
return 0.0, {}
assert scorer_accepts_trajectory(ThreeArg()) is False
assert scorer_accepts_trajectory(WithTrajectory()) is True
Notes
Uses stdlib inspect only. Adapters call it once per adapter instance, not once per evaluated row.