MatchRegex
Requires the output to match one pattern, or every pattern with require_all=True.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
patterns |
str | Sequence[str] | Mapping[str, str] |
required | One pattern, a list, or a name-to-pattern mapping (names appear in failure reasons) |
require_all |
bool |
False |
Every pattern must match |
full_match |
bool |
False |
Match the whole output instead of searching within it |
case_sensitive |
bool |
True |
Case-sensitive matching |
on_fail |
'retry' | 'raise' | 'warn' |
'retry' |
See below |
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import MatchRegex
agent = Agent( 'openai:gpt-4o', capabilities=[ MatchRegex( {'ticket_id': r'TICKET-\d{4,}', 'sign_off': r'Kind regards'}, require_all=True, ), ],)from pydantic_ai_guardrails import MatchRegex
MatchRegex(r'\d{4}-\d{2}-\d{2}', full_match=True) # output must be a bare ISO dateFailure behaviour
Section titled “Failure behaviour”on_fail='retry'(default): raisesModelRetry. The model sees the reason and tries again, up to the agent’s output retry budget (Agent(retries={'output': N})).on_fail='raise': raisesOutputBlockedwith.guardrailand.reason.on_fail='warn': logs to thepydantic_ai_guardrailslogger and returns the output unchanged.