Selector
selector ¶
Protocol definition for candidate selection strategies.
Note
This module defines the async contract for candidate selection strategies and evaluation policies.
CandidateSelectorProtocol ¶
Bases: Protocol
flowchart TD
gepa_adk.ports.selector.CandidateSelectorProtocol[CandidateSelectorProtocol]
click gepa_adk.ports.selector.CandidateSelectorProtocol href "" "gepa_adk.ports.selector.CandidateSelectorProtocol"
Async protocol for candidate selection strategies.
Note
Adapters implementing this protocol provide strategies for selecting candidates from the Pareto frontier for mutation.
Examples:
Source code in src/gepa_adk/ports/selector.py
select_candidate async ¶
select_candidate(state: ParetoState) -> int
Select a candidate index for mutation.
| PARAMETER | DESCRIPTION |
|---|---|
state | Current evolution state with Pareto frontier tracking. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
int | Index of selected candidate. |
| RAISES | DESCRIPTION |
|---|---|
NoCandidateAvailableError | If state has no candidates. |
Note
Outputs a candidate index from the frontier for mutation, enabling Pareto-aware selection strategies.
Examples:
Source code in src/gepa_adk/ports/selector.py
EvaluationPolicyProtocol ¶
Bases: Protocol
flowchart TD
gepa_adk.ports.selector.EvaluationPolicyProtocol[EvaluationPolicyProtocol]
click gepa_adk.ports.selector.EvaluationPolicyProtocol href "" "gepa_adk.ports.selector.EvaluationPolicyProtocol"
Protocol for valset evaluation strategies.
Determines which validation examples to evaluate per iteration and how to identify the best candidate based on evaluation results.
Note
Adapters implementing this protocol control evaluation cost and candidate selection strategies for scalable evolution runs.
Examples:
class MyPolicy:
def get_eval_batch(
self, valset_ids: Sequence[int], state: ParetoState
) -> list[int]:
return list(valset_ids)
def get_best_candidate(self, state: ParetoState) -> int:
return 0
def get_valset_score(self, candidate_idx: int, state: ParetoState) -> float:
return 0.5
Source code in src/gepa_adk/ports/selector.py
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 | |
get_eval_batch ¶
get_eval_batch(
valset_ids: Sequence[int],
state: ParetoState,
target_candidate_idx: int | None = None,
) -> list[int]
Select validation example indices to evaluate.
| PARAMETER | DESCRIPTION |
|---|---|
valset_ids | All available validation example indices (0 to N-1). TYPE: |
state | Current evolution state with candidate scores. TYPE: |
target_candidate_idx | Optional candidate being evaluated (for adaptive policies). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[int] | List of example indices to evaluate this iteration. |
list[int] | Must be a subset of valset_ids (or equal). |
Note
Outputs a list of valset indices to evaluate, which may be a subset or the full valset depending on the policy implementation.
Examples:
batch = policy.get_eval_batch([0, 1, 2, 3, 4], state)
# Returns: [0, 1, 2, 3, 4] for full evaluation
Source code in src/gepa_adk/ports/selector.py
get_best_candidate ¶
get_best_candidate(state: ParetoState) -> int
Return index of best candidate based on evaluation results.
| PARAMETER | DESCRIPTION |
|---|---|
state | Current evolution state with candidate_scores populated. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
int | Index of best performing candidate. |
| RAISES | DESCRIPTION |
|---|---|
NoCandidateAvailableError | If state has no candidates. |
Note
Outputs the index of the best candidate based on the policy's scoring strategy (e.g., highest average score).
Examples:
Source code in src/gepa_adk/ports/selector.py
get_valset_score ¶
get_valset_score(
candidate_idx: int, state: ParetoState
) -> float
Return aggregated valset score for a candidate.
| PARAMETER | DESCRIPTION |
|---|---|
candidate_idx | Index of candidate to score. TYPE: |
state | Current evolution state. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
float | Aggregated score (typically mean across evaluated examples). |
float | Returns float('-inf') if candidate has no evaluated scores. |
Note
Outputs an aggregated score for the candidate, typically the mean across evaluated examples, or negative infinity if no scores exist.
Examples:
Source code in src/gepa_adk/ports/selector.py
ComponentSelectorProtocol ¶
Bases: Protocol
flowchart TD
gepa_adk.ports.selector.ComponentSelectorProtocol[ComponentSelectorProtocol]
click gepa_adk.ports.selector.ComponentSelectorProtocol href "" "gepa_adk.ports.selector.ComponentSelectorProtocol"
Async protocol for component selection strategies.
Note
Adapters implementing this protocol determine which candidate components to update during mutation, enabling flexible evolution strategies.
Examples:
class MySelector:
async def select_components(
self, components: list[str], iteration: int, candidate_idx: int
) -> list[str]:
return components[:1]
Source code in src/gepa_adk/ports/selector.py
select_components async ¶
Select components to update for the current iteration.
| PARAMETER | DESCRIPTION |
|---|---|
components | List of available component keys (e.g. ["instruction", "input_schema"]). TYPE: |
iteration | Current global iteration number (0-based). TYPE: |
candidate_idx | Index of the candidate being evolved. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list[str] | List of component keys to update. |
| RAISES | DESCRIPTION |
|---|---|
ValueError | If components list is empty. |
Note
Outputs a list of component keys to update, enabling selective mutation of specific candidate components.
Examples:
selected = await selector.select_components(
components=["instruction", "schema"], iteration=1, candidate_idx=0
)