Run Protect from the SDK
Screen a single input for toxicity, bias, prompt injection, or PII, straight from your own code.
This guide screens a single input by calling protect() directly from your own code: no dashboard involved. You can call it on incoming input before it reaches your model, or on the model’s output before it reaches whoever receives it; protect() takes a string either way. The guide walks through initializing the client, building a rules list, running the check, and reading the result back, for both text and non-text input. For every parameter and return field, see the Protect SDK reference.
Initialize Protect
Install the SDK first:
pip install ai-evaluation
from fi.evals import Protect
protector = Protect()
Protect reads FI_API_KEY and FI_SECRET_KEY from your environment. Generate these on the API Keys page if you don’t have them yet.
Build the rules list
protect_rules is a list of dicts, each naming one check to run with a metric key:
rules = [
{"metric": "toxicity"},
{"metric": "bias_detection"},
{"metric": "prompt_injection"},
{"metric": "data_privacy_compliance"},
]
For how these checks fit into the guardrail model, see Understanding Protect.
The SDK accepts these metric values: toxicity, bias, bias_detection, sexist, prompt_injection, data_privacy_compliance, and pii. A metric name outside this list raises an error, so confirm the name before you ship it.
Run the check
Call protector.protect() with:
inputs: your input, as the first positional argumentprotect_rules: the rules listaction: the message returned when a rule failsreason: setTrueto include an explanation with the resulttimeout: how long the check can run, in milliseconds
text_to_check = "the text you want to screen"
result = protector.protect(
text_to_check,
protect_rules=rules,
action="I'm sorry, I can't help you with that.",
reason=True,
timeout=25000,
)
Read the result
protect() returns a dictionary shaped like this:
{
"status": "failed",
"completed_rules": ["toxicity"],
"uncompleted_rules": ["bias_detection", "prompt_injection", "data_privacy_compliance"],
"failed_rule": ["toxicity"],
"messages": "I'm sorry, I can't help you with that.",
"reasons": ["Message contains content flagged as toxic."],
"time_taken": 0.42,
}
status:"passed"or"failed"messages: on a failure, carries theactionstring instead of the original inputcompleted_rules/uncompleted_rules: which checks ran to completion, and which didn’t; on a failure, the remaining checks can come back inuncompleted_rulesfailed_rule: the check(s) that tripped a failure, as a list, for example["toxicity"]reasons: holds an explanation for a failure whenreason=Trueis settime_taken: how long the check took, in seconds
If a rule doesn’t finish before timeout elapses, it lands in uncompleted_rules instead.
Use status to decide what to send onward: on a failure, forward messages instead of the original input; on a pass, forward the input unchanged.
if result["status"] == "failed":
response = result["messages"]
else:
response = text_to_check
Screen an image or audio input instead of text
inputs isn’t limited to text. Pass an image or audio path or URL in place of the text string, and call protect() the same way. The rules list carries over unchanged. For accepted URL and file formats, see the Protect SDK reference.
result = protector.protect(
"/path/to/local/audio.wav",
protect_rules=rules,
action="Audio content cannot be processed",
reason=True,
timeout=25000,
)
result = protector.protect(
"/path/to/local/image.png",
protect_rules=rules,
action="Image content cannot be processed",
reason=True,
timeout=25000,
)
Text, image, and audio are the only accepted input types for this call. Image sets, PDFs, and knowledge bases are not accepted.
Dive deeper
Questions & Discussion