Skip to main content

OpenGuardrails Python SDK - An LLM-based context-aware AI guardrail that understands conversation context for security, safety and data leakage detection.

Project description

OpenGuardrails Python Client

PyPI version Python Support License

An LLM-based context-aware AI guardrail that understands conversation context for security, safety and data leakage detection.

Features

  • 🧠 Context Awareness – Based on LLM conversation understanding rather than simple batch detection
  • 🔍 Prompt Injection Detection – Detects malicious prompt injections and jailbreak attacks
  • 📋 Content Compliance Detection – Complies with generative AI safety requirements
  • 🔐 Sensitive Data Leak Prevention – Detects and prevents personal or corporate data leaks
  • 🧩 User-level Ban Policy – Supports user-granular risk recognition and blocking strategies
  • 🖼️ Multimodal Detection – Supports image content safety detection
  • 🛠️ Easy Integration – OpenAI-compatible API format; plug in with one line of code
  • OpenAI-style API – Familiar interface design for rapid adoption
  • 🚀 Sync/Async Support – Supports both synchronous and asynchronous calls for different scenarios

Installation

pip install openguardrails

Quick Start

Basic Usage

from openguardrails import OpenGuardrails

# Create a client
client = OpenGuardrails(
    api_key="your-api-key",
    base_url="https://api.openguardrails.com/v1"  # Cloud API
)

# Check user input
result = client.check_prompt("I want to learn Python programming", user_id="user-123")
print(result.suggest_action)        # Output: pass
print(result.overall_risk_level)    # Output: no_risk
print(result.score)                 # Confidence score, e.g. 0.9993114447238793

# Check model response (context-aware)
result = client.check_response_ctx(
    prompt="Teach me how to cook",
    response="I can teach you some simple home dishes",
    user_id="user-123"  # Optional user-level risk control
)
print(result.suggest_action)      # Output: pass
print(result.overall_risk_level)  # Output: no_risk

Context-Aware Detection (Core Feature)

# Context-based conversation detection - Core feature
messages = [
    {"role": "user", "content": "I want to learn chemistry"},
    {"role": "assistant", "content": "Chemistry is an interesting subject. What part would you like to learn?"},
    {"role": "user", "content": "Teach me reactions for making explosives"}
]

result = client.check_conversation(messages, user_id="user-123")
print(result.overall_risk_level)
print(result.suggest_action)  # Result based on full conversation context
if result.suggest_answer:
    print(f"Suggested answer: {result.suggest_answer}")

Asynchronous API (Recommended)

import asyncio
from openguardrails import AsyncOpenGuardrails

async def main():
    async with AsyncOpenGuardrails(api_key="your-api-key") as client:
        # Async prompt check
        result = await client.check_prompt("I want to learn Python programming")
        print(result.suggest_action)  # Output: pass
        
        # Async conversation context check
        messages = [
            {"role": "user", "content": "I want to learn chemistry"},
            {"role": "assistant", "content": "Chemistry is an interesting subject. What part would you like to learn?"},
            {"role": "user", "content": "Teach me reactions for making explosives"}
        ]
        result = await client.check_conversation(messages)
        print(result.overall_risk_level)

asyncio.run(main())

Concurrent Processing

import asyncio
from openguardrails import AsyncOpenGuardrails

async def batch_check():
    async with AsyncOpenGuardrails(api_key="your-api-key") as client:
        # Handle multiple requests concurrently
        tasks = [
            client.check_prompt("Content 1"),
            client.check_prompt("Content 2"),
            client.check_prompt("Content 3")
        ]
        results = await asyncio.gather(*tasks)
        
        for i, result in enumerate(results):
            print(f"Content {i+1}: {result.overall_risk_level}")

asyncio.run(batch_check())

Multimodal Image Detection

Supports multimodal detection for image content safety. The system analyzes both text prompt semantics and image semantics for risk.

from openguardrails import OpenGuardrails

client = OpenGuardrails(api_key="your-api-key")

# Check a single local image
result = client.check_prompt_image(
    prompt="Is this image safe?",
    image="/path/to/image.jpg"
)
print(result.overall_risk_level)
print(result.suggest_action)

# Check an image from URL
result = client.check_prompt_image(
    prompt="",  # prompt can be empty
    image="https://example.com/image.jpg"
)

# Check multiple images
images = [
    "/path/to/image1.jpg",
    "https://example.com/image2.jpg",
    "/path/to/image3.png"
]
result = client.check_prompt_images(
    prompt="Are all these images safe?",
    images=images
)
print(result.overall_risk_level)

Async version:

import asyncio
from openguardrails import AsyncOpenGuardrails

async def check_images():
    async with AsyncOpenGuardrails(api_key="your-api-key") as client:
        # Async check for a single image
        result = await client.check_prompt_image(
            prompt="Is this image safe?",
            image="/path/to/image.jpg"
        )
        print(result.overall_risk_level)

        # Async check for multiple images
        images = ["/path/to/image1.jpg", "/path/to/image2.jpg"]
        result = await client.check_prompt_images(
            prompt="Are these images safe?",
            images=images
        )
        print(result.overall_risk_level)

asyncio.run(check_images())

On-Premise Deployment

# Sync client connecting to local deployment
client = OpenGuardrails(
    api_key="your-local-api-key",
    base_url="http://localhost:5000/v1"
)

