> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://www.comet.com/docs/opik/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://www.comet.com/docs/opik/_mcp/server.

# Validation Tracking for Guardrails AI with Opik

> Start here to integrate Opik into your Guardrails AI-based genai application for validation tracking, compliance monitoring, and guard evaluation.

[Guardrails AI](https://github.com/guardrails-ai/guardrails) is a framework for validating the inputs and outputs

For this guide we will use a simple example that logs guardrails validation steps as traces to Opik, providing them with the validation result tags.

## Account Setup

[Comet](https://www.comet.com/site?from=llm\&utm_source=opik\&utm_medium=colab\&utm_content=guardrails-ai\&utm_campaign=opik) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm\&utm_source=opik\&utm_medium=colab\&utm_content=guardrails-ai\&utm_campaign=opik) and grab your API Key.

> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm\&utm_source=opik\&utm_medium=colab\&utm_content=guardrails-ai\&utm_campaign=opik) for more information.

## Getting Started

### Installation

First, ensure you have both `opik` and `guardrails-ai` installed:

```bash
pip install opik guardrails-ai
```

### Configuring Opik

Configure the Opik Python SDK for your deployment type. See the [Python SDK Configuration guide](/tracing/advanced/sdk_configuration) for detailed instructions on:

* **CLI configuration**: `opik configure`
* **Code configuration**: `opik.configure()`
* **Self-hosted vs Cloud vs Enterprise** setup
* **Configuration files** and environment variables

### Configuring Guardrails AI

In order to use Guardrails AI, you will need to configure the OpenAI API Key. If you are using any other providers, you can replace this with the required API key. You can [find or create your OpenAI API Key in this page](https://platform.openai.com/settings/organization/api-keys).

You can set it as an environment variable:

```bash
export OPENAI_API_KEY="YOUR_API_KEY"
```

Or set it programmatically:

```python
import os
import getpass

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")
```

We will also need to install the guardrails check for politeness from the Guardrails Hub:

```bash
guardrails hub install hub://guardrails/politeness_check
```

## Logging validation traces

In order to log traces to Opik, you will need to call the track the Guard object with `track_guardrails` function.

```python
from guardrails import Guard, OnFailAction
from guardrails.hub import PolitenessCheck

from opik.integrations.guardrails import track_guardrails

politeness_check = PolitenessCheck(
    llm_callable="gpt-3.5-turbo", on_fail=OnFailAction.NOOP
)

guard: Guard = Guard()
if hasattr(guard, "use_many"):
    guard = guard.use_many(politeness_check)
else:
    guard = guard.use(politeness_check)
guard = track_guardrails(guard, project_name="guardrails-integration-example")

guard.validate(
    "Would you be so kind to pass me a cup of tea?",
)
guard.validate(
    "Shut your mouth up and give me the tea.",
)
```

Every validation will now be logged to Opik as a trace

The trace will now be viewable in the Opik platform:

![](https://fdr-prod-docs-files-public.s3.us-east-1.amazonaws.com/opik.docs.buildwithfern.com/f6f1c15ce4580b67bc3cbc507c8d249493d055b8e0243026f325d8ed92f7945f/img/cookbook/guardrails_ai_traces_cookbook.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIA6KXJSKKNFOCF7G4B%2F20260923%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20260923T085302Z&X-Amz-Expires=604800&X-Amz-Signature=1e7292e1c057d61893c0730669e7d9e9c301bbe2e3b1a9a0030ee8c9c2e9544b&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject)