Safety Policies
Learn how to implement safety guardrails and content filtering using Upsonic’s policy system. Protect your agents from harmful inputs and outputs with customizable safety rules.Policy System Overview
Upsonic’s safety engine provides:- Input Validation: Filter user inputs before processing
- Output Filtering: Check agent responses before delivery
- Tool Validation: Approve tools before and after execution
- Custom Rules: Build domain-specific safety policies
- Feedback Loops: Automatically retry with helpful messages
src/upsonic/safety_engine/base/policy.py:12-96
Policy Architecture
A policy combines two components:- Rule: Detects policy violations
- Action: Determines what to do when violated
Using Policies
Policy Types
User Input Policies
Validate user inputs before processing:Agent Output Policies
Filter agent responses:src/upsonic/agent/policy_manager.py
Tool Policies
Control tool usage:Creating Custom Policies
Custom Rule
Implement custom detection logic:Custom Action
Define what happens on violation:Combine into Policy
Policy Feedback Loops
Instead of blocking, provide helpful feedback:src/upsonic/agent/agent.py:413-434
Advanced Policy Features
LLM-Based Rules
Use LLMs for complex detection:Multi-Language Support
Severity Levels
Built-in Policies
Upsonic provides many pre-built policies:Content Safety
Privacy Protection
Professional Boundaries
Security
Technical Operations
src/upsonic/safety_engine/
Best Practices
Async Support: Policies support async operations for better performance:Reference:
src/upsonic/safety_engine/base/policy.py:60-90Common Use Cases
Customer Support Bot
Enterprise Assistant
Content Moderation
Next Steps
- Building Agents - Apply policies to agents
- Custom Tools - Add tool safety policies
- Multi-Agent Workflows - Team-wide policies
- Memory Management - Privacy in stored data