Frameworks & Platforms
Guardrails
Validate TokenLab model outputs with Guardrails
Overview
Configure the OpenAI SDK with the TokenLab Base URL, request JSON, then validate the returned text with Guard.parse.
Installation
pip install guardrails-ai openaiEnvironment
export TOKENLAB_API_KEY="sk-your-tokenlab-key"Example
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",
)
response = client.chat.completions.create(
model="claude-sonnet-5",
response_format={"type": "json_object"},
messages=[{
"role": "user",
"content": "Suggest a pet. Return only JSON with string fields pet_type and name.",
}],
)
outcome = guard.parse(response.choices[0].message.content)
print(outcome.validated_output)Endpoint Notes
Guardrails focuses on validation around the LLM call. Use the OpenAI-compatible path for chat-completions flows, or pass a custom callable if your application needs a native TokenLab endpoint.