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

Reading what the Guard did

Lesson 3 printed False. That tells you something is wrong and nothing about what. Guardrails records every run in detail, and this lesson opens the record before the course needs it, because a debugging tool taught at the end is a tool nobody used.

The summaries

Example
for summary in outcome.validation_summaries:
    print(summary.validator_name)
    print(summary.validator_status)
    print(summary.failure_reason)

One entry per validator that failed, and nothing for the ones that passed. failure_reason is a sentence written by the validator itself, and lesson 16 shows that the same sentence is what gets sent back to the model when you ask it to try again.

The history

A Guard keeps every call it has ever run. Each validate(), parse() or direct call pushes a Call onto guard.history.

Example
call = desk.history.last

print(call.status)
print(len(call.iterations))
print([log.validator_name for log in call.validator_logs])

A Call is one thing you asked for. An Iteration is one attempt at it. There is one iteration here because nothing asked the model to try again; lesson 16 produces a call with two.

validator_logs is the full record, passes included, which is where you look when a rule you expected to fire did not.

One log entry

Example
log = desk.history.last.validator_logs[0]

print(log.registered_name)
print(repr(log.value_before_validation))
print(log.validation_result.outcome)
print(log.validation_result.error_message)

registered_name is the name the validator has inside Guardrails, and it is worth remembering that it did not change in 0.11. Only the Python import path moved. A configuration file that refers to guardrails/valid_length still works.

One attribute the documentation gets wrong

The Logs and History page prints first_call.validated_output. On 0.11.0 there is no such attribute.

Example
try:
    print(desk.history.last.validated_output)
except AttributeError as error:
    print("AttributeError:", error)

print(repr(desk.history.last.guarded_output))

The attribute is guarded_output, and it is not simply a rename. It returns a value only when the Guard passed, or when every failure was a noop. Lesson 11 hits the case where that rule matters and guarded_output comes back empty while the record plainly holds a value.

Try it yourself
  • Run desk.validate() three times with different strings, then print len(desk.history) and desk.history.first.status.
  • Print log.value_after_validation as well. With noop it equals the value before, and lesson 10 is where the two stop matching.
  • Import print from rich and print desk.history.last.tree. It draws the whole call, and it is the fastest way to see a run you did not expect.

This is what real progress feels like.