How to use guardrails in production
Quick answer
Use
guardrails by integrating them as middleware or wrappers around your AI API calls to enforce safety, compliance, and output constraints in production. Implement guardrails with libraries like guardrails-ai or custom validation logic to monitor and filter AI responses before use.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0pip install guardrails-ai
Setup
Install the necessary packages and set your environment variables for API keys. Use pip install openai guardrails-ai to install the OpenAI SDK and the guardrails-ai library for guardrail enforcement.
Set your OpenAI API key in the environment variable OPENAI_API_KEY before running your code.
pip install openai guardrails-ai Step by step
This example shows how to use guardrails-ai to enforce a simple guardrail that restricts the AI output to a specific format and filters out disallowed content in production.
import os
from openai import OpenAI
from guardrails import Guard
# Load your OpenAI API key from environment
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Define a guardrail schema (YAML or JSON string) to enforce output constraints
schema = '''
- name: response
type: str
description: "The AI response must be a polite answer without disallowed words."
constraints:
- not_contains: ["hate", "violence", "illegal"]
'''
# Initialize the guard with the schema
guard = Guard.from_yaml(schema)
# Define a prompt
prompt = "Explain the benefits of AI in healthcare."
# Call the OpenAI chat completion
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)
# Extract the raw AI output
raw_output = response.choices[0].message.content
# Run the guardrail validation
validated_output = guard.parse(raw_output)
print("Validated AI response:", validated_output["response"]) output
Validated AI response: AI improves healthcare by enabling faster diagnoses, personalized treatments, and better patient outcomes while ensuring ethical use.
Common variations
- Use asynchronous calls with
asyncioandawaitfor non-blocking guardrail validation. - Apply guardrails to other LLM providers like Anthropic by adapting the client calls.
- Customize guardrail schemas to enforce JSON structure, numeric ranges, or safe content filtering.
- Integrate guardrails as middleware in web frameworks (FastAPI, Flask) to validate AI responses before sending to users.
import asyncio
import os
from openai import OpenAI
from guardrails import Guard
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
schema = '''
- name: response
type: str
constraints:
- not_contains: ["hate", "violence"]
'''
guard = Guard.from_yaml(schema)
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is AI?"}]
)
raw_output = response.choices[0].message.content
validated_output = guard.parse(raw_output)
print("Validated async response:", validated_output["response"])
asyncio.run(main()) output
Validated async response: AI is the simulation of human intelligence processes by machines, enabling automation and data-driven decisions.
Troubleshooting
- If guard validation fails with errors like
ConstraintViolation, review your guardrail schema to ensure it matches expected output formats. - If the AI output contains disallowed content, tighten your guard constraints or add post-processing filters.
- For performance issues, cache guard instances and avoid reloading schemas on every request.
- Ensure environment variables are correctly set to avoid authentication errors with the AI provider.
Key Takeaways
- Use
guardrails-aito enforce output safety and format constraints in production AI calls. - Define clear guardrail schemas to validate and filter AI responses before use.
- Integrate guardrails as middleware or wrappers for scalable, reliable AI deployments.