Configure custom guardrails for validation and security.
main.py
: FastAPI application with route definitionsguardrail/
: Directory containing all guardrail implementationsentities.py
: Pydantic models for request/response validationrequirements.txt
: Dependencies and librariesRequestContext
is a Pydantic model that provides structured contextual information for each request processed by your custom guardrail server. It includes details about the user (as a Subject
object) and optional metadata relevant to the request lifecycle. This context is automatically populated by the TrueFoundry AI Gateway and can be leveraged for access control, auditing, or custom logic within your guardrail implementations.
1. PII Redaction (Presidio)
Info
POST /pii-redaction
Code Snippet
Response Behavior
null
- No PII detected, no transformation neededChatCompletionCreateParams
- PII detected and redacted, returns modified requestHTTP 400/500
- Error occurred during processing2. NSFW Filtering (Local Model)
Info
POST /nsfw-filtering
Code Snippet
Response Behavior
null
- Content is safe, no issues detectedHTTP 400
- NSFW content detected, request blocked3. Drug Mention Detection (Guardrails AI)
Info
POST /drug-mention
Code Snippet
Response Behavior
null
- No drug mentions detectedHTTP 400
- Drug mentions detected, request blocked4. Web Sanitization (Guardrails AI)
Info
POST /web-sanitization
Code Snippet
Response Behavior
null
- No malicious content detectedHTTP 400
- Malicious content detected, request blocked5. PII Detection (Guardrails AI)
Info
POST /pii-detection
Code Snippet
Response Behavior
null
- No PII detectedHTTP 400
- PII detected, request blockedInput Guardrail Request
Output Guardrail Request
Navigate to Guardrails
Fill in the Custom Guardrail Form
Config
field you specify in the integration creation form is sent as the config
attribute in every guardrail request payload. This allows you to parameterize your guardrail logic (e.g., set thresholds, enable/disable features, or pass secrets) without changing your server code.
InputGuardrailRequest
or OutputGuardrailRequest
) and sends it to your server. This object includes:
requestBody
)responseBody
)config
object (from your integration creation form)context
(user, metadata, etc.)Integration Creation Form Field | Sent in Guardrail Request as |
---|---|
Config | config |
Auth Data, Headers | HTTP headers customHeaders |
Operation | Determines endpoint & method |
URL | Guardrail server endpoint |
Integration Form Example Values
Field | Example Value |
---|---|
Operation | POST /pii-redaction |
URL | https://my-guardrail-server.example.com/pii-redaction |
Auth Data | Bearer <token> |
Headers | |
Config |
Integration Form Example Values
Field | Example Value |
---|---|
Operation | POST /nsfw-filtering |
URL | https://my-guardrail-server.example.com/nsfw-filtering |
Auth Data | Bearer <token> |
Headers | |
Config |