> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openlayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Learn about guardrails in Openlayer

Guardrails are **runtime checks** that help you enforce constraints on your AI system’s
inputs and outputs.

## Guardrails vs. Tests

Guardrails complement [tests](/tests/overview), in particular in [monitoring mode](/monitoring/overview).

While your Openlayer **tests** run continuously on top of your live data and trigger a notification
in case of failure, **guardrails** validate inputs and outputs in real time and block or modify them if
they don't meet your constraints.

Together, they give you both **proactive coverage** (through tests)
and **reactive protection** (through guardrails).

<Tip>
  Guardrails are not a replacement for tests. They are a complementary tool to
  help you ensure that your AI system is safe and compliant. Furthermore, it is
  worth noting that guardrails introduce latency in your system, as they need to
  validate inputs and outputs in real time.
</Tip>

## Guardrails library

Openlayer has a Python library for guardrails. You can use one of the
built-in guardrails (such as the PII or prompt injection), or implement custom guardrails
following the interface defined in the `BaseGuardrail` class.

You can install it with:

```bash theme={null}
pip install openlayer-guardrails
```

Some guardrails require additional dependencies. Install them using the extras for the specific guardrail you need:

| Guardrail                  | Extra              | Install command                                      |
| -------------------------- | ------------------ | ---------------------------------------------------- |
| `PIIGuardrail`             | `pii`              | `pip install openlayer-guardrails[pii]`              |
| `PromptInjectionGuardrail` | `prompt-injection` | `pip install openlayer-guardrails[prompt-injection]` |

If you try to use a guardrail without its required dependencies, you'll see an error message with the exact install command needed.

### With Openlayer tracing

Guardrails work well with [Openlayer tracing](/monitoring/tracing). In this case,
you can pass the desired guardrails to the `trace` decorator, and they will be applied
to the inputs and outputs of the traced function.

<Note>
  **Prerequisites**: Besides the `openlayer-guardrails`, you need to have the
  `openlayer` library installed and have [tracing correctly configured in your
  project](/monitoring/tracing) to run the example below.
</Note>

```python theme={null}
from openlayer_guardrails import PIIGuardrail
from openlayer.lib.tracing import trace

# Create the guardrail object with the desired configuration
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]"
```

In this case, the guardrails are automatically traced as well, and you can see them in the
Openlayer platform [if tracing is correctly configured](/monitoring/tracing).

<img width="700" style={{ borderRadius: "0.5rem" }} src="https://mintcdn.com/openlayer-44/mbihm4ycaRdhG6gK/images/documentation/guardrail_trace.png?fit=max&auto=format&n=mbihm4ycaRdhG6gK&q=85&s=9e49b03c5c2ad11187d7c6050aeb7fdd" alt="Guardrails trace" data-path="images/documentation/guardrail_trace.png" />

### Standalone usage

Openlayer guardrails can also be used standalone, without tracing. Here's an example:

```python theme={null}
# Import the guardrail
from openlayer_guardrails import PIIGuardrail

# Create the guardrail object with the desired configuration
pii_guard = PIIGuardrail(
    block_entities={"CREDIT_CARD", "US_SSN"},
    redact_entities={"EMAIL_ADDRESS", "PHONE_NUMBER"}
)

# Call the guardrail on the data
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}")
```
