For the complete documentation index, see llms.txt. This page is also available as Markdown.

Azure Prompt Shields Guardrail Usage

Azure Prompt Shields Guardrail Usage

Open In Colab

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:

  • valid is True only when no attack is detected anywhere.

  • score is 1.0 when any attack is detected, 0.0 otherwise.

  • extra carries the per-field detection booleans, and categories holds 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