> ## 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 呼叫周邊的驗證。請針對 chat-completions 流程使用相容 OpenAI 的路徑，或者如果您的應用程式需要原生的 TokenLab 端點，請傳遞自訂的可呼叫物件。
