---
title: "Test Custom Code Guardrail"
method: POST
path: "/guardrails/test_custom_code"
tags: ["Guardrails"]
---

# Test Custom Code Guardrail

`POST /guardrails/test_custom_code`

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](https://docs.litellm.ai/docs/proxy/guardrails/custom_code_guardrail)

Example Request:
```bash
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):
```json
{
    "success": true,
    "result": {
        "action": "block",
        "reason": "SSN detected"
    },
    "error": null,
    "error_type": null
}
```

Example Success Response (allowed):
```json
{
    "success": true,
    "result": {
        "action": "allow"
    },
    "error": null,
    "error_type": null
}
```

Example Success Response (modified):
```json
{
    "success": true,
    "result": {
        "action": "modify",
        "texts": ["My SSN is [REDACTED]"]
    },
    "error": null,
    "error_type": null
}
```

Example Error Response (compilation error):
```json
{
    "success": false,
    "result": null,
    "error": "Syntax error in custom code: invalid syntax (<guardrail>, line 1)",
    "error_type": "compilation"
}
```

## Request body

- TestCustomCodeGuardrailRequest — Request model for testing custom code guardrails.
  - `custom_code` string, required
  - `test_input` object, required
  - `input_type` string
  - `request_data` object, nullable

## Response `200`

Successful Response

- TestCustomCodeGuardrailResponse — Response model for testing custom code guardrails.
  - `success` boolean, required
  - `result` object, nullable
  - `error` string, nullable
  - `error_type` string, nullable

## Other responses

- `422` — Validation Error

---

[API](https://skmtc.net/flock/apis/litellm-api.md) · [All operations](https://skmtc.net/flock/apis/litellm-api/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/flock/litellm-api/revisions/8fbaab4fc7c5/schema)
