Sub topic 6
Choosing Representative and Diverse Examples
This chapter assumes you can read short Python, know what a prompt and a completion are, and understand system, user, and assistant roles in chat prompts. It also assumes you have seen a basic few-shot prompt: an instruction, a few input and output pairs, and an unfinished final entry.
Why selection matters more than quantity
A model reads your examples as evidence about the task. It infers the label set, the output format, the length and tone of answers, and which inputs count as "normal." If the examples are narrow, the model infers a narrow task. If they are skewed, it infers a skewed one.
Two prompts can contain the same number of examples and behave very differently. One uses three near-identical reviews about batteries. The other uses a review about battery life, a review about shipping, and a mixed review. The second teaches a wider task, so it usually holds up better on inputs you did not anticipate.
This chapter covers two properties:
- Representative: the examples look like the inputs the prompt will actually receive.
- Diverse: the examples differ from each other in the ways that matter, so together they cover the range of inputs.
Representative: match the real input distribution
An example set is representative when its inputs resemble production inputs in length, vocabulary, noise, and difficulty. Many bad few-shot prompts use clean, short, textbook examples, and then run on messy, long, ambiguous real data.
Use these checks:
- Source examples from real data. Pull them from logs, tickets, or reviews you have collected, not from sentences you invent. Invented examples tend to be cleaner and more obvious than real ones.
- Match length. If real inputs run to several paragraphs, a set of one-line examples teaches the model to expect one-liners. Outputs may become shorter or more superficial.
- Match style. If real inputs contain typos, slang, emoji, or mixed languages, include some examples with those features.
- Match difficulty. If a third of real inputs are ambiguous, a set of only obvious cases hides that the task is hard.
The sentiment prompt from the earlier chapters has three tidy examples. Real reviews are often mixed, like "The screen is bright but the speakers are weak." A set with no mixed example gives the model no guidance on that case. It will guess, and the guess may differ from run to run.
Diverse: cover the dimensions that matter
Diversity does not mean random variety. It means variety along the dimensions that affect the correct answer or the output format. To find those dimensions, list how inputs can differ for your task.
For sentiment classification, useful dimensions include:
- the label (positive, negative, neutral)
- the topic (product quality, shipping, price, support)
- the length (one line versus a paragraph)
- the signal strength (explicit praise versus subtle disappointment)
- special structure (mixed opinions, sarcasm, questions)
You will not cover every combination. The goal is that each important value of each dimension appears at least once, and that no single value dominates.
A coverage table for a small sentiment set might look like this:
| Example | Label | Topic | Length | Special feature |
|---|---|---|---|---|
| 1 | positive | battery | short | none |
| 2 | negative | reliability | short | none |
| 3 | neutral | shipping | short | factual statement |
| 4 | negative | audio | long | mixed opinion, leans negative |
| 5 | positive | support | long | sarcasm-free but informal |
The table shows that every label appears, three topics beyond the first are covered, and both lengths are present. It also makes the remaining gaps visible, such as sarcasm and non-English text.
Avoiding near-duplicates
Two examples that teach the same thing waste tokens and can bias the model. If four of five examples are short, positive, and about batteries, the model may associate "battery" with positive or "short" with positive. That is a spurious pattern: a surface feature that happens to correlate with the label in your set but not in reality.
A quick way to find near-duplicates is to measure similarity among your candidates. The following code reuses the word-overlap idea from the dynamic selection example, and flags pairs of examples that are too alike.
def overlap(a, b):
wa, wb = set(a.lower().split()), set(b.lower().split())
union = wa | wb
return len(wa & wb) / len(union) if union else 0.0
def find_near_duplicates(pool, threshold=0.5):
"""Return index pairs whose inputs overlap above the threshold."""
pairs = []
for i in range(len(pool)):
for j in range(i + 1, len(pool)):
score = overlap(pool[i][0], pool[j][0])
if score >= threshold:
pairs.append((i, j, round(score, 2)))
return pairs
pool = [
("The battery lasts all day and charging is fast.", "positive"),
("The battery lasts two days and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
print(find_near_duplicates(pool))Running this prints [(0, 1, 0.7)]. Examples 0 and 1 share most of their words and teach the same lesson, so you would drop one and use the slot for something different.
Word overlap is crude. Two sentences can mean the same thing with no shared words, and two sentences can share many words and mean opposite things. For a first pass it is fast and needs no extra libraries. For a more reliable check, compare embeddings instead. An embedding is a list of numbers produced by a model so that texts with similar meaning get similar numbers.
A greedy method for building a diverse set
When you have a large labeled pool and need a small set, you can pick examples greedily. Start with one, then repeatedly add the candidate that is least similar to those already chosen. This is a simple version of a standard approach called max-min selection.
def overlap(a, b):
wa, wb = set(a.lower().split()), set(b.lower().split())
union = wa | wb
return len(wa & wb) / len(union) if union else 0.0
def pick_diverse(pool, k=4):
"""Greedily choose k examples that are dissimilar to each other."""
if not pool:
return []
chosen = [pool[0]]
remaining = list(pool[1:])
while remaining and len(chosen) < k:
def closeness(candidate):
# Highest similarity to any already chosen example.
return max(overlap(candidate[0], c[0]) for c in chosen)
best = min(remaining, key=closeness)
chosen.append(best)
remaining.remove(best)
return chosen
pool = [
("The battery lasts all day and charging is fast.", "positive"),
("The battery lasts two days and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
("The screen is bright but the speakers are weak.", "negative"),
]
for text, label in pick_diverse(pool, k=4):
print(label, "|", text)The method picks the first item, then the items least like it, so the near-duplicate battery review is the one left out. It has a limit: it maximizes textual difference, not label balance or task coverage. A pool full of unusual but irrelevant text will produce a diverse but poor set. Treat the output as a candidate list and review it against your coverage table.
Include the hard cases, but not only the hard cases
Hard inputs are the ones where a plain instruction fails. Examples are the best tool for those cases because they show the intended decision without long explanation. Find them by running the instruction alone (zero-shot) on a sample of real data, collecting the errors, and turning representative errors into examples.
Balance matters here. If every example is an unusual edge case, the model may treat the unusual as normal and over-apply special handling. A reasonable mix is mostly typical cases that establish the baseline, plus a small number of edge cases that clarify boundaries. Later chapters cover edge cases and out-of-scope inputs in more detail.
Keep a pool, then choose a set
It helps to separate two collections:
- The pool is a larger set of labeled examples you have gathered and reviewed. It can hold dozens or hundreds of items.
- The prompt set is the few examples you actually place in the prompt.
You choose the prompt set from the pool, either by hand using your coverage table, with a method like pick_diverse, or per query, which a later chapter covers. Keeping the pool means you can swap examples when you find a failure, without starting over.
Keep some labeled items out of the pool entirely. Those become your held-out test data. An example that appears in the prompt cannot fairly be used to measure the prompt, because the model can copy its answer.
Common mistakes
- Choosing the easiest examples. The set looks clean and the model passes your quick checks, then fails on real data.
- Letting one label dominate. The model learns to favor that label. Label balance is covered in a later chapter.
- Using one topic or phrasing throughout. The model ties the behavior to the topic or phrasing instead of the task.
- Mislabeled examples. An incorrect label teaches a wrong rule. Have a second person check labels, especially for ambiguous items.
- Inconsistent labeling logic. If one mixed review is labeled negative and a similar one neutral, the model cannot learn a rule. Decide the rule first, such as "mixed reviews that lean negative are negative," and apply it everywhere.
- Never revisiting the set. Inputs drift over time. Review the examples whenever failure reports change in character.
A short selection workflow
- Collect a pool of real inputs and label them with a written rule.
- Split off a held-out test set and do not use it as examples.
- List the dimensions that affect the answer, and build a coverage table.
- Choose three to six candidates from the pool that cover the dimensions, removing near-duplicates.
- Add one or two hard cases found from zero-shot errors.
- Run the prompt on the test set, inspect errors, and adjust the examples.
Exercise
A team builds a ticket-routing prompt with five examples. All five are short, polite, English-language tickets about password resets, each routed to "account." In production the prompt routes long, angry billing tickets to "account" too. Name the two selection problems and describe a fix.
Show answer
The set is not representative (it covers only short, polite tickets, while real tickets are long and emotional) and not diverse (one topic, one label, one style). The model likely learned that nearly every ticket goes to "account," and tied the label to the topic and tone. The fix is to pull real tickets from the logs, list the dimensions (route label, topic, length, tone, language), and choose examples covering each route, including long and angry tickets and at least one billing case. Check the new set on held-out tickets that are not used as examples.
Exercise
Using the overlap function, would find_near_duplicates with threshold 0.5 flag "It stopped working after a week." and "It stopped working after two weeks."? Explain why this might still be a problem even if the label is the same.
Show answer
Yes. The first has 6 words and the second has 6, sharing 4 (it, stopped, working, after) plus "a" is not shared, "week." versus "weeks." differ, so the intersection is 4 and the union is 8, giving 0.5, which meets the threshold. Keeping both wastes a slot and tokens, and it reinforces one narrow pattern. Replace one with an input that differs along a meaningful dimension, such as a long complaint or a mixed review.