Prompt
What should I use to add guardrails to AI API requests?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
To add guardrails to AI API requests, the best choice depends on what kind of protection you want:
1. Input/output validation
Use schema validation to enforce request/response structure.
- JSON Schema for raw API payloads
- Pydantic (Python) or Zod (TypeScript) for typed validation
Good for:
- making sure fields are present
- enforcing types and ranges
- rejecting malformed prompts or outputs
2. Content moderation / safety filtering
Use a moderation layer before and after the model call.
- Pre-screen user input for unsafe content
- Post-screen model output before returning it
You can use:
- OpenAI Moderation endpoint
- Azure AI Content Safety
- Google Cloud DLP / safety tooling
- custom keyword/rule-based filters for domain-specific cases
3. Policy enforcement
If you need stronger business rules, use a policy engine.
- Open Policy Agent (OPA) is a common choice
- Useful for access control, allowed tools, data routing, tenant rules, and compliance
4. Prompt / tool safety
If your AI can call tools or functions:
- allowlist tools
- validate arguments strictly
- require human approval for sensitive actions
- limit tool scope and permissions
5. Structured guardrail frameworks
If you want an end-to-end solution, consider:
- Guardrails AI
- NeMo Guardrails
- LangChain / LangGraph with custom validators
- LlamaGuard for safety classification
Practical recommendation
A strong default setup is:
- Validate request schema
- Run moderation on input
- Constrain the model with structured output
- Validate and moderate output
- Apply policy checks before any tool/action execution
If you want, I can recommend a specific stack for Python, TypeScript, or OpenAI API usage.