Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fuser-guides%2Fai%2Fguardrails%2Fpii.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fuser-guides%2Fai%2Fguardrails%2Fpii.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# PII

The PII (Personally Identifiable Information) detection guardrail protects your LLM applications from processing or exposing sensitive personal data such as email addresses, phone numbers, credit card numbers, and Social Security Numbers.

## What you need[​](#what-you-need "Direct link to What you need")

* Python 3.10 or higher.
* `cx-guardrails` installed. See [Getting Started with Guardrails](https://coralogix.com/docs/docs/user-guides/ai/guardrails/getting_started/.md).
* Environment variables configured: `CX_GUARDRAILS_TOKEN`, `CX_GUARDRAILS_ENDPOINT`.
* A [Team API key](https://coralogix.com/docs/docs/user-guides/account-management/api-keys/api-keys/.md) with the **AiObservability** role preset, used as `CX_GUARDRAILS_TOKEN`. The AiObservability preset includes `AI-GUARDRAILS:MANAGE` and all other permissions required to use Guardrails.
* The `AI-GUARDRAILS:MANAGE` [permission](https://coralogix.com/docs/docs/user-guides/aaa/access-control/permissions/permissions-list/.md).

## Install the SDK[​](#install-the-sdk "Direct link to Install the SDK")

```
pip install cx-guardrails
```

## Set up environment variables[​](#set-up-environment-variables "Direct link to Set up environment variables")

```
export CX_GUARDRAILS_TOKEN="your-coralogix-guardrails-api-key"

export CX_GUARDRAILS_ENDPOINT="https://api.eu2.coralogix.com.coralogix.com/api/v1/guardrails/guard"

export CX_TOKEN="your-coralogix-send-your-data-key"

export CX_ENDPOINT="https://your-domain.coralogix.com"



# Optional: Application metadata for observability

export CX_APPLICATION_NAME="my-app"

export CX_SUBSYSTEM_NAME="my-subsystem"
```

## Set up observability[​](#set-up-observability "Direct link to Set up observability")

To send guardrail spans to AI Center, set up OpenTelemetry trace export. For the full overview, see [OpenTelemetry integration for AI Center](https://coralogix.com/docs/docs/user-guides/ai/otel-integration/.md).

Install the OpenTelemetry packages:

```
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc
```

Export the OTLP environment variables:

```
export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingress.eu2.coralogix.com:443"

export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer <your-api-key>"

export OTEL_SERVICE_NAME="my-ai-service"

export OTEL_RESOURCE_ATTRIBUTES="cx.application.name=my-app,cx.subsystem.name=my-subsystem"

export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true

export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental
```

Initialize the tracer provider in your application before any guardrail or LLM calls:

```
from opentelemetry import trace

from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

from opentelemetry.sdk.resources import Resource

from opentelemetry.sdk.trace import TracerProvider

from opentelemetry.sdk.trace.export import BatchSpanProcessor





def configure_otel() -> TracerProvider:

    resource = Resource.create()

    provider = TracerProvider(resource=resource)

    provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))

    trace.set_tracer_provider(provider)

    return provider
```

## Available PII categories[​](#available-pii-categories "Direct link to Available PII categories")

| Category      | Enum value                  | Description                |
| ------------- | --------------------------- | -------------------------- |
| Email Address | `PIICategory.EMAIL_ADDRESS` | Email addresses            |
| Phone Number  | `PIICategory.PHONE_NUMBER`  | Phone numbers              |
| Credit Card   | `PIICategory.CREDIT_CARD`   | Credit/debit card numbers  |
| US SSN        | `PIICategory.US_SSN`        | US Social Security Numbers |

## Usage[​](#usage "Direct link to Usage")

```
import asyncio

from cx_guardrails import Guardrails, PII, GuardrailsTriggered



async def main():

    guardrails = Guardrails()

    async with guardrails.guarded_session():

        try:

            await guardrails.guard_prompt(

                prompt="My email is john.doe@example.com",

                guardrails=[PII()],

            )

            print("No PII detected")

        except GuardrailsTriggered as e:

            print(f"PII detected: {e}")



asyncio.run(main())
```

## Configuration options[​](#configuration-options "Direct link to Configuration options")

### Specific categories[​](#specific-categories "Direct link to Specific categories")

Detect only specific PII types:

```
await guardrails.guard_prompt(

    prompt=user_input,

    guardrails=[PII(categories=[PIICategory.EMAIL_ADDRESS, PIICategory.CREDIT_CARD])],

)
```

### Custom threshold[​](#custom-threshold "Direct link to Custom threshold")

Adjust detection sensitivity (0.0 to 1.0, default 0.7):

```
# Lower threshold — more sensitive

await guardrails.guard_prompt(

    prompt=user_input,

    guardrails=[PII(threshold=0.5)],

)



# Higher threshold — less sensitive

await guardrails.guard_prompt(

    prompt=user_input,

    guardrails=[PII(threshold=0.9)],

)
```

**Threshold:** Defines the value from which a guardrail action is triggered. When the threshold is met or exceeded, the guardrail action is executed, returned through the API, and the system marks the event as an issue.

## Next steps[​](#next-steps "Direct link to Next steps")

Detect and block toxic, harmful, or offensive content with [Toxicity](https://coralogix.com/docs/docs/user-guides/ai/guardrails/toxicity/.md).