# Async client connecting to local deployment
async with AsyncOpenGuardrails(
    api_key="your-local-api-key",
    base_url="http://localhost:5000/v1"
) as client:
    result = await client.check_prompt("Test content")

API Reference

OpenGuardrails Class (Synchronous)

Initialization Parameters

  • api_key (str): API key
  • base_url (str): Base API URL, defaults to the cloud endpoint
  • timeout (int): Request timeout, default 30 seconds
  • max_retries (int): Maximum retry count, default 3

Methods

check_prompt(content: str, user_id: Optional[str] = None) -> GuardrailResponse

Checks the safety of a single prompt.

Parameters:

  • content: Text content to be checked
  • user_id: Optional tenant user ID for per-user risk control and auditing

Returns: GuardrailResponse object

check_conversation(messages: List[Message], model: str = "OpenGuardrails-Text", user_id: Optional[str] = None) -> GuardrailResponse

Checks conversation context safety (core feature).

Parameters:

  • messages: List of messages, each containing role and content
  • model: Model name (default: "OpenGuardrails-Text")
  • user_id: Optional tenant user ID

Returns: GuardrailResponse object

AsyncOpenGuardrails Class (Asynchronous)

Same initialization parameters as the synchronous version.

Methods

async check_prompt(content: str) -> GuardrailResponse

Asynchronously checks a single prompt.

async check_conversation(messages: List[Message]) -> GuardrailResponse

Asynchronously checks conversation context safety (core feature).

async health_check() -> Dict[str, Any]

Checks API service health.

async get_models() -> Dict[str, Any]

Retrieves available model list.

async close()

Closes async session (automatically handled with async with).

GuardrailResponse Class

Represents detection results.

Attributes

  • id: Unique request ID
  • result.compliance.risk_level: Compliance risk level
  • result.security.risk_level: Security risk level
  • result.data.risk_level: Data leak risk level (added in v2.4.0)
  • result.data.categories: Detected sensitive data types (added in v2.4.0)
  • overall_risk_level: Overall risk level (none / low / medium / high)
  • suggest_action: Suggested action (pass / block / substitute)
  • suggest_answer: Suggested response (optional, includes redacted content if applicable)
  • score: Confidence score of the results

Helper Methods

  • is_safe: Whether the content is safe
  • is_blocked: Whether the content is blocked
  • has_substitute: Whether a substitute answer is provided
  • all_categories: Get all detected risk categories

Safety Detection Capabilities

Risk Levels

  • High Risk: Sensitive political topics, national image damage, violent crime, prompt attacks
  • Medium Risk: General political topics, harm to minors, illegal acts, sexual content
  • Low Risk: Hate speech, insults, privacy violations, commercial misconduct
  • No Risk: Safe content

Handling Strategies

  • High Risk: Recommend blocking
  • Medium Risk: Recommend substitution with a safe reply
  • Low Risk: Recommend substitution or business-dependent handling
  • No Risk: Recommend pass

Error Handling

Synchronous Error Handling

from openguardrails import OpenGuardrails, AuthenticationError, ValidationError, RateLimitError

try:
    result = client.check_prompt("Test content")
except AuthenticationError:
    print("Invalid API key")
except ValidationError as e:
    print(f"Input validation failed: {e}")
except RateLimitError:
    print("Rate limit exceeded")
except Exception as e:
    print(f"Other error: {e}")

Asynchronous Error Handling

import asyncio
from openguardrails import AsyncOpenGuardrails, AuthenticationError, ValidationError, RateLimitError

async def safe_check():
    try:
        async with AsyncOpenGuardrails(api_key="your-api-key") as client:
            result = await client.check_prompt("Test content")
            return result
    except AuthenticationError:
        print("Invalid API key")
    except ValidationError as e:
        print(f"Input validation failed: {e}")
    except RateLimitError:
        print("Rate limit exceeded")
    except Exception as e:
        print(f"Other error: {e}")

asyncio.run(safe_check())

Development

# Clone the project
git clone https://github.com/openguardrails/openguardrails
cd openguardrails/client

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Code formatting
black openguardrails
isort openguardrails

# Type checking
mypy openguardrails

License

This project is open-sourced under the Apache 2.0 license.

Support


Made with ❤️ by OpenGuardrails


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

openguardrails-3.0.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

openguardrails-3.0.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file openguardrails-3.0.2.tar.gz.

File metadata

  • Download URL: openguardrails-3.0.2.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for openguardrails-3.0.2.tar.gz
Algorithm Hash digest
SHA256 108e2c8cd42f3dd9fb7424abe5d88cec96180ed447957e1f7de30925a0c8e8d4
MD5 d28b7217e840176dfd7421dcd66b993c
BLAKE2b-256 c8286a818307889c19afeabc43dabe43976067acb78ac5e7820c611319af0fb4

See more details on using hashes here.

File details

Details for the file openguardrails-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: openguardrails-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for openguardrails-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5ab9443730ee6cc59eb19f6a205017f1d8313ee70c61aab20abdcd65570f9fcb
MD5 71f450b4e44512c07a0a77860360e85e
BLAKE2b-256 05f27d5b1c71bfecb4bda3dbdf60ba6295cf004b9ac0393bb58d8da6881c5fcd

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