Skip to main content

DSPy-based guardrails for AI content moderation and security

Project description

DSPy Guardrails

A comprehensive collection of AI guardrails built with DSPy for content moderation and security.
Explore the Documentation »
Report Bug · Request Feature

Table of Contents
  1. About
  2. Quick Start
  3. Usage
  4. Development
  5. Contributing
  6. License

About

DSPy Guardrails is a comprehensive suite of AI guardrails built with DSPy. Each guardrail is implemented as a self-contained module that can be used to test and validate different types of content moderation and security checks.

  • Modular design — Each guardrail type is implemented as a separate, self-contained module
  • Programmatic testing — Run guardrails directly in Python for fast iteration
  • Comprehensive coverage — Covers major content moderation and security scenarios
  • DSPy integration — Leverages DSPy's programmatic prompting for consistent, reliable results
  • Type safety — Full type hints and dataclass definitions for robust implementations

(back to top)

Quick Start

Install

Install dspy-guardrails with uv (recommended)

uv add dspy-guardrails

Install with pip (alternative)

pip install dspy-guardrails

Continue with the usage examples below.

(back to top)

Usage

Basic Usage

import dspy
from dspy_guardrails import guardrail

# Configure DSPy (required)
lm = dspy.LM("openrouter/google/gemini-2.5-flash-preview-09-2025")
guardrail.configure(lm=lm)

# Create and run guardrails
topic_guardrail = guardrail.Topic(topic_scopes=["AI", "Machine Learning"])
result = guardrail.Run(topic_guardrail, "I want to learn about neural networks")
print(f"Allowed: {result.is_allowed}")  # True

Multiple Guardrails

Assumes guardrail.configure(lm=lm) has already been called.

all_guardrails = [
    guardrail.Topic(topic_scopes=["AI"]),
    guardrail.Nsfw(),
    guardrail.Pii(),
]
result = guardrail.Run(all_guardrails, "Safe AI content")
print(f"All passed: {result.is_allowed}")  # True

For more examples and patterns, see the complete quickstart guide and guardrail types.

(back to top)

Available Guardrails

Guardrail Example variables Example instantiation
Topic topic_scopes=["AI", "Machine Learning"], blocked_topics=["spam"] guardrail.Topic(topic_scopes=["AI", "Machine Learning"], blocked_topics=["spam"])
NSFW sensitivity_level="high" guardrail.Nsfw(sensitivity_level="high")
PII allowed_pii_types=["email"] guardrail.Pii(allowed_pii_types=["email"])
Toxicity toxicity_threshold=0.8 guardrail.Toxicity(toxicity_threshold=0.8)
Tone desired_tone="helpful", unwanted_tones=["sarcastic"] guardrail.Tone(desired_tone="helpful", unwanted_tones=["sarcastic"])
Grounding grounding_threshold=0.8 guardrail.Grounding(grounding_threshold=0.8)
Language allowed_languages=["en", "es"] guardrail.Language(allowed_languages=["en", "es"])
Keywords blocked_keywords=["password", "secret"], case_sensitive=False guardrail.Keywords(blocked_keywords=["password", "secret"], case_sensitive=False)
Secret Keys key_patterns=["sk-", "ghp_"], entropy_threshold=3.5 guardrail.SecretKeys(key_patterns=["sk-", "ghp_"], entropy_threshold=3.5)
Gibberish prob_threshold=0.7 guardrail.Gibberish(prob_threshold=0.7)
Prompt Injection injection_patterns=["ignore previous", "system override"] guardrail.PromptInjection(injection_patterns=["ignore previous", "system override"])
Jailbreak detection_threshold=0.9 guardrail.Jailbreak(detection_threshold=0.9)

Development

Code Quality

This project uses several tools to maintain code quality:

  • Ruff: Linting and formatting
  • isort: Import sorting
  • pytest: Testing framework

Available commands:

# Run all quality checks
uv run poe clean

# Individual checks
uv run poe lint          # Ruff linting
uv run poe format        # Ruff formatting
uv run poe sort          # Import sorting

Testing

Run tests using pytest:

# Run all tests
uv run pytest

# Run specific test
uv run pytest path/to/test.py::test_name

(back to top)

Contributing

Quick workflow:

  1. Fork and branch: git checkout -b feature/name
  2. Make changes
  3. Run checks: uv run poe clean-full
  4. Commit and push
  5. Open a Pull Request

(back to top)

License

MIT (as declared in pyproject.toml).

(back to top)


Built by thememium

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

dspy_guardrails-0.1.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

dspy_guardrails-0.1.1-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dspy_guardrails-0.1.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dspy_guardrails-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7a28cc18a54324dbac9b00bfd14c9ca2a321077f896709c560ed87cada1a747f
MD5 4eda72bc85bd397cbc6098076d835043
BLAKE2b-256 5a568aa96a66d3ee5cb3bf668d2202affdb0b5950a5cfa91dd1e281492476c34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dspy_guardrails-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 32.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dspy_guardrails-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a74a94de8f9a2e7108217a6c2f66d2b6c16e66dff159f0224c3bfe47add14ef6
MD5 f3cfba5ce017388a09e284ce277ce279
BLAKE2b-256 3b81c73afa71c4cf85333d53e1a87c548f5beb195c65bdb5c5acf341a8992a5a

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