No Refusals
The no_refusals guardrail detects when the LLM refuses to answer or deflects the question.
Import
Section titled “Import”from pydantic_ai_guardrails.guardrails.output import no_refusalsBasic Usage
Section titled “Basic Usage”from pydantic_ai_guardrails import GuardedAgentfrom pydantic_ai_guardrails.guardrails.output import no_refusals
guarded_agent = GuardedAgent( agent, output_guardrails=[ no_refusals(), ],)Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
refusal_patterns | list[str] | None | Default patterns | Custom refusal patterns |
Default Patterns
Section titled “Default 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”
Examples
Section titled “Examples”Default Detection
Section titled “Default Detection”guardrail = no_refusals()Custom Patterns
Section titled “Custom Patterns”guardrail = no_refusals( refusal_patterns=[ r"I cannot", r"I'm unable to", r"outside my capabilities", r"I don't have access", ],)Violation Result
Section titled “Violation Result”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',}Use Cases
Section titled “Use Cases”- Completeness: Ensure agent always attempts to help
- UX quality: Avoid unhelpful responses
- Retry trigger: Use with auto-retry to get better answers
With Auto-Retry
Section titled “With Auto-Retry”guarded_agent = GuardedAgent( agent, output_guardrails=[no_refusals()], max_retries=2,)The agent will retry with feedback to provide a helpful response.