Sub topic 3
Teaching Edge Cases and Out-of-Scope Inputs
Assume you understand prompts, chat roles, and basic Python. This chapter extends a few-shot prompt with boundary examples, selection logic, and evaluation.
Teach the Boundaries
Examples should show more than ordinary cases. Include an ambiguous input, an unusual valid input, and an out-of-scope input. Demonstrate the exact response expected for each. This teaches the model when to classify, when to ask for clarification, and when to refuse or return a fallback.
Classify each review as positive, negative, or neutral. If it is not a product review, output out_of_scope.
Review: The battery lasts all day.
Sentiment: positive
Review: The product arrived on Tuesday.
Sentiment: neutral
Review: What is the weather today?
Sentiment: out_of_scope
Review: The screen is bright, but I cannot decide if the speakers are good.
Sentiment:A boundary example is useful only if its label and wording match the real policy. Do not include contradictory examples.
Show Reasoning Carefully
Reasoning examples can show a short decision trace:
Review: The case looks good, but it broke immediately.
Signals: positive appearance; negative durability.
Sentiment: negativeUse a fixed, concise format. If users only need a label, a hidden or compact rationale may reduce output errors and token use. Do not treat a plausible explanation as proof that the model reasoned correctly.
Select Examples Dynamically
A fixed set works when inputs are similar. For varied inputs, retrieve examples from a larger pool using similarity, then add coverage rules for labels and edge cases.
def select_examples(pool, query, k=3):
words = set(query.lower().split())
ranked = sorted(
pool,
key=lambda item: len(words & set(item[0].lower().split())),
reverse=True,
)
return ranked[:k]Keep at least one boundary example when the query might be out of scope.
Test on Held-Out Data
Keep evaluation items separate from the examples shown to the model. Compare accuracy, invalid labels, and out-of-scope handling across prompt versions.
Pitfalls
- Copying: The model may repeat an example instead of solving the query.
- Label bias: More examples for one label can skew predictions.
- Leakage: Evaluation answers or near-duplicates in the prompt make results misleading.
- Token cost: More examples increase latency and price. Remove examples that add no coverage.
> callout warning > Test on inputs from the real distribution, including rare and invalid cases. A high average score can hide unsafe boundary behavior. > ```
Choose Another Method When Needed
Use instructions alone for simple, stable tasks. Use few-shot prompting when examples clarify format or ambiguous policy. Use dynamic selection when the input range is broad. Consider fine-tuning when the task is repeated at high volume and stable behavior justifies training and maintenance costs.
Exercise
Why should held-out evaluation data never be used as few-shot examples?
Show answer
It would reveal the answers or close duplicates, producing an inflated score that does not measure generalization.