|
| 1 | +--- |
| 2 | +title: "OpenAI Agent SDK guardrails examples" |
| 3 | +sidebarTitle: "OpenAI Agent SDK guardrails" |
| 4 | +description: "This example project demonstrates how to implement different types of guardrails using the OpenAI Agent SDK with Trigger.dev for AI safety and control." |
| 5 | +--- |
| 6 | + |
| 7 | +import RealtimeLearnMore from "/snippets/realtime-learn-more.mdx"; |
| 8 | + |
| 9 | +<Info>The OpenAI Agent SDK is a Python library. </Info> |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +This demo is a practical guide that demonstrates: |
| 14 | + |
| 15 | +- **Three types of AI guardrails**: Input validation, output checking, and real-time streaming monitoring |
| 16 | +- Integration of the [OpenAI Agent SDK](https://openai.github.io/openai-agents-python/) with [Trigger.dev](https://trigger.dev) for production AI workflows |
| 17 | +- **Educational examples** of implementing guardrails for AI safety and control mechanisms |
| 18 | +- Real-world scenarios like math tutoring agents with content validation and complexity monitoring |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +## GitHub repo |
| 23 | + |
| 24 | +<Card |
| 25 | + title="View the OpenAI Agent SDK Guardrails repo" |
| 26 | + icon="GitHub" |
| 27 | + href="https://github.com/triggerdotdev/examples/tree/main/openai-agent-sdk-guardrails-examples" |
| 28 | +> |
| 29 | + Click here to view the full code for this project in our examples repository on GitHub. You can |
| 30 | + fork it and use it as a starting point for your own project. |
| 31 | +</Card> |
| 32 | + |
| 33 | +## Video |
| 34 | + |
| 35 | +<video |
| 36 | + controls |
| 37 | + className="w-full aspect-video" |
| 38 | + src="https://github.com/user-attachments/assets/9b1e55c7-467d-4aca-8b4a-a018014c0827" |
| 39 | +></video> |
| 40 | + |
| 41 | +## Relevant code |
| 42 | + |
| 43 | +### Trigger.dev Tasks |
| 44 | + |
| 45 | +- **[inputGuardrails.ts](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/trigger/inputGuardrails.ts)** - Passes user prompts to Python script and handles `InputGuardrailTripwireTriggered` exceptions |
| 46 | +- **[outputGuardrails.ts](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/trigger/outputGuardrails.ts)** - Runs agent generation and catches `OutputGuardrailTripwireTriggered` exceptions with detailed error info |
| 47 | +- **[streamingGuardrails.ts](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/trigger/streamingGuardrails.ts)** - Executes streaming Python script and parses JSON output containing guardrail metrics |
| 48 | + |
| 49 | +### Python Implementations |
| 50 | + |
| 51 | +- **[input-guardrails.py](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/python/input-guardrails.py)** - Agent with `@input_guardrail` decorator that validates user input before processing (example: math tutor that only responds to math questions) |
| 52 | +- **[output-guardrails.py](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/python/output-guardrails.py)** - Agent with `@output_guardrail` decorator that validates generated responses using a separate guardrail agent |
| 53 | +- **[streaming-guardrails.py](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/src/python/streaming-guardrails.py)** - Processes `ResponseTextDeltaEvent` streams with async guardrail checks at configurable intervals (example: stops streaming if language is too complex for a 10-year-old) |
| 54 | + |
| 55 | +### Configuration |
| 56 | + |
| 57 | +- **[trigger.config.ts](https://github.com/triggerdotdev/examples/blob/main/openai-agent-sdk-guardrails-examples/trigger.config.ts)** - Uses the Trigger.dev Python extension |
| 58 | + |
| 59 | +### Learn more |
| 60 | + |
| 61 | +- [OpenAI Agent SDK documentation](https://openai.github.io/openai-agents-python/) |
| 62 | +- [OpenAI Agent SDK guardrails](https://openai.github.io/openai-agents-python/guardrails/) |
| 63 | +- Our [Python build extension](/config/extensions/pythonExtension#python) |
0 commit comments