Sub topic 8
Order and Label Balance
This chapter assumes you can read short Python and know how chat roles (system, user, assistant) work. You should also know how to put input and output pairs into a prompt. Here you will see how the order of your examples and the mix of labels can push a model toward certain answers, and how to control both.
Why order and balance matter
A model reads your examples as a sequence. It does not weigh them equally. Two effects show up often:
- Label bias. If most examples carry one label, the model tends to predict that label more often, even when the query points elsewhere.
- Recency bias. The examples closest to the query often have the most influence. If the last example is "negative", the model may lean "negative" on an ambiguous query.
Both effects get stronger when you use few examples or when the query is ambiguous. Neither is a bug you can fix once. You reduce them by design, then check the result by measuring.
Balancing labels
Count the labels in your examples. Aim for an even spread across the classes you expect, unless your real data is skewed and you want the prompt to reflect that.
Here is a skewed set for the sentiment task from earlier chapters:
| Example | Label |
|---|---|
| The battery lasts all day. | positive |
| Fast shipping and great fit. | positive |
| Love the screen. | positive |
| It stopped working after a week. | negative |
Three of four examples are positive. A mixed review such as "The screen is bright but the speakers are weak" may now be pushed toward "positive". A balanced set uses the same number of examples per label, such as two positive, two negative, and two neutral.
Shuffling the order
Grouping examples by label is a common mistake. A block of "positive" examples followed by a block of "negative" examples ends on a run of one label, which strengthens recency bias. Interleave the labels instead.
The function below balances labels by taking examples round-robin from each label group, then shuffles the result with a fixed seed so the order is reproducible:
import random
from collections import defaultdict
def balanced_order(examples, per_label=2, seed=0):
rng = random.Random(seed)
groups = defaultdict(list)
for text, label in examples:
groups[label].append((text, label))
chosen = []
for label, items in groups.items():
rng.shuffle(items)
chosen.extend(items[:per_label])
rng.shuffle(chosen)
return chosen
pool = [
("The battery lasts all day.", "positive"),
("Fast shipping and great fit.", "positive"),
("Love the screen.", "positive"),
("It stopped working after a week.", "negative"),
("The strap broke on day two.", "negative"),
("The package arrived on Tuesday.", "neutral"),
("It comes in a white box.", "neutral"),
]
for text, label in balanced_order(pool, per_label=2, seed=1):
print(label, "|", text)The fixed seed matters. If the order changes on every run, you cannot tell whether a score change came from your edit or from a different shuffle.
Treating order as a variable
No single order is correct. Different orders can give different accuracy on the same data, and the gap can be large with small models or few examples. Test several orders and look at the spread.
def accuracy(predict, labeled):
correct = 0
for text, label in labeled:
if predict(text).strip().lower() == label:
correct += 1
return correct / len(labeled)
def order_sensitivity(make_predict, pool, held_out, seeds=(0, 1, 2, 3, 4)):
scores = []
for seed in seeds:
examples = balanced_order(pool, per_label=2, seed=seed)
scores.append(accuracy(make_predict(examples), held_out))
return min(scores), max(scores)Here make_predict takes a list of examples and returns a function that sends the prompt to your model. The held-out data must not overlap with the pool. If the minimum and maximum scores are far apart, your prompt is fragile. Add examples, tighten the instruction, or pick the order that scores best and keep it fixed.
When balance is not the goal
Match the label mix to your real data when it matters. If 90 percent of incoming reviews are neutral, a perfectly even prompt may over-predict the rare classes. Measure on held-out data with a realistic mix, and adjust until errors are acceptable.
Exercise
Your prompt has six examples: four labeled "negative" and two labeled "positive", with all negatives listed first. Name two problems this order creates, and describe one change that fixes both.
Show answer
The skewed mix invites label bias toward "negative", and ending on a block of positives or a run of one label invites recency bias. Rebalance to equal counts per label (for example three and three) and interleave them with a fixed-seed shuffle, then check accuracy on held-out data.