latestOpenAPI 3.1.02026-08-197104501.1 MB

8fbaab4fc7c5

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:

  1. Compiling the provided code in a sandbox
  2. Executing the apply_guardrail function with test input
  3. Returning the result (allow/block/modify)

👉 Custom Code Guardrail docs

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

custom_codestring required
test_inputobject required
input_typestring
request_dataobject nullable

Response

Successful Response

successboolean required
resultobject nullable
errorstring nullable
error_typestring nullable