Skip to main content

A Python SDK for interacting with the Guardrails API.

Project description

README for Guardrails SDK

Guardrails SDK

The Guardrails SDK is a Python library designed to facilitate interaction with the Guardrails API. It provides a simple and intuitive interface for developers to integrate safety mechanisms and quality controls into their applications powered by Large Language Models (LLMs).

Features

  • Content Validation: Validate user-generated content against specified guardrails to ensure safety and compliance.
  • PII Detection & Masking: Automatically detect and mask Personally Identifiable Information (PII) in text to protect user privacy.
  • Toxicity Detection: Identify and flag toxic language in user inputs to maintain a safe environment.
  • Compitator mention: Detects user-specified competitor-related words or phrases in the text.

Installation

You can install the Guardrails SDK using pip. Run the following command:

pip install guardrails-sdk

Sample request

🧾 Field Descriptions

Field Type Description
content string The raw input text that you want to send to LLM.
guardrails List[string] List of built-in or custom entity types to apply masking on. Examples: "EMAIL_ADDRESS", "PHONE_NUMBER", "CUSTOM_ACCOUNT_NUMBER" etc.
treshold float The minimum confidence score (0.0 - 1.0) to consider a match valid. Defaults to 0.5.
custom_entities List[Dict] A list of custom entity matchers defined using regular expressions. Each entry describes a user-defined entity.

🔧 custom_entities Object Fields

Subfield Required Description
entity_name ✅ Yes A unique name/label for your custom entity. Should also be listed in the guardrails array to take effect.
regex ✅ Yes The regular expression pattern used to detect the custom entity in the input text.
score ❌ No Optional confidence score (between 0.0 and 1.0). If not provided, default logic will be used.

Sample request

{
  "content": "Hello this is viswateja from v@v.com and LC-123456 with 1234-5678-9012",
  "guardrails": ["EMAIL_ADDRESS", "CUSTOM_ACCOUNT_NUMBER", "LOYALTY_CARD"],
  "treshold": 0.5,
  "custom_entities": [
    {
      "entity_name": "CUSTOM_ACCOUNT_NUMBER",
      "regex": "\\b\\d{4}-\\d{4}-\\d{4}\\b",
      "score": 0.9
    },
    {
      "entity_name": "LOYALTY_CARD",
      "regex": "LC-\\d{6}"
    }
  ]
}

Usage

Here’s a quick example of how to use the Guardrails SDK: The below code snippet helps to run all the guard rails provided by SDK

from guardrails_sdk import GuardrailsClient, TransformRequest
import asyncio

req = {
    "content": "Hello this is viswateja from v@v.com and LC-123456 with 1234-5678-9012",
    "guardrails": [
        "EMAIL_ADDRESS","CUSTOM_ACCOUNT_NUMBER","LOYALTY_CARD"
    ],
    "treshold": 0.5,
    "custom_entities": [
        {
            "entity_name": "CUSTOM_ACCOUNT_NUMBER",
            "regex": "\\b\\d{4}-\\d{4}-\\d{4}\\b",
            "score": 0.9
        },
        {
            "entity_name": "LOYALTY_CARD",
            "regex": "LC-\\d{6}"
        }
    ]
}


async def main():
    client = GuardrailsClient()
    request = TransformRequest(**req)
    res = await client.run_all_guardrails(request)
    print(res)

if __name__ == "__main__":
    asyncio.run(main())

📘 API Reference

working examples can be found here https://github.com/tejadata/guardrails/tree/main/examples

GuardrailsClient

A class to run various content safety guardrails such as PII masking, toxicity detection, and prompt injection classification.


validate_content(request: ToxiRequest) -> Dict

Detects toxic content in the input text based on the given threshold.

🔸 Parameters:
  • request: ToxiRequest
    • content (str): Input text to analyze.
    • treshold (float, optional): Confidence threshold for toxicity detection. Default is 0.5.
🔸 Returns:
  • Dict: Result of toxicity analysis (e.g., toxicity score and classification).

transform_content(request: TransformRequest) -> Dict

Analyzes and masks PII or other custom entities in the text based on the specified guardrails.

🔸 Parameters:
  • request: TransformRequest
    • content (str): Input text to scan and mask.
    • guardrails (List[str]): List of built-in or custom entity types to mask (e.g., "EMAIL_ADDRESS", "PHONE_NUMBER").
    • treshold (float, optional): Confidence threshold for entity detection. Default is 0.5.
    • custom_entities (List[Dict], optional): Custom entity definitions using regex patterns.
🔸 Returns:
  • Dict: Original and masked text, along with metadata for detected entities.

prompt_injection(request: Prompt) -> Dict

Classifies the input text to detect prompt injection attacks in LLM interactions.

🔸 Parameters:
  • request: Prompt
    • content (str): Prompt input to classify.
🔸 Returns:
  • Dict: Classification label and confidence score for whether the prompt is a potential injection.

run_all_guardrails(request: TransformRequest) -> Dict

Executes all guardrails (PII masking, toxicity detection, and prompt injection detection) in parallel.

🔸 Parameters:
  • request: TransformRequest (same as above)
🔸 Returns:
  • Dict: Combined output for:
    • "pii": Masked content and entity info
    • "toxicity": Toxicity classification
    • "prompt_injection": Prompt injection status

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any enhancements or bug fixes.

License

This project is licensed under the terms of the Apache License 2.0. See the LICENSE file for details.

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

guardrails_sdk-0.2.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

guardrails_sdk-0.2.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file guardrails_sdk-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for guardrails_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 606abc3083896cf30e5273b8a169a2ca600b86f34243be8932c5db0c0a9e9472
MD5 90f05e431f018dab0236fc66a1ea1322
BLAKE2b-256 eb4058e7413eb14f1e07a303372a0dcd7593af4866339dd01fe10fd715f8abd9

See more details on using hashes here.

File details

Details for the file guardrails_sdk-0.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for guardrails_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf05023acce44202e8f8bc66d33ae0c7f674f6837c03dc9612b57a868427aa45
MD5 894333277b4d4467a8163e1d6941a616
BLAKE2b-256 47d487aa253478cbde8ef6b7451a24eb0d6e086c3d856ba238da351108b79bc2

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