What are GEPA Adapters?
GEPA connects to your system via theGEPAAdapter interface, enabling optimization of any system with textual parameters. Adapters bridge between GEPA’s evolutionary optimization engine and your specific task implementation.
Core Concept
Adapters implement three key responsibilities:- Program Construction & Evaluation (
evaluate): Execute your system with candidate parameters and return scores - Reflective Dataset Construction (
make_reflective_dataset): Transform execution traces into training data for improvement - Optional Custom Proposal (
propose_new_texts): Override default instruction proposal logic
GEPAAdapter Protocol
The adapter protocol is defined insrc/gepa/core/adapter.py:58:
Type Parameters
- DataInst: User-defined input data type (e.g., questions, documents)
- Trajectory: Execution trace capturing program steps
- RolloutOutput: Final output from program execution
Key Concepts
Candidate
Adict[str, str] mapping component names to their text:
Scores
Higher is better. GEPA uses:- Minibatch:
sum(scores)for acceptance decisions - Full validation set:
mean(scores)for tracking and Pareto fronts
Trajectories
Opaque to GEPA - only consumed by yourmake_reflective_dataset. Should capture:
- Inputs to each component
- Generated outputs
- Error messages or diagnostic info
- Any context needed for feedback generation
Error Handling
Never raise for individual example failures:- Return valid
EvaluationBatchwith failure scores (e.g., 0.0) - Populate trajectories with error details when possible
- Reserve exceptions for systemic failures (missing model, schema mismatch)
Built-in Adapters
DefaultAdapter
Single-turn LLM tasks with system prompt optimization
DSPy Adapter
Optimize DSPy module signature instructions
DSPy Full Program
Evolve entire DSPy programs including structure
Generic RAG
Vector store-agnostic RAG optimization
MCP Adapter
Optimize Model Context Protocol tool usage
TerminalBench
Optimize Terminus terminal-use agents
AnyMaths
Mathematical problem-solving optimization
Creating Custom Adapters
To create your own adapter:- Define your
DataInst,Trajectory, andRolloutOutputtypes - Implement
evaluate()to execute your system - Implement
make_reflective_dataset()to generate feedback - Optionally implement
propose_new_texts()for custom proposal logic
Example Skeleton
Best Practices
Evaluation
- Keep
capture_traces=Falsefast - used frequently during optimization - Make trajectories comprehensive when
capture_traces=True - Return per-example scores that can be aggregated via summation
- Use consistent failure scores (e.g., 0.0) for failed examples
Reflective Datasets
- Keep examples concise but informative
- Include both successful and failed cases
- Provide actionable feedback mentioning specific issues
- Use deterministic subsampling (seeded RNG) for reproducibility
Performance
- Batch API calls when possible
- Cache expensive operations (embeddings, model loading)
- Use parallel execution for independent examples
- Avoid unnecessary deep copies
Integration with GEPA
Use your adapter withgepa.optimize():
Reference Implementation
See the DSPy adapter for a comprehensive reference implementation supporting:- Multiple predictors
- Tool optimization
- Custom proposal logic
- Complex trace handling