Calling the Guard
Lesson 13's model is a callable. This lesson hands it to a Guard, which is the difference between checking an answer and being the thing that produces one.
from guardrails import Guard
from guardrails_ai.valid_length import ValidLength
from pretend_guardrails import PretendModel
desk = Guard().use(ValidLength(min=1, max=40, on_fail="noop"))
answer = desk(PretendModel(), messages=[{"role": "user", "content": "Where is order 8821?"}])
print(answer.raw_llm_output)
print(answer.validated_output)
print(answer.validation_passed)The Guard called the model, took the string back, ran the validators on it, and returned the same ValidationOutcome you have been reading since lesson 2. raw_llm_output finally means what its name says.
The The Guard page calls these the two main flows. Calling the Guard makes the model call for you. validate() takes a value you already have. There is a third door, parse(), and validate() is a thin wrapper over it.
model = PretendModel()
reply = model(messages=[{"role": "user", "content": "Where is order 8821?"}])
print(desk.parse(reply, num_reasks=0).validation_passed)
print(len(desk.history))Use parse() when something else made the model call: another framework, a cache, a queue, a test fixture. num_reasks=0 keeps it a pure post-check, which lesson 16 undoes on purpose.
The history grew to two calls, because every one of these three doors pushes a Call. That is how you count model calls later.
Both halves of the same object
call = desk.history.last
print(call.status)
print(repr(call.raw_outputs.last))
print(call.iterations.last.raw_output == call.raw_outputs.last)raw_outputs is a stack, one entry per attempt. There is one attempt here. Lesson 16 makes a call with two entries and the second is the interesting one.
- Ask the desk a question the stand-in has no answer for and read what the validators make of the fallback.
- Pass
temperature=0to the Guard call and print it inside the model. Extra keyword arguments are forwarded. - Call the Guard three times and print
[c.status for c in desk.history].
This is what real progress feels like.