skills/dspy-miprov2-optimizer/SKILL.md
This skill should be used when the user asks to "optimize a DSPy program", "use MIPROv2", "tune instructions and demos", "get best DSPy performance", "run Bayesian optimization", mentions "state-of-the-art DSPy optimizer", "joint instruction tuning", or needs maximum performance from a DSPy program with substantial training data (200+ examples).
npx skillsauth add omidzamani/dspy-skills dspy-miprov2-optimizerInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Jointly optimize instructions and few-shot demonstrations using Bayesian Optimization for maximum performance.
| Input | Type | Description |
|-------|------|-------------|
| program | dspy.Module | Program to optimize |
| trainset | list[dspy.Example] | 200+ training examples |
| metric | callable | Evaluation function |
| auto | str | "light", "medium", or "heavy" |
| num_trials | int | Optimization trials (40+) |
| Output | Type | Description |
|--------|------|-------------|
| compiled_program | dspy.Module | Fully optimized program |
Install the optional Optuna dependency before using MIPROv2:
pip install -U "dspy[optuna]>=3.2.1,<3.3"
import dspy
from dspy.teleprompt import MIPROv2
lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
class RAGAgent(dspy.Module):
def __init__(self):
self.retrieve = dspy.Retrieve(k=3)
self.generate = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
context = self.retrieve(question).passages
return self.generate(context=context, question=question)
from dspy.teleprompt import MIPROv2
optimizer = MIPROv2(
metric=dspy.evaluate.answer_exact_match,
auto="medium", # Balanced optimization
num_threads=24
)
compiled = optimizer.compile(RAGAgent(), trainset=trainset)
| Preset | Trials | Use Case |
|--------|--------|----------|
| "light" | ~10 | Quick iteration |
| "medium" | ~40 | Production optimization |
| "heavy" | ~100+ | Maximum performance |
import dspy
from dspy.teleprompt import MIPROv2
from dspy.evaluate import Evaluate
import json
import logging
logger = logging.getLogger(__name__)
class ReActAgent(dspy.Module):
def __init__(self, tools):
self.react = dspy.ReAct("question -> answer", tools=tools)
def forward(self, question):
return self.react(question=question)
def search_tool(query: str) -> list[str]:
"""Search knowledge base."""
results = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query, k=3)
return [r['long_text'] for r in results]
def optimize_agent(trainset, devset):
"""Full MIPROv2 optimization pipeline."""
agent = ReActAgent(tools=[search_tool])
# Baseline evaluation
evaluator = Evaluate(
devset=devset,
metric=dspy.evaluate.answer_exact_match,
num_threads=8
)
baseline = evaluator(agent)
logger.info(f"Baseline: {baseline:.2%}")
# MIPROv2 optimization
optimizer = MIPROv2(
metric=dspy.evaluate.answer_exact_match,
auto="medium",
num_threads=24,
# Custom settings
num_candidates=15,
max_bootstrapped_demos=4,
max_labeled_demos=8
)
compiled = optimizer.compile(agent, trainset=trainset)
optimized = evaluator(compiled)
logger.info(f"Optimized: {optimized:.2%}")
# Save with metadata
compiled.save("agent_mipro.json")
metadata = {
"baseline_score": baseline,
"optimized_score": optimized,
"improvement": optimized - baseline,
"num_train": len(trainset),
"num_dev": len(devset)
}
with open("optimization_metadata.json", "w") as f:
json.dump(metadata, f, indent=2)
return compiled, metadata
from dspy.teleprompt import MIPROv2
# Disable demos for pure instruction optimization
optimizer = MIPROv2(
metric=metric,
auto="medium",
max_bootstrapped_demos=0,
max_labeled_demos=0
)
num_threads=24 or higher if availabletools
This skill should be used when the user asks to "optimize with SIMBA", "use mini-batch introspective optimization", "generate self-reflective rules", mentions "SIMBA optimizer", "stochastic mini-batch ascent", "output variability", or needs an alternative to MIPROv2/GEPA that evolves rules and demonstrations from numeric metrics.
data-ai
This skill should be used when the user asks to "create a DSPy signature", "define inputs and outputs", "design a signature", "use InputField or OutputField", "add type hints to DSPy", mentions "signature class", "type-safe DSPy", "Pydantic models in DSPy", or needs to define what a DSPy module should do with structured inputs and outputs.
development
This skill should be used when the user asks to "use DSPy RLM", "process a very long context", "use ProgramOfThought", "use CodeAct", "run DSPy modules in parallel", mentions Recursive Language Models, sandboxed Python execution, Deno, `dspy.RLM`, `dspy.ProgramOfThought`, `dspy.CodeAct`, or `dspy.Parallel`, or needs to choose a DSPy reasoning module beyond Predict, ChainOfThought, and ReAct.
tools
This skill should be used when the user asks to "create a ReAct agent", "build an agent with tools", "implement tool-calling agent", "use dspy.ReAct", mentions "agent with tools", "reasoning and acting", "multi-step agent", "agent optimization with GEPA", or needs to build production agents that use tools to solve complex tasks.