OpenAI Agents SDK for Python guardrails
This example project demonstrates how to implement different types of guardrails using the OpenAI Agent SDK for Python with Trigger.dev.
Overview
This demo is a practical guide that demonstrates:
- Three types of AI guardrails: Input validation, output checking, and real-time streaming monitoring
- Integration of the OpenAI Agent SDK for Python with Trigger.dev for production AI workflows
- Triggering Python scripts from tasks using our Python build extension
- Educational examples of implementing guardrails for AI safety and control mechanisms
- Real-world scenarios like math tutoring agents with content validation and complexity monitoring
Guardrails are safety mechanisms that run alongside AI agents to validate input, check output, monitor streaming content in real-time, and prevent unwanted or harmful behavior.
GitHub repo
View the OpenAI Agent SDK Guardrails repo
Click here to view the full code for this project in our examples repository on GitHub. You can fork it and use it as a starting point for your own project.
Deploy this example to your project
Copies openai-agent-sdk-guardrails-examples from GitHub, installs it, and deploys its tasks to your project’s production environment.
npx degit triggerdotdev/examples/openai-agent-sdk-guardrails-examples openai-agent-sdk-guardrails-examples
cd openai-agent-sdk-guardrails-examples
npm install
npx trigger.dev@latest login
npx trigger.dev@latest deploy --project-ref <your-project-ref>If the example has a .env.example, set those variables in your project’s environment variables before deploying. To run the tasks on your machine instead, use dev in place of deploy.
Video
Relevant code
Trigger.dev Tasks
- inputGuardrails.ts - Passes user prompts to Python script and handles
InputGuardrailTripwireTriggeredexceptions - outputGuardrails.ts - Runs agent generation and catches
OutputGuardrailTripwireTriggeredexceptions with detailed error info - streamingGuardrails.ts - Executes streaming Python script and parses JSON output containing guardrail metrics
Python Implementations
- input-guardrails.py - Agent with
@input_guardraildecorator that validates user input before processing (example: math tutor that only responds to math questions) - output-guardrails.py - Agent with
@output_guardraildecorator that validates generated responses using a separate guardrail agent - streaming-guardrails.py - Processes
ResponseTextDeltaEventstreams with async guardrail checks at configurable intervals (example: stops streaming if language is too complex for a 10-year-old)
Configuration
- trigger.config.ts - Uses the Trigger.dev Python extension
Learn more
Was this page helpful?
Last updated
Edit on GitHub