Azure Prompt Shields Guardrail Usage
Azure Prompt Shields Guardrail Usage
import os
from getpass import getpass
def ensure_env_var(name: str) -> None:
"""Prompt for an environment variable if not already set."""
if name not in os.environ:
print(f"{name} not found in environment!")
value = getpass(f"Please enter your {name}: ")
os.environ[name] = value
print(f"{name} set for this session!")
else:
print(f"{name} found in environment.")
for var in ["CONTENT_SAFETY_KEY", "CONTENT_SAFETY_ENDPOINT"]:
ensure_env_var(var)Detecting direct attacks
Output should look like:
validisTrueonly when no attack is detected anywhere.scoreis1.0when any attack is detected,0.0otherwise.extracarries the per-field detection booleans, andcategoriesholds a per-source verdict (the user prompt plus each document).
Detecting indirect (data-borne) attacks
Pass the documents your application is about to feed the model — retrieved chunks, tool outputs, emails — and Prompt Shields scans each one for embedded instructions:
Output should look like:
You can also pass documents without a user_prompt to screen retrieved context on its own.
For more information, please see our docs. Note that hosted prompt-injection detectors have known evasion gaps under adaptive attacks — treat Prompt Shields as defense-in-depth, not a complete mitigation.
Last updated