Safety/alignment guardrails for AI agents
Project description
🛡️ Bumpers
Add safety guardrails to your AI agents with just a few lines of code. Bumpers is a Python library that helps you build safer AI applications by validating agent actions and outputs in real-time.
✨ Features
- 🔒 Validation Engine: Pre-built validators for common safety checks
- 🤖 Framework Support: Works with both custom agents and popular frameworks like LangChain
- 🚫 Action Control: Whitelist allowed actions and block dangerous operations
- 🔍 Content Filtering: Detect and block sensitive or inappropriate content
- 📊 Semantic Monitoring: Track semantic drift and goal fulfillment
- ⏱️ Rate Limiting: Prevent infinite loops and resource abuse
- 📝 Logging & Analytics: Track and analyze agent behavior
🚀 Quick Start
Installation
- Install:
pip install bumpers
Basic Usage with LangChain
from langchain.agents import AgentExecutor
from bumpers.core.engine import CoreValidationEngine
from bumpers.validators.action import ActionWhitelistValidator
from bumpers.validators.content import ContentFilterValidator
from bumpers.integrations.langchain_callback import BumpersLangChainCallback
# Set up validation engine
validation_engine = CoreValidationEngine()
# Add validators
validation_engine.register_validator(
ActionWhitelistValidator(allowed_actions=["search", "calculate"]),
ValidationPoint.PRE_ACTION
)
validation_engine.register_validator(
ContentFilterValidator(forbidden_words=["confidential", "secret"]),
ValidationPoint.PRE_OUTPUT
)
# Create Bumpers callback
bumpers_callback = BumpersLangChainCallback(validation_engine=validation_engine)
# Add to your LangChain agent
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
callbacks=[bumpers_callback] # Add Bumpers protection
)
# Use normally - Bumpers will automatically protect your agent
result = agent_executor.invoke({"input": "What's 2 + 2?"})
🛡️ Available Validators
Action Control
ActionWhitelistValidator: Restrict which actions an agent can performResourceValidator: Prevent resource-intensive operationsRateLimitValidator: Control action frequency
Content Safety
ContentFilterValidator: Block sensitive or inappropriate contentLLMSafetyValidator: Use LLMs to validate content safetyPatternValidator: Block content matching specific patterns
Semantic Control
SemanticDriftValidator: Keep responses on topicGoalFulfillmentValidator: Ensure answers address the original questionTemporalRelevanceValidator: Validate time-based responses
Chain Control
ChainLengthValidator: Limit action chain lengthRedundancyLoopingValidator: Detect and prevent action loops
🔍 Monitoring & Analytics
Track agent behavior and get alerts:
from bumpers.monitoring.monitor import BumpersMonitor
from bumpers.monitoring.conditions import create_high_failure_rate_condition
def alert_handler(message):
print(f"🚨 Alert: {message}")
# Create monitor
monitor = BumpersMonitor(
logger=logger,
alert_handlers=[alert_handler]
)
# Add conditions
monitor.add_condition(
create_high_failure_rate_condition(threshold=0.3)
)
# Start monitoring
monitor.start()
📊 Analytics
Analyze agent behavior:
from bumpers.analytics.analyzer import BumpersAnalyzer
analyzer = BumpersAnalyzer(logger)
# Get validation statistics
stats = analyzer.get_validation_stats()
print("Validation Stats:", stats)
# Get intervention summary
summary = analyzer.get_intervention_summary()
print("Interventions:", summary)
🎯 Fail Strategies
Customize how validators handle failures:
from bumpers.validators.base import FailStrategy
validator = ContentFilterValidator(
forbidden_words=["confidential"],
fail_strategy=FailStrategy.STOP # Immediately halt execution
)
validator = RateLimitValidator(
max_actions_per_minute=10,
fail_strategy=FailStrategy.RAISE_ERROR # Raise exception but allow catching
)
validator = PatternValidator(
patterns=[r"sensitive\s+data"],
fail_strategy=FailStrategy.LOG_ONLY # Just log the violation
)
🔧 Custom Validators
Create your own validators:
from bumpers.validators.base import BaseValidator
from bumpers.core.engine import ValidationResult, ValidationPoint
class CustomValidator(BaseValidator):
def __init__(self, name: str = "custom"):
super().__init__(name)
def validate(self, context: dict) -> ValidationResult:
# Implement your validation logic
return ValidationResult(
passed=True,
message="Validation passed",
validator_name=self.name,
validation_point=ValidationPoint.PRE_ACTION,
context=context
)
🌟 Star History
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bumpers-0.1.4.tar.gz.
File metadata
- Download URL: bumpers-0.1.4.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7dfeac2e344a60ff92a3f9fd1fd3454baaf7a37fbe52c682d57eadcc30d7ca8
|
|
| MD5 |
94bcfc91c4e856ffdc2679dcb0b8c029
|
|
| BLAKE2b-256 |
6b4d4ea221ff16226e63e8419205cbd103e97433cca308bca78ea55336774946
|
File details
Details for the file bumpers-0.1.4-py3-none-any.whl.
File metadata
- Download URL: bumpers-0.1.4-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87745a15a81a2e6123ee137ceff93d8d9687814a6c973ef0688d9e8ded9d2f8e
|
|
| MD5 |
4550f8e3ed7cdfce125a086c948e4de0
|
|
| BLAKE2b-256 |
a09a8e0ef5de0e39b0155473f933006f1e3f5837c5cc4d809c729e65eb098edd
|