Skip to main content

/guardrails/apply_guardrail

Use this endpoint to directly call a guardrail configured on your LiteLLM instance. This is useful when you have services that need to directly call a guardrail.

Supported Guardrail Typesโ€‹

This endpoint supports various guardrail types including:

  • Presidio - PII detection and masking
  • Bedrock - AWS Bedrock guardrails for content moderation
  • Lakera - AI safety guardrails
  • Custom guardrails - User-defined guardrails

Configurationโ€‹

Bedrock Guardrail Configurationโ€‹

To use Bedrock guardrails with the apply_guardrail endpoint, configure your guardrail in your LiteLLM config.yaml:

guardrails:
- guardrail_name: "bedrock-content-guard"
litellm_params:
guardrail: bedrock
mode: "pre_call"
guardrailIdentifier: "your-guardrail-id" # Your actual Bedrock guardrail ID
guardrailVersion: "DRAFT" # or your version number
aws_region_name: "us-east-1" # Your AWS region
aws_role_name: "your-role-arn" # Your AWS role with Bedrock permissions
default_on: true

Required AWS Setup:

  1. Create a Bedrock guardrail in AWS Console
  2. Get the guardrail ID and version
  3. Ensure your AWS credentials have Bedrock permissions
  4. Configure the guardrail in your LiteLLM config

Usageโ€‹


In this example mask_pii is a Presidio guardrail configured on LiteLLM.

Example calling the endpoint
curl -X POST 'http://localhost:4000/guardrails/apply_guardrail' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer your-api-key' \
-d '{
"guardrail_name": "mask_pii",
"text": "My name is John Doe and my email is john@example.com",
"language": "en",
"entities": ["NAME", "EMAIL"]
}'

Request Formatโ€‹


The request body should follow the ApplyGuardrailRequest format.

Example Request Bodyโ€‹

{
"guardrail_name": "mask_pii",
"text": "My name is John Doe and my email is john@example.com",
"language": "en",
"entities": ["NAME", "EMAIL"]
}

Required Fieldsโ€‹

  • guardrail_name (string):
    The identifier for the guardrail to apply (e.g., "mask_pii").
  • text (string):
    The input text to process through the guardrail.

Optional Fieldsโ€‹

  • language (string):
    The language of the input text (e.g., "en" for English).
  • entities (array of strings):
    Specific entities to process or filter (e.g., ["NAME", "EMAIL"]).

Response Formatโ€‹


The response will contain the processed text after applying the guardrail.

Example Responseโ€‹

{
"response_text": "My name is [REDACTED] and my email is [REDACTED]"
}

Response Fieldsโ€‹

  • response_text (string):
    The text after applying the guardrail.

Error Responsesโ€‹

If a guardrail blocks content (e.g., Bedrock guardrail), the endpoint will return an error:

{
"detail": "Content blocked by Bedrock guardrail: Content violates policy"
}