Skip to main content

A comprehensive collection of AI guardrails built with DSPy for 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

Early Return

Stop execution on the first failing guardrail.

guardrails = [guardrail.Topic(topic_scopes=["AI"]), guardrail.Nsfw()]
result = guardrail.Run(guardrails, "Risky content", early_return=True)
print(result.is_allowed)

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.4.tar.gz (19.3 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.4-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dspy_guardrails-0.1.4.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dspy_guardrails-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d0668ad6a590c868df367797faa6e7c3cd18ad45d51067833f75ce74716174c1
MD5 bd4779a17dae4ee63a38d98edc1c6c09
BLAKE2b-256 c1b72a3e1f8524ddbeccf33befd2a2e142c4584b015b71a63dbcf49613ef8b3b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dspy_guardrails-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dspy_guardrails-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 855d6b248f00a00743935b17db7544c8276a638e91339bc42afff5a53fcd2f15
MD5 a6cbda7b17f4099e687f91b2b2884734
BLAKE2b-256 add174a849a6d44e92ab08ff5bc64ed91fe81cf964ef9937ef11fb61c76cf50b

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