Overview

6 Optimizing prompt instructions

This chapter introduces how DSPy optimizes prompt instructions, complementing the earlier focus on demonstrations. It explains that several optimizers can be used for this purpose, including COPRO, MIPROv2, InferRules, SIMBA, GEPA, and Ensemble, and emphasizes that no single method is universally best. Instead, the right choice depends on the task, the model, and the amount of compute you can spend, so it is often worthwhile to try multiple optimizers and compare results.

COPRO serves as the simplest starting point, using a hill-climbing style search that repeatedly generates instruction variants, evaluates them on validation data, and keeps the best one. The chapter then shows how MIPROv2 expands this idea by optimizing instructions and demonstrations together, using grounding information about the program and dataset to produce better candidate prompts, and then using Bayesian optimization to efficiently search combinations. InferRules takes a more rule-driven approach by extracting concise natural-language rules from examples and inserting them into the prompt, while SIMBA and GEPA add more reflective, iterative strategies that use feedback, history, and in GEPA’s case a genetic-algorithm style Pareto-front search to evolve stronger prompts.

The chapter also highlights practical concerns such as cost, because these optimizers can make many LM calls, especially with larger search settings. It explains how prompt and task models can be separated so a stronger model can be used during optimization and a cheaper one in production, how optimized programs can be chained together or combined through ensembling, and how final programs can be saved and reloaded. Overall, the main message is that instruction optimization in DSPy is a flexible search problem: better prompts usually come from guided iteration, grounded candidate generation, and careful evaluation rather than from a single manually written instruction.

The COPRO algorithm. COPRO loops a specified number of iterations. Each iteration takes the current best prompt, creates several variations, and evaluates each one. It then determines the new best prompt and loops back. In the end, COPRO returns the best prompt discovered. This may be discovered in any iteration, but usually it’s one of the later iterations, as each iteration tends to produce stronger prompts.
The MIPROv2 algorithm Step 3. In Steps 1 and 2, we create the candidate sets of demos and the candidate instructions. In Step 3, we try various ways to combine these until we determine the one that performs the best. As there is often a large number of combinations, this uses a Bayesian Optimization method to determine which combinations to try. In this example, we try Set of demos #5 and Instructions #3, creating a prompt that is tested on all or part of the validation set with the task LM. The score is recorded, which allows us to track which sets of demos work well, which instructions work well, and which work well together. This helps us determine what other combinations to test in subsequent iterations.
The first 4 steps in generating and evaluating the candidate prompts in the GEPA optimizer. The nodes indicate a single prompt. Those in gray are in the Pareto Front and may be used to create subsequent candidate prompts. After Step 1, we have a single prompt, which is the prompt related to the DSPy program passed to the GEPA optimizer. In Step 2, we mutate this, creating Prompt 2, and evaluate that on the validation set. As long as this prompt is the top-performing prompt on at least one Example in the validation set, it becomes part of the Pareto Front. In Step 3 we can either mutate or combine prompts from the Pareto Front. In this case we combine Prompts 1 and 2, creating Prompt 3. This is the top-scoring on at least one Example, so it enters the Pareto Front, but Prompt 1 is now no longer part of the Pareto Front. In Step 4, we can again either mutate or combine elements of the Pareto Front. In this case, we mutate Prompt 3, creating Prompt 4, which also becomes part of the Pareto Front.

Summary

  • DSPy includes multiple optimizers to optimize the instructions.
  • Some optimizers (MIPROv2, InferRules, and SIMBA) can also select demonstrations to include in the prompts.
  • As well as including demonstrations and instructions, some optimizers (including InferRules and SIMBA) can generate rules summarizing the patterns in the training examples.
  • COPRO uses a method similar to Coordinate Ascent or Hill Climbing to iteratively improve upon a set of instructions.
  • MIPROv2 optimizes both instructions and demonstrations through Bayesian optimization
  • SIMBA iteratively improves a prompt, each step either adding a rule or a demonstration and potentially removing demonstrations.
  • GEPA uses a Genetic Algorithm to maintain a Pareto front to identify the strongest prompt.
  • Both SIMBA and GEPA use reflection, examining a history of previous prompts, their responses, and the scores, to generate improved prompts.
  • Both SIMBA and GEPA also support feedback from the metric function
  • Many programs can be combined using the Ensemble optimizer.
  • Many optimizers can be applied, one after the other, to a single program to create a highly optimized program.
  • SIMBA and GEPA are currently considered the strongest optimizers within DSPy, but the best performing for any given task depends on the task and LM.

FAQ

What is DSPy instruction optimization?DSPy instruction optimization is the process of improving the prompt instructions used by a DSPy program. Instead of manually writing the best wording, DSPy can generate, test, and refine candidate instructions to find ones that improve model performance on a task.
Which optimizers are covered in this chapter for optimizing prompt instructions?The chapter covers COPRO, MIPROv2, InferRules, SIMBA, GEPA, and Ensemble. Each optimizer improves prompts in a different way, so it is often useful to try more than one.
How does COPRO optimize prompt instructions?COPRO starts with the current instructions, generates several alternatives, evaluates them on a validation set, and keeps the best one. It repeats this process for multiple iterations, steadily improving the prompt through a hill-climbing or coordinate-ascent style search.
What are prompt instructions versus output field prefixes in DSPy?Prompt instructions are the main textual guidance in the system message that tells the LM how to solve the task. Output field prefixes are shorter hints attached to output fields; COPRO can optimize both, but the instructions usually matter more.
Why is it useful to provide a docstring in a DSPy signature?A docstring gives DSPy a better starting point than the default instruction text, which may be very generic. Even when using an optimizer, a good docstring helps seed the search with a stronger initial prompt.
How does MIPROv2 differ from COPRO?MIPROv2 optimizes both instructions and demonstrations together, while COPRO focuses mainly on instructions. MIPROv2 also uses grounding and Bayesian optimization to search efficiently among combinations of demo sets and candidate instructions.
What is grounding in MIPROv2?Grounding means giving the prompt LM extra task context, such as source code summaries, dataset summaries, and examples, so it can generate more relevant instructions. The goal is to make the candidate instructions better aligned with the actual task.
How does InferRules improve prompts?InferRules generates concise natural-language rules from the training examples and inserts them into the instructions. These rules summarize patterns in the data and can be more token-efficient than using many demonstrations.
What makes SIMBA different from other optimizers?SIMBA is reflective and iterative: it examines its own performance, then adds either a single rule or a single demonstration at each step. It can also use feedback from the metric function, which helps it learn why outputs were scored a certain way.
How does GEPA optimize prompts, and why use Ensemble?GEPA uses a genetic-algorithm style search with mutation, crossover, reflection, and a Pareto front of strong candidate prompts. Ensemble, on the other hand, combines multiple compiled programs at inference time, often using majority vote, to improve overall reliability.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building LLM Applications with DSPy ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building LLM Applications with DSPy ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Building LLM Applications with DSPy ebook for free