Sub topic 1
Zero-Shot Prompts and Where They Fall Short
Prerequisites
This chapter assumes you know three things:
- What a large language model (LLM) is, and that a prompt produces a completion.
- How to read short Python code.
- What the system, user, and assistant roles mean in a chat-style prompt.
You don't need to have called a model API. The code here runs without one.
What zero-shot prompting means
A zero-shot prompt describes a task and gives no worked examples of it. The model sees an instruction and an input, and it has to work out both what you want and what the answer should look like. The "zero" is the number of demonstrations in the prompt.
Here is a zero-shot prompt for sentiment classification:
Classify the sentiment of the following review as positive, negative, or neutral.
Review: The screen is bright but the speakers are weak.
Sentiment:Modern instruction-tuned models handle prompts like this well. They were trained on large amounts of text and then tuned to follow instructions, so common tasks such as summarizing, translating, and simple classification often work with no examples.
Zero-shot is the right starting point for any prompt. It is cheap, short, and quick to change. It also gives you a baseline to compare against, so you can tell later whether examples actually helped.
A worked zero-shot example in code
In a chat API, a zero-shot prompt is a system message plus one user message. The helper below builds that message list. A second helper checks whether a reply is exactly one of the allowed labels.
SYSTEM = (
"Classify the sentiment of each review as positive, negative, or neutral. "
"Answer with one word."
)
ALLOWED = {"positive", "negative", "neutral"}
def build_zero_shot(query):
return [
{"role": "system", "content": SYSTEM},
{"role": "user", "content": query},
]
def is_valid_label(reply):
return reply.strip().lower() in ALLOWED
messages = build_zero_shot("The screen is bright but the speakers are weak.")
print(messages[-1]["content"])
sample_replies = [
"positive",
"Negative.",
"The sentiment is mixed, leaning negative.",
"neutral\n",
]
for reply in sample_replies:
print(repr(reply), "->", is_valid_label(reply))Running it prints:
The screen is bright but the speakers are weak.
'positive' -> True
'Negative.' -> False
'The sentiment is mixed, leaning negative.' -> False
'neutral\n' -> TrueThe instruction said "Answer with one word", yet two of the four sample replies would break a program that expects a bare label. A trailing period fails a strict comparison. A full sentence fails it completely. Models do produce replies like these, and a pipeline that parses the output has to cope with them.
Where zero-shot falls short
Zero-shot prompts fail in a small number of recurring ways. Knowing them helps you decide when to add examples.
| Failure | What you see | Why it happens |
|---|---|---|
| Format drift | Extra words, punctuation, or markdown around the answer | The instruction names a format, but the model has no concrete pattern to copy |
| Ambiguous labels | Inconsistent answers on mixed or borderline inputs | Words like "neutral" mean different things to different people |
| Unstated conventions | Answers are plausible but don't match your style or house rules | Your conventions are not in the prompt, so the model guesses |
| Unstable boundaries | The same kind of input gets different labels across runs | The model has no anchor for where one category ends |
| Unclear scope | The model answers questions it should decline | The prompt never shows what out-of-scope input looks like |
Format drift
Describing a format in words is weaker than showing it. "Answer with one word" is a request, and the model weighs it against its habit of being conversational. The result is replies like "Sentiment: Positive" or "I'd say negative." On a single manual test this looks fine. Across thousands of inputs the failures add up.
Ambiguous labels
Take the review "The screen is bright but the speakers are weak." Is it neutral because the praise and complaint cancel? Is it negative because the complaint is the last thing said? A reasonable reader could choose either. Without guidance the model picks by its own habits, and those habits may differ from your annotation policy.
The problem is the task definition, not the model's ability. If you can't state a rule that settles the case, a longer instruction rarely helps. A labeled example settles it more reliably, because it shows the decision and doesn't have to describe the rule.
Unstated conventions
Many tasks carry rules that people absorb without writing them down:
- Product names are never translated.
- Dates are written as YYYY-MM-DD.
- A support ticket mentioning a refund is always tagged "billing", even if it also mentions shipping.
- Summaries start with a verb and stay under 15 words.
You can write each rule into the instruction, but rules pile up and some are hard to put into words. Examples carry many of them at once without you listing them.
Unstable boundaries
Even when the model answers in the right format, its answers on borderline inputs can shift between runs, between model versions, or after a small rewording of the instruction. A prompt that scored well last month can change behavior after a model update. Examples that sit on the boundary hold those cases in place.
Unclear scope
A prompt for a billing-support classifier will usually still produce a billing category when a user writes "What's the weather?" The model is trying to be helpful and has no sign that refusing or returning "other" is the expected behavior.
A diagram of the decision
The diagram shows how to treat a zero-shot result. Fix the instruction first. Add examples when a failure comes from ambiguity or format.
Measure before you decide
"It felt unreliable" is not a good reason to change a prompt. Collect a small set of labeled inputs, run the zero-shot prompt over all of them, and count. A set of 30 to 50 varied inputs is enough to see large problems. Include the awkward cases you have actually met, not only clean ones.
This function scores any prompt-driven prediction function against labeled data. It is the same scoring approach you will use later to compare few-shot variants:
def accuracy(predict, labeled):
correct = 0
for text, label in labeled:
if predict(text).strip().lower() == label:
correct += 1
return correct / len(labeled)Two points about this check:
- The exact comparison treats a reply like "negative." as wrong. That is deliberate if your downstream code does an exact match. If your code can clean up replies, add that cleaning step and score after it, so you measure what your pipeline sees.
- Record format failures separately from wrong-label failures. They have different fixes. A format failure often yields to a stricter instruction or a parsing step. A wrong label usually means the task definition is unclear.
Keep the labeled set separate from any examples you later put in a prompt. If an example also appears in the test set, the score is inflated. This matters more once you add examples, and a later chapter covers it in detail.
Tighten the instruction first
Before adding examples, try cheaper fixes. Each one costs only a few tokens:
- State the allowed outputs explicitly: "Reply with exactly one of: positive, negative, neutral."
- Define the hard label: "Use neutral for reviews with no opinion or with equal praise and complaint."
- Say what to leave out: "Do not add punctuation or explanation."
- Put the instruction in the system message and keep the user message to the input only.
- Where the API supports it, use structured output or a constrained response format so the reply must match a schema.
Option 5 is worth noting. If the only failure is format, constrained output removes it without examples. Examples can't fix a label that is wrong on borderline inputs, though, and a schema can't either.
When zero-shot is enough
Stay with zero-shot when all of these hold:
- The task is common and its meaning is clear, such as translating a sentence or summarizing an article.
- Your measured accuracy meets your target on a realistic test set.
- Output format is either loose or enforced by a schema.
- You can't afford the extra tokens, or the added latency, of a longer prompt on every call.
Switching to few-shot is not an upgrade by default. Examples add cost, can bias the model toward the labels they show, and need upkeep. The case for them comes from a measured gap.
Why this leads to few-shot prompting
The failures above share one cause. The prompt says what to do, and the model has to infer the rest. When the missing information is a pattern, such as a format, a style, a boundary, or a convention, a demonstration conveys it better than a description does. That is the idea behind few-shot prompting, which the next chapter introduces: place a few input and output pairs in the prompt so the model can see the task.
Exercise
A zero-shot classifier is told "Answer with one word". On 40 labeled reviews, 34 replies are the correct label, 3 are the correct label with a trailing period, and 3 are the wrong label. Your code does an exact string match. What accuracy does the exact-match check report, and what two different problems does the result combine?
Show answer
The exact-match check reports 34/40 = 85%. The 3 replies with a trailing period count as wrong, so they are format failures even though the label is correct. The other 3 are real label errors. The two problems have different fixes: the format failures can be fixed by a stricter instruction, a constrained output format, or cleaning the reply before comparing (which would give 37/40 = 92.5%). The label errors point to an unclear task definition, which is where examples can help.
Exercise
Name two situations where you would stay with a zero-shot prompt even though it sometimes makes mistakes.
Show answer
Stay with zero-shot when measured accuracy already meets your target on a realistic test set. Also stay when the only failure is output format and a schema or constrained response format fixes it. Another valid answer is when token cost or latency on every call matters more than the small accuracy gain examples would bring.