Trial builder
trial_builder ¶
Trial building utilities for reflection datasets.
This module provides a shared TrialBuilder class for constructing trial records from evaluation results. Both ADKAdapter and MultiAgentAdapter use this to build consistent trial structures for reflection.
Terminology
- trial: One performance record {feedback, trajectory}
- feedback: Critic evaluation {score, feedback_text, feedback_*}
- trajectory: The journey from input to output with optional trace
| ATTRIBUTE | DESCRIPTION |
|---|---|
TrialBuilder | Builder for trial records from evaluation results. TYPE: |
Examples:
Build a trial record:
from gepa_adk.adapters.trial_builder import TrialBuilder
builder = TrialBuilder()
trial = builder.build_trial(
input_text="What is 2+2?",
output="4",
score=0.95,
metadata={"feedback": "Correct answer"},
)
assert trial["feedback"]["score"] == 0.95
assert trial["trajectory"]["input"] == "What is 2+2?"
See Also
gepa_adk.adapters.adk_adapter: Uses TrialBuilder for single-agent trials.gepa_adk.adapters.multi_agent: Uses TrialBuilder for multi-agent pipeline trials.
Note
This implements the GEPA whitepaper trial structure where score and feedback_text are mandatory, with optional extras like feedback_dimensions and feedback_guidance passed through when available.
TrialBuilder ¶
Build trial records for reflection datasets.
Constructs consistent trial structures following the GEPA whitepaper format. Extracts feedback fields from scorer metadata and builds trajectory dicts.
| ATTRIBUTE | DESCRIPTION |
|---|---|
_logger | Logger for metadata passthrough debugging. TYPE: |
Examples:
Basic trial building:
builder = TrialBuilder()
# With minimal data
trial = builder.build_trial(
input_text="Hello",
output="Hi there!",
score=0.8,
)
# With full metadata
trial = builder.build_trial(
input_text="Explain AI",
output="AI is...",
score=0.9,
metadata={
"feedback": "Clear explanation",
"dimension_scores": {"clarity": 0.95},
"actionable_guidance": "Add examples",
},
extra_trajectory={"component": "instruction"},
)
See Also
build_feedback: Build just the feedback dict.
Note
All optional metadata fields are validated before inclusion to prevent malformed data from propagating to reflection prompts.
Source code in src/gepa_adk/adapters/trial_builder.py
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
__init__ ¶
Initialize the TrialBuilder.
Examples:
Note
Creates a module-scoped logger for metadata passthrough debugging.
Source code in src/gepa_adk/adapters/trial_builder.py
build_feedback ¶
build_feedback(
score: float,
metadata: dict[str, Any] | None = None,
*,
error: str | None = None,
log_passthrough: bool = False,
) -> dict[str, Any]
Build the feedback dict from score and metadata.
Extracts and validates feedback fields from scorer metadata, including feedback_text, feedback_guidance, and feedback_dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
score | Evaluation score (mandatory). TYPE: |
metadata | Optional scorer metadata dict containing: - feedback: Text feedback from critic. - actionable_guidance: Improvement suggestions. - dimension_scores: Per-dimension score breakdown. TYPE: |
error | Optional error message to include in feedback. TYPE: |
log_passthrough | If True, log debug info about metadata extraction. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Feedback dict with keys: - score (mandatory): The evaluation score. - feedback_text (if available): Text feedback from critic. - feedback_guidance (if available): Improvement suggestions. - feedback_dimensions (if available): Dimension score breakdown. - error (if provided): Error message from execution. |
Examples:
builder = TrialBuilder()
# Minimal feedback
feedback = builder.build_feedback(0.75)
assert feedback == {"score": 0.75}
# With metadata
feedback = builder.build_feedback(
0.85,
metadata={
"feedback": "Good work",
"dimension_scores": {"accuracy": 0.9},
},
)
assert "feedback_text" in feedback
Note
Only non-empty strings and dicts are included to keep feedback clean.
Source code in src/gepa_adk/adapters/trial_builder.py
build_trial ¶
build_trial(
input_text: str | None,
output: str,
score: float,
metadata: dict[str, Any] | None = None,
*,
error: str | None = None,
trace: dict[str, Any] | None = None,
extra_trajectory: dict[str, Any] | None = None,
log_passthrough: bool = False,
) -> dict[str, Any]
Build a complete trial record for reflection.
Constructs a trial with feedback and trajectory dicts following the GEPA whitepaper structure.
| PARAMETER | DESCRIPTION |
|---|---|
input_text | The input that was given to the system. Can be None for pipelines where input context is implicit. TYPE: |
output | What the system produced. TYPE: |
score | Evaluation score for this output. TYPE: |
metadata | Optional scorer metadata dict (from CriticScorer). TYPE: |
error | Optional error message from execution. TYPE: |
trace | Optional execution trace dict (tool calls, state, tokens). TYPE: |
extra_trajectory | Optional extra fields to include in trajectory (e.g., component name, component value, tokens). TYPE: |
log_passthrough | If True, log debug info about metadata extraction. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Trial dict with keys: - feedback: Evaluation feedback (score, feedback_text, etc.) - trajectory: Execution journey (input, output, trace, etc.) |
Examples:
builder = TrialBuilder()
# Simple trial
trial = builder.build_trial(
input_text="What is Python?",
output="A programming language",
score=0.9,
)
assert trial["feedback"]["score"] == 0.9
assert trial["trajectory"]["output"] == "A programming language"
# With trace and extra trajectory data
trial = builder.build_trial(
input_text="Count to 3",
output="1, 2, 3",
score=1.0,
trace={"tool_calls": [{"name": "count"}]},
extra_trajectory={"component": "counter"},
)
assert "trace" in trial["trajectory"]
assert trial["trajectory"]["component"] == "counter"
Note
Optional input is only included in trajectory when not None, supporting pipelines where input context is implicit.
Source code in src/gepa_adk/adapters/trial_builder.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
normalize_feedback ¶
Normalize simple or advanced feedback to consistent trial format.
Converts both simple string feedback and advanced dict feedback to a standardized format for use in trial records. This enables the reflection agent to receive consistent feedback regardless of which format the scorer used.
| PARAMETER | DESCRIPTION |
|---|---|
score | The evaluation score (0.0-1.0). TYPE: |
raw_feedback | Either a string (simple format) or dict (advanced format). If None, feedback_text defaults to empty string. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Normalized feedback dict with at minimum: - score: float - feedback_text: str |
dict[str, Any] | Plus optional fields if provided in advanced format: - dimensions: dict[str, float] - guidance: str - Any custom fields from input dict |
Examples:
Simple string feedback:
result = normalize_feedback(0.75, "Good but verbose")
# {"score": 0.75, "feedback_text": "Good but verbose"}
Advanced dict feedback:
result = normalize_feedback(
0.45,
{
"feedback_text": "Too clinical",
"dimension_scores": {"voice": 0.2},
"actionable_guidance": "Add I statements",
},
)
# {
# "score": 0.45,
# "feedback_text": "Too clinical",
# "dimensions": {"voice": 0.2},
# "guidance": "Add I statements"
# }
Fallback to legacy "feedback" key:
result = normalize_feedback(0.6, {"feedback": "Legacy format"})
# {"score": 0.6, "feedback_text": "Legacy format"}
Handle None:
Note
Score parameter always takes precedence over any "score" key in dict. Field mapping applies: "dimension_scores" → "dimensions", "actionable_guidance" → "guidance". Non-string feedback_text values convert to strings. Empty dimension dicts are excluded. Custom fields pass through unchanged.
Source code in src/gepa_adk/adapters/trial_builder.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 | |