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

# Guardrails

> Learn how to use Langtrace with Guardrails AI for enhanced LLM validation and tracing

# Guardrails

Guardrails AI is a framework for adding validation and safety rails to Large Language Model outputs. Langtrace provides native integration with Guardrails, automatically capturing traces with validator information and metadata to help you monitor and analyze your model's performance and validation results.

## Setup

1. Install Langtrace's SDK and [initialize](/quickstart) the SDK in your code.

2. Install Guardrails SDK and set up your validators according to the [Guardrails documentation](https://www.guardrailsai.com/docs).

3. Initialize Langtrace in your code and implement Guardrails validation:

```python theme={null}
import os
from langtrace_python_sdk import langtrace
from guardrails import Guard, OnFailAction
from guardrails.hub import ProfanityFree, ToxicLanguage

langtrace.init()

guard = Guard()
guard.name = 'ChatBotGuard'
guard.use_many(
    ProfanityFree(on_fail=OnFailAction.EXCEPTION),
    ToxicLanguage(on_fail="exception"),
    on="output"
)

result = guard(
    messages=[{"role": "user", "content": "Hello, how are you?"}],
    model="gpt-4",
    stream=False,
)
```

## Monitoring Validation Results

Once implemented, Langtrace automatically captures:

* Validator execution and results
* Validation metadata
* Model inputs and outputs
* Validation failures and exceptions

View your traces in the Langtrace dashboard:

<img src="https://mintcdn.com/langtraceai-2/m7BVnnhK3Cx2yRxS/images/integrations/guardrails/langtrace-guardrails-trace1.png?fit=max&auto=format&n=m7BVnnhK3Cx2yRxS&q=85&s=621dcd5ccdf0e936b7556527a9a43cea" alt="Langtrace Guardrails Trace" width="3084" height="1780" data-path="images/integrations/guardrails/langtrace-guardrails-trace1.png" />

<img src="https://mintcdn.com/langtraceai-2/m7BVnnhK3Cx2yRxS/images/integrations/guardrails/langtrace-guardrails-trace2.png?fit=max&auto=format&n=m7BVnnhK3Cx2yRxS&q=85&s=2963987140faaf24b359be7f58e56776" alt="Langtrace Guardrails Validation" width="3068" height="1776" data-path="images/integrations/guardrails/langtrace-guardrails-trace2.png" />

## Resources

* [Getting started with Langtrace](/introduction)
* [Guardrails AI Documentation](https://www.guardrailsai.com/docs)
* [Join our Discord Community](https://discord.langtrace.ai/)
