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

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Guardrails Code Integration

> Add guardrails to your LLM application to block harmful content in real-time.

> [!TIP]
>
> **Let your agent set this up.** [Copy the evaluations prompt](https://langwatch.ai/docs/skills/directory.md) into your coding agent to get started automatically.

Guardrails run LangWatch evaluators synchronously from your code, through the LangWatch SDK, and return a result you can act on immediately.

## Basic Usage

The key difference between guardrails and regular evaluations is the `as_guardrail=True` parameter, which tells LangWatch this evaluation should block if it fails.

> [tab] Python
>
> ```python theme={null}
> import langwatch
>
> @langwatch.trace()
> def my_llm_app(user_input):
>     # Run guardrail check
>     guardrail = langwatch.evaluation.evaluate(
>         "azure/jailbreak",
>         name="Jailbreak Detection",
>         as_guardrail=True,
>         data={"input": user_input},
>     )
>
>     # Check result and handle failure
>     if not guardrail.passed:
>         return "I'm sorry, I can't help with that request."
>
>     # Continue with normal processing
>     response = call_llm(user_input)
>     return response
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { LangWatch } from "langwatch";
>
> const langwatch = new LangWatch();
>
> async function myLLMApp(userInput: string): Promise<string> {
>   // Run guardrail check
>   const guardrail = await langwatch.evaluations.evaluate("azure/jailbreak", {
>     name: "Jailbreak Detection",
>     asGuardrail: true,
>     data: { input: userInput },
>   });
>
>   // Check result and handle failure
>   if (!guardrail.passed) {
>     return "I'm sorry, I can't help with that request.";
>   }
>
>   // Continue with normal processing
>   const response = await callLLM(userInput);
>   return response;
> }
> ```

## Guardrail Response Structure

When you run a guardrail, you get back a result object with these fields:

| Field | Type | Description |
| - | - | - |
| `passed` | boolean | Whether the guardrail passed (true = safe, false = blocked) |
| `score` | number | Numeric score from 0-1 (if applicable) |
| `label` | string | Category label (if applicable) |
| `details` | string | Explanation of the result |

```python theme={null}
import langwatch

guardrail = langwatch.evaluation.evaluate(
    "presidio/pii_detection",
    name="PII Check",
    as_guardrail=True,
    data={"output": response},
)

print(f"Passed: {guardrail.passed}")
print(f"Score: {guardrail.score}")
print(f"Details: {guardrail.details}")
```

## Input vs Output Guardrails

### Input Guardrails

Check user input **before** calling your LLM:

```python theme={null}
import langwatch

@langwatch.trace()
def chatbot(user_input):
    # Check input for jailbreak attempts
    input_check = langwatch.evaluation.evaluate(
        "azure/jailbreak",
        name="Input Safety Check",
        as_guardrail=True,
        data={"input": user_input},
    )
    
    if not input_check.passed:
        return "I can't process that request."
    
    # Safe to proceed
    return call_llm(user_input)
```

### Output Guardrails

Check LLM response **before** returning to user:

```python theme={null}
import langwatch

@langwatch.trace()
def chatbot(user_input):
    response = call_llm(user_input)
    
    # Check output for PII before returning
    output_check = langwatch.evaluation.evaluate(
        "presidio/pii_detection",
        name="Output PII Check",
        as_guardrail=True,
        data={"output": response},
    )
    
    if not output_check.passed:
        return "I apologize, but I cannot share that information."
    
    return response
```

### Combined Guardrails

Use both to check the input and the output:

```python theme={null}
import langwatch

@langwatch.trace()
def chatbot(user_input):
    # Input guardrails
    jailbreak = langwatch.evaluation.evaluate(
        "azure/jailbreak",
        name="Jailbreak Detection",
        as_guardrail=True,
        data={"input": user_input},
    )
    if not jailbreak.passed:
        return "I can't help with that request."
    
    moderation = langwatch.evaluation.evaluate(
        "openai/moderation",
        name="Content Moderation",
        as_guardrail=True,
        data={"input": user_input},
    )
    if not moderation.passed:
        return "Please keep our conversation appropriate."
    
    # Generate response
    response = call_llm(user_input)
    
    # Output guardrails
    pii = langwatch.evaluation.evaluate(
        "presidio/pii_detection",
        name="PII Check",
        as_guardrail=True,
        data={"output": response},
    )
    if not pii.passed:
        return "I cannot share personal information."
    
    return response
```

## Async Guardrails

For async applications, use `async_evaluate`:

> [tab] Python
>
> ```python theme={null}
> import langwatch
>
> @langwatch.trace()
> async def async_chatbot(user_input):
>     # Async guardrail check
>     guardrail = await langwatch.evaluation.async_evaluate(
>         "azure/jailbreak",
>         name="Jailbreak Detection",
>         as_guardrail=True,
>         data={"input": user_input},
>     )
>
>     if not guardrail.passed:
>         return "I can't help with that request."
>
>     response = await async_call_llm(user_input)
>     return response
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { LangWatch } from "langwatch";
>
> const langwatch = new LangWatch();
>
> // TypeScript is async by default
> const guardrail = await langwatch.evaluations.evaluate("azure/jailbreak", {
>   name: "Jailbreak Detection",
>   asGuardrail: true,
>   data: { input: userInput },
> });
> ```

## Parallel Guardrails

Run multiple guardrails in parallel to reduce latency:

> [tab] Python
>
> ```python theme={null}
> import asyncio
> import langwatch
>
> @langwatch.trace()
> async def chatbot_with_parallel_guards(user_input):
>     # Run multiple guardrails in parallel
>     jailbreak, moderation, off_topic = await asyncio.gather(
>         langwatch.evaluation.async_evaluate(
>             "azure/jailbreak",
>             name="Jailbreak Detection",
>             as_guardrail=True,
>             data={"input": user_input},
>         ),
>         langwatch.evaluation.async_evaluate(
>             "openai/moderation",
>             name="Content Moderation",
>             as_guardrail=True,
>             data={"input": user_input},
>         ),
>         langwatch.evaluation.async_evaluate(
>             "langevals/off_topic",
>             name="Off Topic Check",
>             as_guardrail=True,
>             data={"input": user_input},
>             settings={"allowed_topics": ["customer support", "product questions"]},
>         ),
>     )
>
>     # Check all results
>     if not jailbreak.passed:
>         return "I can't process that request."
>     if not moderation.passed:
>         return "Please keep our conversation appropriate."
>     if not off_topic.passed:
>         return "I can only help with customer support questions."
>
>     return await async_call_llm(user_input)
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { LangWatch } from "langwatch";
>
> const langwatch = new LangWatch();
>
> async function chatbotWithParallelGuards(userInput: string): Promise<string> {
>   // Run multiple guardrails in parallel
>   const [jailbreak, moderation, offTopic] = await Promise.all([
>     langwatch.evaluations.evaluate("azure/jailbreak", {
>       name: "Jailbreak Detection",
>       asGuardrail: true,
>       data: { input: userInput },
>     }),
>     langwatch.evaluations.evaluate("openai/moderation", {
>       name: "Content Moderation",
>       asGuardrail: true,
>       data: { input: userInput },
>     }),
>     langwatch.evaluations.evaluate("langevals/off_topic", {
>       name: "Off Topic Check",
>       asGuardrail: true,
>       data: { input: userInput },
>       settings: { allowed_topics: ["customer support", "product questions"] },
>     }),
>   ]);
>
>   // Check all results
>   if (!jailbreak.passed) {
>     return "I can't process that request.";
>   }
>   if (!moderation.passed) {
>     return "Please keep our conversation appropriate.";
>   }
>   if (!offTopic.passed) {
>     return "I can only help with customer support questions.";
>   }
>
>   return await callLLM(userInput);
> }
> ```

## Custom Guardrails with LLM-as-Judge

Create custom guardrails using LLM-as-Judge evaluators:

```python theme={null}
import langwatch

@langwatch.trace()
def chatbot_with_custom_guardrail(user_input):
    # Custom policy check using LLM-as-Judge
    policy_check = langwatch.evaluation.evaluate(
        "langevals/llm_boolean",
        name="Company Policy Check",
        as_guardrail=True,
        data={"input": user_input},
        settings={
            "prompt": """Evaluate if this user message violates our company policy.
            
Policy rules:
- No requests for financial advice
- No requests for medical diagnosis
- No requests about competitors

User message: {input}

Does this message violate our policy? Answer only 'true' or 'false'.""",
            "model": "openai/gpt-6-luna",
        },
    )
    
    if not policy_check.passed:
        return "I'm not able to help with that topic. Please contact our support team."
    
    return call_llm(user_input)
```

## Error Handling

Always handle potential errors in guardrail execution:

```python theme={null}
import langwatch

@langwatch.trace()
def chatbot_with_error_handling(user_input):
    try:
        guardrail = langwatch.evaluation.evaluate(
            "azure/jailbreak",
            name="Jailbreak Detection",
            as_guardrail=True,
            data={"input": user_input},
        )
        
        if not guardrail.passed:
            return "I can't help with that request."
            
    except Exception as e:
        # Log the error but don't block the user
        print(f"Guardrail error: {e}")
        # Optionally: fail open or fail closed based on your security needs
        # return "Service temporarily unavailable"  # Fail closed
        pass  # Fail open - continue without guardrail
    
    return call_llm(user_input)
```

## Configuring Evaluator Settings

Many evaluators accept custom settings:

```python theme={null}
import langwatch

# Configure PII detection to only flag specific entity types
pii_check = langwatch.evaluation.evaluate(
    "presidio/pii_detection",
    name="PII Check",
    as_guardrail=True,
    data={"output": response},
    settings={
        "entities_to_detect": ["PERSON", "EMAIL_ADDRESS", "PHONE_NUMBER"],
        "score_threshold": 0.7,
    },
)

# Configure competitor blocklist
competitor_check = langwatch.evaluation.evaluate(
    "langevals/competitor_blocklist",
    name="Competitor Check",
    as_guardrail=True,
    data={"output": response},
    settings={
        "competitors": ["CompetitorA", "CompetitorB", "CompetitorC"],
    },
)
```

## Next Steps

> [card] [Guardrails Overview](https://langwatch.ai/docs/evaluations/guardrails/overview.md)
>
> Learn about guardrails concepts and best practices.

> [card] [Evaluators List](https://langwatch.ai/docs/evaluations/evaluators/list.md)
>
> Browse all available evaluators for guardrails.

> [card] [Python SDK Reference](https://langwatch.ai/docs/integration/python/reference.md)
>
> Full API documentation for the Python SDK.

> [card] [TypeScript SDK Reference](https://langwatch.ai/docs/integration/typescript/reference.md)
>
> Full API documentation for the TypeScript SDK.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.