OpenAI Agents SDK guardrail integration for PII redaction.
src/privacy_filter_openai/
├── __init__.py # Exports PiiInputGuardrail, PiiOutputGuardrail
└── guardrails.py # Guardrail implementations
# IMPORTANT: privacy-filter must be installed first (editable or from wheel)
cd ../privacy-filter
uv pip install -e .
# Then work on openai-agents-hook
cd ../openai-agents-hook
uv sync
# Build wheel
uv buildpyproject.tomldepends on"privacy-filter"(local package)- Optional extra:
openai→ installsopenai-agents(the Agents SDK) - Must install privacy-filter first before this package will work
From agents SDK:
class Guardrail:
async def __call__(
self,
context: RunContextWrapper,
agent: Agent,
input_content: str, # or output_content
) -> GuardrailFunctionOutput:
...
@dataclass
class GuardrailFunctionOutput:
output: str
triggered: boolclass PiiInputGuardrail:
"""Redacts PII from user messages before agent processing."""
async def __call__(context, agent, input_content) -> GuardrailFunctionOutput
class PiiOutputGuardrail:
"""Restores PII from placeholders in agent responses."""
async def __call__(context, agent, output_content) -> GuardrailFunctionOutputThe tricky part: input and output guardrails are separate instances, but they need to share the PiiStore (placeholder mappings).
Solution: Use a module-level dict keyed by context id:
_PIISTORE_CONTEXT: dict[int, PiiStore] = {}
# Input guardrail stores:
_PIISTORE_CONTEXT[id(context)] = store
# Output guardrail retrieves and clears:
store = _PIISTORE_CONTEXT.pop(id(context), None)This assumes input guardrail runs before output guardrail for the same context.
This is a separate package (openai-agents-privacy-filter) that depends on privacy-filter.
Important publishing order:
privacy-filtermust be on PyPI first- Wait a few minutes for PyPI index propagation
- Then build/publish
openai-agents-privacy-filter
# 1. Ensure privacy-filter is published to PyPI
cd ../privacy-filter
uv build
uv publish dist/*
# 2. Wait for PyPI, then build/publish openai-agents-hook
cd ../openai-agents-hook
uv build
uv publish dist/*Note: This package fails to build if privacy-filter is not on PyPI.
privacy-filter— Core PII library (local dependency)openai-agents(optional) — OpenAI Agents SDK