/ examples / consolidated_params / basic_guardrails.py
basic_guardrails.py
 1  """
 2  Basic Guardrails Example - Agent-Centric API
 3  
 4  Demonstrates guardrails with callable validator.
 5  """
 6  
 7  from praisonaiagents import Agent
 8  
 9  # Define a simple guardrail validator
10  def validate_response(output):
11      """Validate that response doesn't contain forbidden words."""
12      forbidden = ["error", "fail", "cannot"]
13      text = str(output).lower()
14      for word in forbidden:
15          if word in text:
16              return (False, f"Response contains forbidden word: {word}")
17      return (True, output)
18  
19  # Basic: Use callable guardrail
20  agent = Agent(
21      instructions="You are a helpful assistant.",
22      guardrails=validate_response,
23  )
24  
25  response = agent.start("What is 2 + 2?")
26  print(response)