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

# Guardrails

> 使用 Guardrails 验证 TokenLab 模型输出

## 概述

Guardrails 可以封装任何与其 LLM API 接口兼容的可调用对象（callable）。对于 TokenLab，请使用 TokenLab 的 base URL 配置 OpenAI SDK，并将聊天补全（chat completions）的可调用对象传递给您的 guard。

<Note>
  **类型**: 验证与结构化输出框架

  **主要路径**: 兼容 OpenAI 的 Chat Completions

  **支持置信度**: 支持兼容 OpenAI 的路径
</Note>

## 环境

```bash theme={null}
export TOKENLAB_API_KEY="sk-your-tokenlab-key"
```

## 示例

```python theme={null}
import os

from guardrails import Guard
from openai import OpenAI
from pydantic import BaseModel, Field


class Pet(BaseModel):
    pet_type: str = Field(description="Species of pet")
    name: str = Field(description="A unique pet name")


guard = Guard.for_pydantic(output_class=Pet)

client = OpenAI(
    api_key=os.environ["TOKENLAB_API_KEY"],
    base_url="https://api.tokenlab.sh/v1",
)

raw_output, validated_output, *rest = guard(
    llm_api=client.chat.completions.create,
    model="claude-sonnet-5",
    messages=[{"role": "user", "content": "What kind of pet should I get?"}],
)
```

## 端点说明

Guardrails 专注于围绕 LLM 调用进行验证。对于聊天补全流程，请使用兼容 OpenAI 的路径；如果您的应用程序需要原生的 TokenLab 端点，请传递自定义的可调用对象。
