Input & Output Guardrails
The firewall for your LLM. Validating what goes in and out.
1. Why We Need Rails
An LLM is a probabilistic engine. It usually follows instructions, but sometimes it fails. If it fails in a chat app, it's annoying. If it fails in a medical app (Suggesting poison), it is catastrophic. Guardrails are deterministic code that wraps the probabilistic model.
2. Input Guardrails (Topic Control)
Block requests before they hit the expensive LLM.
- PII Detection: "My SSN is 123..." -> Block or Redact.
- Topic Filtering: "How do I make a bomb?" -> Block.
This saves money and improves safety. Tools: NVIDIA NeMo Guardrails, Lakera Guard.
3. Output Guardrails (Format & content)
The model generates an answer. Do we show it to the user?
- Format Check: Did it return valid JSON? If not, retry.
- Safety Check: Did it generate hate speech? If yes, replace with "I cannot answer that."
- Hallucination Check: Run the Faithfulness Eval (see previous lesson). If Score < 0.5, reply "I don't know."
4. Implementation Logic
response = llm.invoke(input)
# Deterministic Guard
if "kill" in response.lower():
return "Safety Violation Detected."
# Schema Guard (Pydantic)
try:
data = json.loads(response)
except:
return "Error: Invalid JSON format."5. Summary
Guardrails are the "Brakes" of the system. You cannot drive fast if you don't have good brakes. They allow you to deploy powerful models with confidence.
Key Intuition: "The model is the engine. The guardrail is the steering wheel."