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.
Configuration
Section titled “Configuration”import osfrom openai import OpenAIfrom pydantic_ai import Agent
# Create OpenAI client with Glitchclient = 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 AIagent = Agent( 'openai:gpt-4', system_prompt='You are a helpful assistant.', result_type=str, client=client, # Use the Glitch-wrapped client)Basic Example
Section titled “Basic Example”import osfrom openai import OpenAIfrom pydantic_ai import Agent
# Configure Glitch-wrapped clientclient = OpenAI( api_key=os.environ["GLITCH_API_KEY"], base_url="https://api.golabrat.ai/v1",)
# Create agent with secured clientagent = Agent( 'openai:gpt-4', system_prompt='You are a helpful assistant.', result_type=str, client=client,)
# Use the agent - all requests are automatically securedresult = await agent.run('What is the capital of France?')print(result.data) # "Paris"Error Handling
Section titled “Error Handling”Handle security blocks gracefully:
from openai import OpenAI, APIStatusErrorimport 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.dataexcept APIStatusError as e: if e.status_code == 403: # Security block - handle gracefully return "I can't process that request." raiseNext Steps
Section titled “Next Steps”- Frameworks Overview — Other framework integrations
- Quick Start — Get running in 5 minutes
- API Reference — Full endpoint documentation