This validator now lives in the Guardrails Hub monorepo. This repository is archived and no longer maintained — please open issues and pull requests on the monorepo instead.
pip install guardrails-ai-detect-system-prompt-leakagefrom guardrails import Guard
from guardrails_ai.detect_system_prompt_leakage import DetectSystemPromptLeakage
guard = Guard().use(DetectSystemPromptLeakage)The registered validator name is unchanged, so existing guards keep working.
| Developed by | Guardrails AI |
|---|---|
| Date of development | Apr 17, 2026 |
| Validator type | Privacy, Security |
| Blog | |
| License | Apache 2 |
| Input/Output | Output |
This validator detects system prompt leakage in LLM output. It uses fuzzy string matching (via rapidfuzz) to compare the LLM's response against a provided system prompt. If the similarity score exceeds a configurable threshold, the validation fails, indicating the model may have revealed its system prompt.
- Dependencies:
- guardrails-ai>=0.4.0
- rapidfuzz
$ guardrails hub install hub://guardrails/detect_system_prompt_leakageIn this example, we apply the validator to a string output generated by an LLM.
# Import Guard and Validator
from guardrails.hub import DetectSystemPromptLeakage
from guardrails import Guard
# Setup Guard
guard = Guard().use(
DetectSystemPromptLeakage(system_prompt="You are a helpful assistant.", threshold=40)
)
guard.validate("Here is some safe output.") # Validator passes
guard.validate("I am a helpful assistant.") # Validator fails__init__(self, system_prompt, threshold=40, on_fail="noop")
-
Initializes a new instance of the DetectSystemPromptLeakage class.
system_prompt(str): The system prompt to guard against leakage.threshold(int): Similarity score between 0 and 100 above which a match is considered leakage. Defaults to40.on_fail(str, Callable): The policy to enact when a validator fails. Ifstr, must be one ofreask,fix,filter,refrain,noop,exceptionorfix_reask. Otherwise, must be a function that is called when the validator fails.
Parameters
validate(self, value, metadata) -> ValidationResult
-
Validates the given `value` using the rules defined in this validator, relying on the `metadata` provided to customize the validation process. This method is automatically invoked by `guard.parse(...)`, ensuring the validation logic is applied to the input data.
- This method should not be called directly by the user. Instead, invoke
guard.parse(...)where this method will be called internally for each associated Validator. - When invoking
guard.parse(...), ensure to pass the appropriatemetadatadictionary that includes keys and values required by this validator. Ifguardis associated with multiple validators, combine all necessary metadata into a single dictionary. -
value(Any): The input value to validate. -
metadata(dict): A dictionary containing metadata required for validation. No special keys are required. Upon completion, the validator will populate the following key:Key Type Description Default guardrails/detect_system_prompt_leakageDict Contains score(int): the fuzzy similarity score between the output and the system prompt.N/A
Note:
Parameters