Regex Match
The regex_match guardrail validates output against regex patterns.
Import
Section titled “Import”from pydantic_ai_guardrails.guardrails.output import regex_matchBasic Usage
Section titled “Basic Usage”from pydantic_ai_guardrails import GuardedAgentfrom pydantic_ai_guardrails.guardrails.output import regex_match
guarded_agent = GuardedAgent( agent, output_guardrails=[ regex_match(pattern=r'^[A-Z]', must_match=True), ],)Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | str | Required | Regex pattern |
must_match | bool | True | If True, output must match. If False, output must not match. |
flags | int | 0 | Regex flags (e.g., re.IGNORECASE) |
Examples
Section titled “Examples”Must Match
Section titled “Must Match”# Output must start with capital letterguardrail = regex_match( pattern=r'^[A-Z]', must_match=True,)Must Not Match
Section titled “Must Not Match”# Block TODO markersguardrail = regex_match( pattern=r'TODO|FIXME|XXX', must_match=False,)Case Insensitive
Section titled “Case Insensitive”import re
guardrail = regex_match( pattern=r'error|warning|fail', must_match=False, flags=re.IGNORECASE,)Format Validation
Section titled “Format Validation”# Must be a valid date formatguardrail = regex_match( pattern=r'^\d{4}-\d{2}-\d{2}$', must_match=True,)Violation Result
Section titled “Violation Result”When triggered, returns:
{ 'tripwire_triggered': True, 'message': 'Output does not match required pattern', 'severity': 'medium', 'metadata': { 'pattern': r'^[A-Z]', 'must_match': True, },}Use Cases
Section titled “Use Cases”- Format enforcement: Dates, IDs, codes
- Content filtering: Block unwanted phrases
- Structure validation: Headers, sections
- Quality checks: No placeholder text