Guardrails AIguardrails-ai 0.11.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
27 small wins to finish your pathNext lesson

Checking a reply with plain Python

Lesson 0 ran the finished desk. This lesson has no Guardrails in it at all. It is the same job done with if statements, so that the library has something to be better than.

The shop's support desk sends short replies. Two rules to start with: never promise a refund, and stay under thirty characters, because the message gateway truncates anything longer.

Example
reply = "We can refund order 8821 in full, today."

problems = []
if "refund" in reply.lower():
    problems.append("promises a refund")
if len(reply) > 30:
    problems.append("longer than 30 characters")

print(problems)

That works, and for two rules it is the right amount of code. The trouble starts when you want to do something about the problems rather than list them.

Example
if "refund" in reply.lower():
    reply = reply.replace("refund", "store credit")
if len(reply) > 30:
    reply = reply[:27] + "..."

print(reply)

The repair is now tangled into the detection. The same condition is written twice, once to notice and once to act, and the two copies will drift apart the first time someone edits one of them.

Pulling the rules into a list is the obvious next move.

Example
RULES = [
    ("promises a refund", lambda text: "refund" in text.lower()),
    ("longer than 30 characters", lambda text: len(text) > 30),
]

def check(text):
    return [name for name, is_bad in RULES if is_bad(text)]

print(check("We can refund order 8821 in full, today."))
print(check("Order 8821 ships today."))

That is a validator list, written badly. Three things are missing and each one is a lesson in this course.

  • A name a rule keeps. The string "promises a refund" is invented here and means nothing anywhere else.
  • A place to say what to do. Notice it, repair it, refuse to answer, or ask the model again. Right now that decision is spread across the file.
  • A record. When the desk is live you want to know which rule fired, on which reply, how often.

Guardrails is those three things, plus a library of rules other people already wrote. The next lesson replaces the list with one.

Try it yourself
  • Add a third rule that fails when the reply does not end in a full stop, and watch how much code the repair costs.
  • Reorder the two repairs so the truncation runs first. The output changes, which is the point about order that lesson 7 comes back to.
  • Write down, in one sentence, what your program should do when a reply breaks two rules at once. Keep it. Lesson 11 has an opinion.

Little by little, you're building something great.