latestOpenAPI 3.1.02026-08-236994571.1 MB
79928a3d37d3
Guardrails
Test Custom Code Guardrail
Test custom code guardrail logic without creating a guardrail.
This endpoint allows admins to experiment with custom code guardrails by:
- Compiling the provided code in a sandbox
- Executing the apply_guardrail function with test input
- Returning the result (allow/block/modify)
Example Request:
curl -X POST "http://localhost:4000/guardrails/test_custom_code" \
-H "Authorization: Bearer <your_api_key>" \
-H "Content-Type: application/json" \
-d '{
"custom_code": "def apply_guardrail(inputs, request_data, input_type):\n for text in inputs[\"texts\"]:\n if regex_match(text, r\"\\d{3}-\\d{2}-\\d{4}\"):\n return block(\"SSN detected\")\n return allow()",
"test_input": {
"texts": ["My SSN is 123-45-6789"]
},
"input_type": "request"
}'
Example Success Response (blocked):
{
"success": true,
"result": {
"action": "block",
"reason": "SSN detected"
},
"error": null,
"error_type": null
}
Example Success Response (allowed):
{
"success": true,
"result": {
"action": "allow"
},
"error": null,
"error_type": null
}
Example Success Response (modified):
{
"success": true,
"result": {
"action": "modify",
"texts": ["My SSN is [REDACTED]"]
},
"error": null,
"error_type": null
}
Example Error Response (compilation error):
{
"success": false,
"result": null,
"error": "Syntax error in custom code: invalid syntax (<guardrail>, line 1)",
"error_type": "compilation"
}
post/guardrails/test_custom_code
Request body
Response
Successful Response