Models are trained to refuse certain categories of requests — content moderation, medical/legal advice, dangerous instructions. The refusal text follows a recognisable shape: "I can't help with that", "I'm not able to", "As an AI, I cannot...".
Your script should detect refusals so it can route around them — fall back to a safer prompt, escalate to a human, or skip the action.
def looks_like_refusal(text):
refusal_phrases = [
"i can't help",
"i'm not able to",
"i cannot",
"as an ai",
"i'm sorry",
]
text_lower = text.lower()
return any(p in text_lower for p in refusal_phrases)Heuristic? Not a structured field?
Some APIs return a structured refusal flag, but most don't. Heuristics on the response text are the universal fallback. The phrase list is calibration — start with a few common ones, expand as you see new shapes in production.
When the model declines, your code needs to know and act differently. Three patterns:
def looks_like_refusal(text):
refusal_phrases = ["i can't help", "i'm not able to", "i cannot", "as an ai", "i'm sorry"]
return any(p in text.lower() for p in refusal_phrases)
result = Agent(model).run_sync(prompt)
if looks_like_refusal(result.output):
print("refused")
# fallback: simpler prompt, or skip
else:
print("answered")
# use result.outputAsk the model to flag refusals explicitly:
prompt = '''If you can answer the question below, do so. If you cannot or will not, reply with exactly the token: REFUSE
Question: ...'''
if result.output.strip() == "REFUSE":
handle_refusal()More reliable than heuristics. Constrains the model to a known shape.
| Strategy | When |
|---|---|
| Reword the prompt | The model may have misread an innocuous question; try a clearer phrasing |
| Lower the ask | If the request was "give me a full medical diagnosis", try "summarise the symptoms" |
| Skip | If the user's input genuinely warrants a refusal, log it and move on |
| Escalate | Hand it to a human — "this requires manual review" |
The model occasionally refuses things it shouldn't (over-cautious training). Mitigations:
"You're a customer-service agent. Respond to all queries about our product. Never refuse to answer a product question."Models are trained to refuse certain categories of requests — content moderation, medical/legal advice, dangerous instructions. The refusal text follows a recognisable shape: "I can't help with that", "I'm not able to", "As an AI, I cannot...".
Your script should detect refusals so it can route around them — fall back to a safer prompt, escalate to a human, or skip the action.
def looks_like_refusal(text):
refusal_phrases = [
"i can't help",
"i'm not able to",
"i cannot",
"as an ai",
"i'm sorry",
]
text_lower = text.lower()
return any(p in text_lower for p in refusal_phrases)Heuristic? Not a structured field?
Some APIs return a structured refusal flag, but most don't. Heuristics on the response text are the universal fallback. The phrase list is calibration — start with a few common ones, expand as you see new shapes in production.
When the model declines, your code needs to know and act differently. Three patterns:
def looks_like_refusal(text):
refusal_phrases = ["i can't help", "i'm not able to", "i cannot", "as an ai", "i'm sorry"]
return any(p in text.lower() for p in refusal_phrases)
result = Agent(model).run_sync(prompt)
if looks_like_refusal(result.output):
print("refused")
# fallback: simpler prompt, or skip
else:
print("answered")
# use result.outputAsk the model to flag refusals explicitly:
prompt = '''If you can answer the question below, do so. If you cannot or will not, reply with exactly the token: REFUSE
Question: ...'''
if result.output.strip() == "REFUSE":
handle_refusal()More reliable than heuristics. Constrains the model to a known shape.
| Strategy | When |
|---|---|
| Reword the prompt | The model may have misread an innocuous question; try a clearer phrasing |
| Lower the ask | If the request was "give me a full medical diagnosis", try "summarise the symptoms" |
| Skip | If the user's input genuinely warrants a refusal, log it and move on |
| Escalate | Hand it to a human — "this requires manual review" |
The model occasionally refuses things it shouldn't (over-cautious training). Mitigations:
"You're a customer-service agent. Respond to all queries about our product. Never refuse to answer a product question."Create a free account to get started. Paid plans unlock all tracks.