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

fix repairs the value

Lesson 9's two actions hand back the broken value or nothing. fix hands back a repaired one, and only when the validator supplied a repair.

Example
from guardrails import Guard
from guardrails_ai.valid_length import ValidLength

desk = Guard().use(ValidLength(min=1, max=20, on_fail="fix"))
outcome = desk.validate("Order 8821 is running late and arrives Friday.")

print(outcome.validation_passed)
print(repr(outcome.validated_output))

ValidLength knows how to make a value shorter, so it truncates. The verdict is True, which is worth stopping on: the verdict describes the value you are being handed, not the value the Guard was given.

The record still knows better.

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

print(log.validation_result.outcome)
print(repr(log.value_before_validation))
print(repr(log.value_after_validation))

This is the pair lesson 4 asked you to print. With noop the two values match; with fix they do not, and the difference is the repair.

A repair that is not there

The NoRefunds validator from lesson 8 returned a FailResult with an error_message and nothing else. Set it to fix and see what arrives.

Example
desk = Guard().use(NoRefunds(on_fail="fix"))
outcome = desk.validate("We can refund order 8821 in full.")

print(outcome.validation_passed)
print(repr(outcome.validated_output))

None, and no complaint about it. A validator that cannot repair and is asked to repair produces nothing, and the only clue is a value that was a string a moment ago.

This is also what you get from the on_fix spelling in lesson 7. The keyword is dropped, fix_value stays empty, and the symptom is this None rather than anything mentioning the typo.

Giving it something to work with

Example
desk = Guard().use(NoRefunds(on_fail="fix"))
outcome = desk.validate("We can refund order 8821 in full.")

print(outcome.validation_passed)
print(outcome.validated_output)

The Guard hands back what the validator put in fix_value. Guardrails never invents a repair; it only ever passes yours along. The hub validators that repair, like LowerCase and ValidLength, do it the same way.

Try it yourself
  • Set on_fail="fix" on NoCompetitors from lesson 8 and give it a fix_value that deletes the sentence containing the name.
  • Put two repairing validators in one Guard and work out from value_before_validation which one saw the other's output.
  • Rewrite the fix_value in NoRefunds to keep the original capitals. The version here lower-cases the whole reply, which is a repair nobody asked for.

Every expert started right here.