Overview

5 Optimizing prompt examples

The chapter explains prompt optimization in DSPy as a data-driven process similar to machine learning training: generate many candidate prompts, test them on validation data, and keep the one that best improves the target metric. It focuses first on optimizing the demonstrations in a prompt rather than the instructions, using an intent classification task to show how labeled examples can make vague classes easier for a language model to distinguish. The key idea is that the best prompt is not found by exhaustive search, since the number of possible demo combinations is enormous, so DSPy provides practical optimizers and evaluation tools to search efficiently.

It then walks through several ways to choose demonstrations. LabeledFewShot simply samples labeled training examples at random and inserts them into the prompt, which is easy to use but not especially smart; repeated runs and different values of k can improve results. BootstrapFewShot adds a verification step using a metric function, accepting only examples whose LM predictions are good enough, which is especially useful when demonstrations need to include generated fields such as reasoning. The chapter also shows how teacher models can help during bootstrapping, and how BootstrapFewShotWithRandomSearch extends the idea by trying multiple candidate prompt sets and selecting the best-performing one on validation data.

Finally, the chapter introduces KNN optimization, which changes the process from selecting one fixed demo set to choosing the most similar training examples for each individual LM call using embeddings and vector similarity. This can reduce prompt size while keeping demonstrations relevant to each input, though it depends on the quality of the training set and embedder. Across the experiments, every optimizer improves over the unoptimized baseline, with random-search variants and well-chosen labeled demos often performing best, while KNN remains competitive and efficient. The main takeaway is that prompt examples matter a great deal, and DSPy makes it much easier to search for and evaluate strong demo sets than manual prompt engineering alone.

The three main stages of prompt programming. Here and in the next chapter, we'll cover the third of these, optimization.
Prompt optimization. The input to the optimizer is a set of Examples with their labels. During optimization, the optimizer selects a subset of the Examples to be the prompt demonstrations.
KNN optimizer algorithm. This first searches for two similar Examples and then adds them to the prompt. Examples 1 and 2 have high similarity scores and are chosen as prompt demos, while Examples 3 and 4 have low similarity and are left out of the prompt.

Summary

  • DSPy supports two forms of prompt optimization: optimizing the set of demonstrations included in a prompt and optimizing the instructions.
  • In most cases, optimizing the demos is more relevant than optimizing the instructions.
  • Picking good demos for a prompt manually is difficult and can be unintuitive.
  • Optimizing consists of generating and evaluating many prompts and evaluating.
  • When optimizing, it can be more efficient to include early stopping.
  • DSPy provides LabeledFewShot, BootstrapFewShot, BootstrapFewShotWithRandomSearch, and KNN optimizers to optimize the demonstrations.
  • The LabeledFewShot optimizer selects a set of examples randomly from a provided training set.
  • The BootstrapFewShot optimizer can work the same or can be used to enable DSPy’s bootstrapping process.
  • Using the bootstrapping functionality allows us to, among other things, include values of all output fields in the demonstrations, even if some of these are missing from the Examples in the training data.
  • BootstrapFewShotWithRandomSearch will try different sets of demonstrations and select the best of these.
  • KNN will choose prompt demos based on their similarity to the input, so different demos may be chosen for each LM call.
  • Optimizing can sometimes allow us to achieve similar scores with smaller LMs as we can with more powerful LMs.

FAQ

What is DSPy optimization trying to do in prompt programming?DSPy optimization seeks to automatically find a prompt that maximizes the chosen metric on validation data, similar to how machine learning selects the best model based on validation performance.
What are the two main types of prompt optimization covered in this chapter?The chapter focuses on optimizing the demonstrations in a prompt. It also mentions optimizing instructions, which is covered in the next chapter.
What is the difference between “demonstrations” and “Examples” in DSPy?“Demonstrations” are the input-output pairs included in a prompt, while “Examples” are the dataset items stored in code. Demos are selected from the Examples.
Why are demonstrations useful in a prompt?Demonstrations help the language model learn patterns, distinguish between labels, and handle ambiguous or edge-case inputs more accurately than instructions alone.
What does the LabeledFewShot optimizer do?LabeledFewShot randomly selects a fixed number of labeled training Examples and inserts them into the prompt as demonstrations.
How does BootstrapFewShot differ from LabeledFewShot?BootstrapFewShot uses a metric function to verify whether candidate Examples are good enough to serve as demos. It can use bootstrapped demos, labeled demos, or both, and it rejects examples that fail the metric.
What is bootstrapping in DSPy?Bootstrapping is the process of using the LM to generate missing output fields for demonstrations and then keeping only the examples that pass the metric check. In multi-step modules, it can also generate intermediate traces.
Why is BootstrapFewShotWithRandomSearch useful?It evaluates multiple candidate sets of demonstrations and selects the best-performing one on a validation set, making it more systematic than choosing demos at random.
How does the KNN optimizer choose prompt demonstrations?KNN dynamically selects the k most similar training Examples for each input by comparing embeddings, so the demos can change from one LM call to another.
Which optimizer performed best in the chapter’s evaluation results?Performance varied by model and configuration, but optimized prompts generally beat the baseline. In the reported results, LabeledFewShot and BootstrapFewShotWithRandomSearch often achieved the strongest scores, while KNN was also competitive with fewer prompt tokens.

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