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

A real model, in one line

Twenty-two lessons have run on a function that pretends. This lesson is the line that makes it real, and it is the one lesson here with nothing printed, because running it would need an account, a key and a card.

Everything else on the site prints what it really printed. There is nothing to print here, and inventing a plausible transcript is exactly the habit this site exists to avoid, so the page says so instead.

The swap

python
from guardrails import Guard
from guardrails_ai.valid_length import ValidLength

desk = Guard().use(ValidLength(min=1, max=200, on_fail="reask"))

answer = desk(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Where is order 8821?"}],
    num_reasks=1,
)
print(answer.validated_output)

The first positional argument is gone and model= has taken its place. Nothing else in the file changes: the same validators, the same on_fail, the same outcome object, the same history.

Set OPENAI_API_KEY in the environment and that call works. Guardrails passes the model name and everything else straight to LiteLLM, which is why the Use supported LLMs page can claim a hundred providers: Anthropic with ANTHROPIC_API_KEY, Gemini with GEMINI_API_KEY, Azure with three variables, Bedrock, Mistral, a local Ollama. The model string changes and nothing else does.

What actually changes

  • Replies stop being predictable. Every test that asserted an exact string needs rewriting around the validators instead.
  • Reasks cost money. Lesson 16 counted model calls for free. Each one is now a billed request, which is the argument for fix_reask over reask.
  • Failures get interesting. Rate limits, timeouts and truncated answers. Guardrails retries some of these with backoff up to a sixty second wait, which the Error and Remediation page lists.
  • The prompt matters again. Lesson 19's placeholder was optional with a scripted model. With a real one, leaving it out means the model was never told what shape to produce.

Keeping the stand-in

Do not delete it. A stand-in that can be scripted is the only way to test a validator's behaviour without paying for randomness, and lesson 25 uses it for exactly that. A reasonable shape is a function that returns the real model name in production and a PretendModel in tests, with the Guard untouched either way.

python
import os

from pretend_guardrails import PretendModel


def model_for(guard, messages):
    if os.environ.get("OPENAI_API_KEY"):
        return guard(model="gpt-4o-mini", messages=messages)
    return guard(PretendModel(), messages=messages)
Try it yourself
  • Set a key and run the first snippet. Compare the reply with what the stand-in gives for the same question.
  • Point it at a local Ollama model with model="ollama/llama3" and no key at all.
  • Print answer.raw_llm_output next to answer.validated_output on a real model and see how often a reask actually fired.
PreviousAsyncGuard

Slow is fine. Stopping is the only problem.