Skip to main content

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.

Python Version License Build Status

✨ 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

  1. Clone the repository:
git clone https://github.com/pythonomar22/bumpers.git
cd bumpers
  1. Create and activate a virtual environment (recommended):
# On macOS/Linux
python -m venv bumpersvenv
source bumpersvenv/bin/activate

# On Windows
python -m venv bumpersvenv
bumpersvenv\Scripts\activate
  1. Install in editable mode:
pip install -e .

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 perform
  • ResourceValidator: Prevent resource-intensive operations
  • RateLimitValidator: Control action frequency

Content Safety

  • ContentFilterValidator: Block sensitive or inappropriate content
  • LLMSafetyValidator: Use LLMs to validate content safety
  • PatternValidator: Block content matching specific patterns

Semantic Control

  • SemanticDriftValidator: Keep responses on topic
  • GoalFulfillmentValidator: Ensure answers address the original question
  • TemporalRelevanceValidator: Validate time-based responses

Chain Control

  • ChainLengthValidator: Limit action chain length
  • RedundancyLoopingValidator: 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

Star History Chart

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

bumpers-0.1.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bumpers-0.1.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file bumpers-0.1.0.tar.gz.

File metadata

  • Download URL: bumpers-0.1.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for bumpers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7453b1976f17f9bedf9a05553ff60e955d6b86cc8211f1b16ea497a8c3b4fa63
MD5 ba7fc743b30a554ddd6fdbfd28d7ae5c
BLAKE2b-256 472ec9129666d25c66df4ab2142185a6659d87ef2ac8aeeee9d581145fe5020e

See more details on using hashes here.

File details

Details for the file bumpers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bumpers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for bumpers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30d125635666a11cb84b25e94d4089113ad2489f11633bf57bb71af0d8b8f490
MD5 612258e76a3c137855aaff2aed13369a
BLAKE2b-256 6df2c200660ab7b0417f84844dd7c21450a1f596eeae8c9ca74922b5ac65fd10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page