Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 9 · 8. Production, Evaluation & Governance
Lesson 67 of 68

Input & Output Guardrails

The firewall for your LLM. Validating what goes in and out.

Derpx0610 Jan · 1 min read
LangChain Mastery
Async & High-Throughput PatternsLatency & TTFT OptimizationTracing with LangSmithRAG Evaluation BasicsFaithfulness & RelevancePrompt Injection DefenseInput & Output GuardrailsDeployment & API Serving
Contents
1. Why We Need Rails2. Input Guardrails (Topic Control)3. Output Guardrails (Format & content)4. Implementation Logic5. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Async & High-Throughput PatternsLatency & TTFT OptimizationTracing with LangSmithRAG Evaluation BasicsFaithfulness & RelevancePrompt Injection DefenseInput & Output GuardrailsDeployment & API Serving
Contents
1. Why We Need Rails2. Input Guardrails (Topic Control)3. Output Guardrails (Format & content)4. Implementation Logic5. Summary

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?

  1. Format Check: Did it return valid JSON? If not, retry.
  2. Safety Check: Did it generate hate speech? If yes, replace with "I cannot answer that."
  3. Hallucination Check: Run the Faithfulness Eval (see previous lesson). If Score < 0.5, reply "I don't know."

4. Implementation Logic

simple_guardrail.py
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."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonPrompt Injection DefenseNext lesson →Deployment & API Serving