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.

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

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.1.2.tar.gz (8.5 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.1.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for guardrails_sdk-0.1.2.tar.gz
Algorithm Hash digest
SHA256 40787d864590b0ecee7c9c2d791a07fe215cdf062d547402fd0b9dbb44c51502
MD5 244bd770588b2cdf54b62652bda56cd1
BLAKE2b-256 05c289ac053a4bc738bfc5164485044d6466e7f290084562cf964c608bb98aef

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for guardrails_sdk-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fdbb19aedd859fbfd491613c25d9d715fcd651146093d4dad39b18cc437d52e4
MD5 15d75f04700d6aff5a1d58116f65a22f
BLAKE2b-256 a609fa1fccd93bf353a63f4e991c0b1f0312e5b655b1278ca22872e7fc58e223

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