Guardrails AI: Add Safety Rails and Output Validation to Any LLM
Guardrails AI wraps LLM calls with validators for PII detection, toxicity, JSON schema, and custom rules - with automatic reask-and-retry when validation fails.
LLMs produce text that can be malformed, unsafe, or structurally incorrect. In production apps you need to handle: outputs that are not valid JSON when you expected JSON, responses that contain PII (names, emails, phone numbers), toxic language in customer-facing chatbots, and hallucinated content that violates business rules. Guardrails AI centralises this validation logic.
from guardrails import Guard
from guardrails.hub import DetectPII
guard = Guard().use(DetectPII(pii_entities=["EMAIL_ADDRESS", "PHONE_NUMBER"], on_fail="refrain"))
response = guard(
openai.chat.completions.create,
prompt="Summarise this support ticket: John called from john@acme.com about billing.",
model="gpt-4o-mini",
)
# If PII is detected in the output, response.validated_output = None (refrain)
from guardrails.validators import Validator, register_validator, PassResult, FailResult
@register_validator(name="no-code-snippets", data_type="string")
class NoCodeSnippets(Validator):
def validate(self, value: str, metadata: dict):
if "```" in value or "def " in value:
return FailResult(
error_message="Response must not contain code snippets.",
fix_value=value.split("```")[0].strip(),
)
return PassResult()
guard = Guard().use(NoCodeSnippets(on_fail="fix"))
on_fail Actions
Action
Behaviour
reask
Send validation error back to LLM and retry
refrain
Return None (silently skip bad output)
fix
Apply the fix_value from the validator
exception
Raise ValidationError
noop
Log but return the invalid output
Server Mode for Microservices
guardrails start --config config.py --port 8000
Call the validation server from any language via HTTP - useful when your LLM app is not Python.
Practical deep-dives on LLMs, developer tools, and AI engineering. No filler. Unsubscribe any time.
// written byFIG. AUTH-01
538
Mahmudul Haque Qudrati
CEO & ML Engineer
CEO and ML Engineer at Pristren. Builds AI-powered software for teams and writes about machine learning, LLMs, developer tools, and practical AI applications.
What Is Alibaba Banning Claude Code Over Backdoor Risks? A Practical Overview
Alibaba is reportedly banning Claude Code from its workplace due to alleged backdoor risks. This post explains the incident, the technical concerns, and the broader implications for AI coding assistants in regulated environments.