Overview
gepa.optimize() is GEPA’s core optimization function for evolving text components (prompts, code, instructions) toward a given metric. It uses evolutionary search with LLM-based reflection and Pareto-efficient tracking.
Function Signature
Required Parameters
The initial candidate to start with. A mapping from component names to component text.
Training data supplied as an in-memory sequence or a
DataLoader yielding batches for reflective updates.Optional Parameters
Data & Evaluation
Validation data source (sequence or
DataLoader) used for tracking Pareto scores. If not provided, GEPA reuses the trainset.A
GEPAAdapter instance that implements the adapter interface. This allows GEPA to plug into your system’s environment. If not provided, GEPA will use the default adapter with the model defined by task_lm.The model to use for the task. Only used if
adapter is not provided, and is used to initialize the default adapter.A custom evaluator to use for evaluating the candidate program. Only used if
adapter is not provided.Reflection Configuration
A
LanguageModel instance or model name string that is used to reflect on the performance of the candidate program.candidate_selection_strategy
CandidateSelector | Literal['pareto', 'current_best', 'epsilon_greedy']
default:"'pareto'"
The strategy to use for selecting the candidate to update. Supported strategies: ‘pareto’, ‘current_best’, ‘epsilon_greedy’.
Strategy for tracking Pareto frontiers:
'instance': tracks per validation example'objective': tracks per objective metric'hybrid': combines both'cartesian': tracks per (example, objective) pair
Whether to skip updating the candidate if it achieves a perfect score on the minibatch.
Strategy for selecting training examples. Can be a
BatchSampler instance or a string for a predefined strategy.The number of examples to use for reflection in each proposal step. Defaults to 3. Only valid when
batch_sampler='epoch_shuffled' (default).The perfect score to achieve.
The prompt template to use for reflection. Can be either a string (applied to all components) or a dict mapping component names to their specific templates. Must contain
<curr_param> and <side_info> placeholders.Optional custom function for proposing new candidates. If provided, this will be used instead of the default LLM-based reflection approach. Signature:
(candidate, reflective_dataset, components_to_update) -> dict[str, str].Component Selection
Component selection strategy. Can be a
ReflectionComponentSelector instance or a string (‘round_robin’, ‘all’). The ‘round_robin’ strategy cycles through components in order. The ‘all’ strategy selects all components for modification in every GEPA iteration.Merge Configuration
Whether to use the merge strategy.
The maximum number of merge invocations to perform.
Minimum number of shared validation ids required between parents before attempting a merge subsample. Only relevant when using
val_evaluation_policy other than full_eval.Budget & Stopping
Optional maximum number of metric calls to perform. If not provided,
stop_callbacks must be provided.Optional stopper(s) that return True when optimization should stop. Examples:
FileStopper, TimeoutStopCondition, SignalStopper, NoImprovementStopper, or custom stopping logic. If not provided, max_metric_calls must be provided.Logging & Tracking
A
LoggerProtocol instance that is used to log the progress of the optimization.The directory to save the results to. Optimization state and results will be saved to this directory. If the directory already exists, GEPA will read the state from this directory and resume the optimization from the last saved state. If provided, a
FileStopper is automatically created which checks for the presence of “gepa.stop” in this directory.Optional list of callback objects for observing optimization progress. Callbacks receive events like
on_optimization_start, on_iteration_start, on_candidate_accepted, etc.Whether to use Weights and Biases to log the progress of the optimization.
The API key to use for Weights and Biases.
Additional keyword arguments to pass to the Weights and Biases initialization.
Whether to use MLflow to log the progress of the optimization. Both wandb and mlflow can be used simultaneously.
The tracking URI to use for MLflow.
The experiment name to use for MLflow.
Whether to track the best outputs on the validation set. If True,
GEPAResult will contain the best outputs obtained for each task in the validation set.Show a tqdm progress bar over metric calls when enabled.
Use cloudpickle instead of pickle. This can be helpful when the serialized state contains dynamically generated DSPy signatures.
Evaluation Caching
Whether to cache the (score, output, objective_scores) of (candidate, example) pairs. If True and a cache entry exists, GEPA will skip the fitness evaluation and use the cached results.
Reproducibility
The seed to use for the random number generator.
Whether to propagate proposer/evaluator exceptions instead of stopping gracefully.
val_evaluation_policy
EvaluationPolicy[DataId, DataInst] | Literal['full_eval'] | None
default:"None"
Strategy controlling which validation ids to score each iteration and which candidate is currently best. Supported strings: “full_eval” (evaluate every id each time). Passing None defaults to “full_eval”.
Returns
A
GEPAResult object containing the optimization results, including the best candidate, all explored candidates, validation scores, and Pareto frontier information.Key Concepts
System & Candidate
- System: A harness that uses text components to perform a task. Each text component of the system to be optimized is a named component of the system.
- Candidate: A mapping from component names to component text. A concrete instantiation of the system is realized by setting the text of each system component to the text provided by the candidate mapping.
- DataInst: An (uninterpreted) data type over which the system operates.
- RolloutOutput: The output of the system on a
DataInst.
Optimization Strategies
At each iteration, GEPA proposes a new candidate using one of the following strategies:- Reflective mutation: GEPA proposes a new candidate by mutating the current candidate, leveraging rich textual feedback.
- Merge: GEPA proposes a new candidate by merging 2 candidates that are on the Pareto frontier.
Example Usage
See Also
- optimize_anything() - Simpler API for optimizing any text artifact
- GEPAResult - Result object returned by optimize()
- Custom Adapters - How to implement custom adapters