Sub topic 9
Teaching Edge Cases and Out-of-Scope Inputs
This chapter assumes you can read short Python, know chat roles (system, user, assistant), and have built a few-shot prompt with consistent examples. Here you will add examples for inputs that sit at the edge of the task, or outside it.
Why typical examples are not enough
Examples drawn from the common case teach the model the common case. Real inputs also include:
- Ambiguous items, such as a review with both praise and complaints.
- Empty or garbled input.
- Input in the wrong language or on the wrong topic.
- Requests the system should decline.
If none of these appear in your examples, the model improvises. It may force every input into one of your labels, or it may write an explanation where your pipeline expects one word.
Edge cases that stay in scope
An edge case is a valid input where the right answer is not obvious. Show the answer you want, and keep the format identical to the other examples.
Take the sentiment task. The review "The screen is bright but the speakers are weak" is mixed. If you want mixed reviews labeled neutral, include one example that says so. Without it, the model may pick positive or negative unpredictably.
Choose edge cases from real failures. Run the prompt on sample data, collect the wrong answers, and add one example for each kind of mistake. Do not add one example per rare oddity, because each example costs tokens and shifts the model's attention.
Out-of-scope inputs
An out-of-scope input is one the task does not apply to. A shipping notice, a question, or a blank string is not a review with a sentiment. Define an explicit fallback label and show it.
SYSTEM = (
"Classify the sentiment of each product review as positive, negative, "
"or neutral. If the text is not a product review, answer: not_a_review. "
"Answer with one word."
)
examples = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The screen is bright but the speakers are weak.", "neutral"),
("What time does your store open?", "not_a_review"),
("", "not_a_review"),
]
def build_messages(examples, query):
messages = [{"role": "system", "content": SYSTEM}]
for text, label in examples:
messages.append({"role": "user", "content": text or "(empty)"})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": query or "(empty)"})
return messages
for message in build_messages(examples, "Where is my order?"):
print(message["role"], "->", message["content"])Two details matter here. The fallback label is named in the system message and also demonstrated, so the instruction and the examples agree. The empty string is replaced with a visible placeholder, because many chat APIs reject empty message content.
Refusals follow the same pattern
When the system must decline a request, show a refusal in the exact style you want. A short, fixed reply is easier to detect in code than a free-form apology.
Balancing the edge cases
Edge-case examples compete with typical ones. If three of your six examples are not_a_review, the model will lean toward that label. As a rule of thumb, keep edge cases a minority of the examples. Check the effect with the held-out accuracy function from the evaluation chapter, and include some out-of-scope items in your test set so you measure what you taught.
Common errors
| Error | Result | Fix |
|---|---|---|
| Fallback label only in the examples, not the instruction | Model sometimes invents other labels | Name it in the system message too |
| Edge examples in a different format | Model copies the odd format | Keep the same layout for every example |
| Too many refusal examples | Valid inputs get refused | Keep refusals a small share |
| No out-of-scope items in the test set | Accuracy looks high but the fallback is untested | Add them to held-out data |
Exercise
Your classifier labels the input "Thanks for the quick reply!" as positive, but it is a message to support staff, not a product review. Which two changes in the prompt would you make, and where would you check the result?
Show answer
Add an instruction naming a fallback label such as notareview, and add one example of a non-review message with that label, in the same format as the other examples. Check the result on held-out data that includes non-review messages, comparing accuracy before and after the change.