Overview
GEPA provides a comprehensive callback system for observing and instrumenting optimization runs. Callbacks are synchronous, observational (cannot modify state), and receive fullGEPAState access for maximum flexibility.
All callbacks implement the GEPACallback protocol and receive typed event objects containing relevant parameters.
GEPACallback Protocol
TheGEPACallback protocol defines optional callback methods for different optimization events. Implement only the methods you need.
Using Callbacks
Pass callbacks to theoptimize() function:
Callback Events
Each callback method receives a single event TypedDict containing all relevant parameters. This design allows easy extension without breaking changes.Optimization Lifecycle
on_optimization_start
Called when optimization begins.on_optimization_end
Called when optimization completes.Iteration Events
on_iteration_start
Called at the start of each iteration.on_iteration_end
Called at the end of each iteration.Evaluation Events
on_evaluation_start
Called before evaluating a candidate.on_evaluation_end
Called after evaluating a candidate.on_evaluation_skipped
Called when an evaluation is skipped.on_valset_evaluated
Called after evaluating on the validation set.Candidate Selection Events
on_candidate_selected
Called when a candidate is selected for mutation.on_minibatch_sampled
Called when a training minibatch is sampled.Proposal Events
on_reflective_dataset_built
Called after building the reflective dataset.on_proposal_start
Called before proposing new instructions.on_proposal_end
Called after proposing new instructions.Acceptance Events
on_candidate_accepted
Called when a new candidate is accepted.on_candidate_rejected
Called when a candidate is rejected.Merge Events
on_merge_attempted
Called when a merge is attempted.on_merge_accepted
Called when a merge is accepted.on_merge_rejected
Called when a merge is rejected.State Events
on_pareto_front_updated
Called when the Pareto front is updated.on_state_saved
Called after state is saved to disk.on_budget_updated
Called when the evaluation budget is updated.Error Events
on_error
Called when an error occurs during optimization.CompositeCallback
TheCompositeCallback class allows you to register multiple callbacks that all receive events.
Example: Progress Tracking Callback
Source Reference
The callback system is implemented insrc/gepa/core/callbacks.py.