Skip to content

Pydantic AI

Pydantic AI provides typed prompts and structured outputs. Wrap the OpenAI client before passing it to Pydantic AI to score fully rendered prompts and model outputs.

import os
from openai import OpenAI
from pydantic_ai import Agent
# Create OpenAI client with Glitch
client = OpenAI(
api_key=os.environ["GLITCH_API_KEY"], # Your Glitch API key
base_url="https://api.golabrat.ai/v1", # Glitch API endpoint
)
# Pass the secured client to Pydantic AI
agent = Agent(
'openai:gpt-4',
system_prompt='You are a helpful assistant.',
result_type=str,
client=client, # Use the Glitch-wrapped client
)
import os
from openai import OpenAI
from pydantic_ai import Agent
# Configure Glitch-wrapped client
client = OpenAI(
api_key=os.environ["GLITCH_API_KEY"],
base_url="https://api.golabrat.ai/v1",
)
# Create agent with secured client
agent = Agent(
'openai:gpt-4',
system_prompt='You are a helpful assistant.',
result_type=str,
client=client,
)
# Use the agent - all requests are automatically secured
result = await agent.run('What is the capital of France?')
print(result.data) # "Paris"

Handle security blocks gracefully:

from openai import OpenAI, APIStatusError
import os
client = OpenAI(
api_key=os.environ["GLITCH_API_KEY"],
base_url="https://api.golabrat.ai/v1",
)
try:
result = await agent.run(user_input)
return result.data
except APIStatusError as e:
if e.status_code == 403:
# Security block - handle gracefully
return "I can't process that request."
raise