Openlayer Guardrails
Open source guardrail implementations that work with Openlayer tracing.
Installation
pip install openlayer-guardrails
Usage
Standalone Usage
from openlayer_guardrails import PIIGuardrail
# Create guardrail
pii_guard = PIIGuardrail(
block_entities={"CREDIT_CARD", "US_SSN"},
redact_entities={"EMAIL_ADDRESS", "PHONE_NUMBER"}
)
# Check inputs manually
data = {"message": "My email is john@example.com and SSN is 123-45-6789"}
result = pii_guard.check_input(data)
if result.action.value == "block":
print(f"Blocked: {result.reason}")
elif result.action.value == "modify":
print(f"Modified data: {result.modified_data}")
With Openlayer Tracing
from openlayer_guardrails import PIIGuardrail
from openlayer.lib.tracing import trace
# Create guardrail
pii_guard = PIIGuardrail()
# Apply to traced functions
@trace(guardrails=[pii_guard])
def process_user_data(user_input: str):
return f"Processed: {user_input}"
# PII is automatically handled
result = process_user_data("My email is john@example.com")
# Output: "Processed: My email is [EMAIL-REDACTED]"
Toxicity Guardrail (Brazilian Portuguese)
Detects toxic content in Brazilian Portuguese using the ToxiGuardrailPT model.
pip install 'openlayer-guardrails[toxicity]'
from openlayer_guardrails import ToxicityPTGuardrail
# Create guardrail (default threshold=0.0; positive scores = safe, negative = toxic)
toxicity_guard = ToxicityPTGuardrail()
# Check inputs
result = toxicity_guard.check_input({"message": "Você é um idiota!"})
print(result.action) # GuardrailAction.BLOCK
# Check outputs with contextual scoring (sentence-pair encoding)
result = toxicity_guard.check_output(
output="Claro, aqui está a informação solicitada.",
inputs={"prompt": "Me ajude com meu trabalho."},
)
print(result.action) # GuardrailAction.ALLOW
Toxicity Guardrail (English)
Detects toxic content in English across six categories using unitary/toxic-bert.
pip install 'openlayer-guardrails[toxicity]'
from openlayer_guardrails import ToxicityENGuardrail
# Create guardrail (default threshold=0.5)
toxicity_guard = ToxicityENGuardrail()
# Check inputs
result = toxicity_guard.check_input({"message": "You are terrible and should die"})
print(result.action) # GuardrailAction.BLOCK
print(result.metadata["triggered_categories"])
# e.g. {'toxic': 0.98, 'severe_toxic': 0.72, 'insult': 0.89, 'threat': 0.81}
# Monitor only specific categories
guard = ToxicityENGuardrail(categories={"threat", "severe_toxic"})
Handling long texts
By default, all guardrails truncate inputs to 512 tokens for fast inference.
To evaluate the full text, enable chunking mode by setting max_length=None:
guard = ToxicityPTGuardrail(max_length=None) # or ToxicityENGuardrail(max_length=None)
In chunking mode, long texts are split into overlapping 512-token windows and each window is scored independently. The most toxic score across all windows is used. Latency scales linearly with the number of chunks.
Downloading models from a mirror or offline
The Prompt Injection and Toxicity guardrails download their models from the Hugging Face Hub on first use. If the Hub is not reachable from your environment, use one of the options below.
Use a mirror. The download goes through huggingface_hub, which reads the
standard Hugging Face environment variables. Point it at your mirror (for
example a Hugging Face remote repository in Artifactory or Nexus) and give it
the mirror's key:
export HF_ENDPOINT=https://artifactory.example.com/api/huggingfaceml/hf-remote
export HF_TOKEN=<your-mirror-key>
Set these before Python imports transformers: the library reads them once,
at import time. You can also pass the key per guardrail with hf_token=....
| Variable | Purpose |
|---|---|
HF_ENDPOINT |
Base URL of the Hub or of your mirror |
HF_TOKEN |
Token for the Hub, or the key of your mirror |
HF_HOME / HF_HUB_CACHE |
Where downloaded models are cached |
HF_HUB_OFFLINE=1 |
Load from the cache only, never call the network |
Use a different repo or a local copy. Every Hugging Face guardrail accepts
model_id, which takes a repo ID or a local directory. Use it when your mirror
stores the model under another name, or when you copy the model files into the
image yourself. The model must be a copy of the default one (same architecture
and labels).
guard = ToxicityENGuardrail(model_id="/models/toxic-bert")
Pin a version. Pass revision (a branch, tag or commit SHA) to make sure
the weights do not change between deployments:
guard = ToxicityPTGuardrail(revision="<commit-sha>")
spaCy models (PIIGuardrail). The PII guardrails use a spaCy model, which
is a pip package, not a Hugging Face model, so HF_ENDPOINT does not apply.
Install the model from your internal package index or from a wheel on your
mirror, then select it with spacy_model=... if it is not one of
en_core_web_lg, en_core_web_md or en_core_web_sm:
pip install --index-url https://pypi.example.com/simple en_core_web_sm
# or
pip install https://mirror.example.com/en_core_web_sm-3.8.0-py3-none-any.whl
Model Limitations
Prompt Injection Guardrail
| Property | Value |
|---|---|
| Model | meta-llama/Llama-Prompt-Guard-2-86M |
| Max tokens | 512 |
| Language | English |
| Parameters | 86M |
| Scope | Input-only (outputs are not checked) |
Texts longer than 512 tokens are truncated. Only the first 512 tokens are evaluated.
Toxicity Guardrail (PT-BR)
| Property | Value |
|---|---|
| Model | nicholasKluge/ToxiGuardrailPT |
| Max tokens | 512 |
| Language | Brazilian Portuguese |
| Parameters | 109M |
| Architecture | BERTimbau (bert-base-portuguese-cased) |
| Output type | Single scalar reward score (positive = safe, negative = toxic) |
| Reported accuracy | 70.36% (hatecheck-portuguese), 74.04% (told-br) |
| Scope | Input and output (output uses sentence-pair encoding for context) |
By default, texts longer than 512 tokens are truncated. Set max_length=None to enable chunking for full-text coverage. The model was trained on Brazilian Portuguese data and may not generalize well to European Portuguese or other languages.
Toxicity Guardrail (EN)
| Property | Value |
|---|---|
| Model | unitary/toxic-bert |
| Max tokens | 512 (chunking available via max_length=None) |
| Language | English |
| Parameters | 110M |
| Architecture | BERT (bert-base-uncased) |
| Output type | Multi-label probabilities across 6 categories |
| Categories | toxic, severe_toxic, obscene, threat, insult, identity_hate |
| Reported AUC | 0.98636 (Jigsaw Toxic Comment Challenge) |
| Scope | Input and output |
By default, texts longer than 512 tokens are truncated. Set max_length=None to enable chunking for full-text coverage. The model was trained on English data.
Metadata
Release files for openlayer-guardrails 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openlayer_guardrails-2.1.0.tar.gz | 205.8 kB | Without provenance | Details for openlayer_guardrails-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openlayer_guardrails-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for openlayer_guardrails-2.1.0-py3-none-any.whl |
Total release size: 261.2 kB
Release files / openlayer_guardrails-2.1.0.tar.gz
| Download URL | openlayer_guardrails-2.1.0.tar.gz |
|---|---|
| Size | 205.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08d4cc51f0303820e16b6192215aec7f8d3aad9826fa59bd7be535e4500c5f31
|
|
BLAKE2b-256 checksum How to use checksums |
7d500b46c85e712909fe9cd033a2d2e6ba4eb795232ff513045ca7fcc9d7cc29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / openlayer_guardrails-2.1.0-py3-none-any.whl
| Download URL | openlayer_guardrails-2.1.0-py3-none-any.whl |
|---|---|
| Size | 55.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a22eff80de552c7fd458dae11d4540dfab13325ca17113d23684af4c5ed32f11
|
|
BLAKE2b-256 checksum How to use checksums |
7431118f2c111f18a9cca23255f67073625d8495512b0df60352186704cd542c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for openlayer-guardrails 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.