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.1.tar.gz (19.4 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.1-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bumpers-0.1.1.tar.gz
  • Upload date:
  • Size: 19.4 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.1.tar.gz
Algorithm Hash digest
SHA256 a11e0b12d97b4405a8f8a3df2538b0932b800437eee648a8ea39853dc877ba1b
MD5 cd59524ac5519faf9a4aa877fefebb19
BLAKE2b-256 56c267696e41b7b95d8363f435a6a03fbd62a2affbb135898e9ae0689f4ddc05

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bumpers-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b169f70384b49908a5645a6128f1a9105f8f0a301c88a485ad57f563ed79b93
MD5 a3880cdef395fcda9c0fc9e91dd68096
BLAKE2b-256 a0ac57f44d864a61db7e829fca82a656cd25cdb51d8254acd770fced174372bf

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