BlockKeywords
Blocks prompts that contain any of the given keywords.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
keywords |
Sequence[str] | str |
required | Keywords to block (at least one) |
case_sensitive |
bool |
False |
Match case exactly |
whole_words |
bool |
False |
Match on word boundaries only, so pass does not match password |
regex |
bool |
False |
Treat each keyword as a regular expression |
on_fail |
'raise' | 'warn' |
'raise' |
Raise InputBlocked or log and continue |
An empty keyword list or an invalid regex raises ValueError at construction.
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import BlockKeywords, InputBlocked
agent = Agent( 'openai:gpt-4o', capabilities=[BlockKeywords(['password', 'api key'], whole_words=True)],)
try: agent.run_sync('What is the admin password?')except InputBlocked as e: print(e.reason) # blocked keyword(s): 'password'from pydantic_ai_guardrails import BlockKeywords
BlockKeywords([r'\bdrop\s+table\b', r'\brm\s+-rf\b'], regex=True)Failure behaviour
Section titled “Failure behaviour”on_fail='raise': raisesInputBlocked;.reasonlists the matched keywords.on_fail='warn': logs and continues.