Skip to content

No Refusals

The no_refusals guardrail detects when the LLM refuses to answer or deflects the question.

from pydantic_ai_guardrails.guardrails.output import no_refusals
from pydantic_ai_guardrails import GuardedAgent
from pydantic_ai_guardrails.guardrails.output import no_refusals
guarded_agent = GuardedAgent(
agent,
output_guardrails=[
no_refusals(),
],
)
ParameterTypeDefaultDescription
refusal_patternslist[str] | NoneDefault patternsCustom refusal patterns

Detects phrases like:

  • “I cannot help with that”
  • “I’m not able to”
  • “As an AI, I don’t”
  • “I apologize, but I cannot”
  • “I’m sorry, but I can’t”
  • “I don’t have the ability to”
guardrail = no_refusals()
guardrail = no_refusals(
refusal_patterns=[
r"I cannot",
r"I'm unable to",
r"outside my capabilities",
r"I don't have access",
],
)

When triggered, returns:

{
'tripwire_triggered': True,
'message': 'Model refused to answer',
'severity': 'medium',
'metadata': {
'pattern_matched': "I cannot help with that",
},
'suggestion': 'Provide a helpful response that addresses the question directly',
}
  • Completeness: Ensure agent always attempts to help
  • UX quality: Avoid unhelpful responses
  • Retry trigger: Use with auto-retry to get better answers
guarded_agent = GuardedAgent(
agent,
output_guardrails=[no_refusals()],
max_retries=2,
)

The agent will retry with feedback to provide a helpful response.