Testing Azure AI Content Safety Prompt Shields with PyRIT

Prompt Shields can run in PyRIT as a target that reports whether a jailbreak was detected, or as a true/false scorer inside attacks. This page covers the endpoint, configuration sequence, and the field length limits that affect encoded prompts.

On this page

How PyRIT uses Azure AI Content Safety Prompt Shields

In PyRIT, Azure AI Content Safety Prompt Shield can be used either as a target or as a true/false scorer, and both options require a deployed Content Safety endpoint. The question a team usually brings is whether its jailbreak defenses fire on the attacks PyRIT generates. Before any test runs, confirm that the endpoint exists, that its region and resource are the ones your application uses, and that the people running PyRIT have access to its key or identity. Without a deployed endpoint, neither role works, so this is the first item on the plan.

In the target role, PyRIT sends a prompt to Prompt Shield and records whether it was flagged as a jailbreak attempt. This fits when you want to measure the shield itself, for example to see how often a set of attack prompts from your own catalog gets caught before any model is involved. In the scorer role, PyRIT uses Prompt Shield to judge a response or prompt inside an attack, returning true or false for detection. This fits when an attack already runs against a model and you want a detection signal in the same results. Choose the role based on whether the shield or the model is the object under test.

This section covers Prompt Shields only. It does not cover Content Safety text or image moderation scoring in general, which is a separate scorer family. Keep the two in separate result sets so that a finding about jailbreak detection is not mixed with harm category scores. Ask your Azure contact to confirm which Prompt Shield API version your endpoint supports, since the parameters PyRIT sends depend on it.

What you need before Prompt Shields works in PyRIT

Prompt Shields only runs inside PyRIT once four things are in place: a deployed Content Safety endpoint, a credential PyRIT can read, a supported Python and PyRIT install, and a network path from the machine running the tests. Confirm each item before writing any target code, because a missing key or blocked route usually shows up as a failed call rather than a clear error.

Deployed Content Safety endpoint

PyRIT's Prompt Shield target needs an Azure AI Content Safety resource that is already deployed. It cannot be tested against a local stub or a generic model endpoint. Confirm the resource exists in the subscription you intend to use, note its endpoint URL, and confirm with Microsoft that the Prompt Shield capability and the API version your PyRIT release calls are available in your region before you assume them.

Prompt Shield target docs

Access key or identity

Decide how PyRIT authenticates to the resource: an access key or a Microsoft Entra ID identity. Keys are simpler to rotate badly, so record who owns the key, where it is stored and when it expires. If you use an identity, confirm that its role assignment on the Content Safety resource allows the Prompt Shield call. Ask your security team which option your policy allows before the first test run.

Supported Python and PyRIT install

Install PyRIT from PyPI on a Python version the package supports. The package supports Python 3.10 up to but not including 3.15, and the documentation recommends 3.13. Use a virtual environment so the install is reproducible, and pin the PyRIT version your team tested against, since target behavior can change between releases.

Credential file in ~/.pyrit/.env

PyRIT reads credentials from ~/.pyrit/.env on the machine that runs it. Store the endpoint and key there rather than in notebooks or shell history, and restrict the file to the account that runs the tests. Shared CI runners need their own secret injection, so agree on where the values come from before the pipeline is built.

Network reachability

The machine running PyRIT must reach the Content Safety endpoint over the network path your organization allows. Check outbound rules, DNS resolution, proxy settings and any private endpoint or firewall that applies to the resource. Test reachability with a single call before running a full attack, so connection problems are not mistaken for detection results.

Confirm versions and regions

Do not assume the Content Safety API version, the regions where Prompt Shields is offered, or the field limits in your environment. Ask Microsoft or check current Azure documentation for your region, and record the answers with the test plan. These details change over time, and a result that was valid for one API version may not hold for another.

Configuring Prompt Shields as a target and as a scorer

This sequence takes Prompt Shields from a deployed Content Safety resource to a working PyRIT target and then to a true/false scorer inside an attack. Each step ends with a check so you know the component is detecting jailbreaks and recording results before you rely on it.

  1. Identify or deploy the Content Safety resource

    Your cloud or security team confirms that an Azure AI Content Safety resource exists in the subscription you test against, or creates one. PyRIT's Prompt Shield target requires a deployed Content Safety endpoint, so the resource must be reachable from the machine that runs PyRIT. Record the endpoint URL and confirm in the Azure portal that the resource is in a region and network path your team is allowed to use. Done when you can open the resource, see its endpoint and key, and confirm the key is issued for that resource only.

  2. Store the endpoint and key in PyRIT

    The engineer running the tests adds the Content Safety endpoint and key to the PyRIT credentials file at ~/.pyrit/.env, using the variable names given in the PyRIT Prompt Shield target documentation. Keep the file out of source control and restrict its file permissions to the test account. Settings that are not secrets belong in ~/.pyrit/.pyrit_conf. Done when a short script can read both values from the environment without printing them, and the key does not appear in shell history or logs.

  3. Instantiate the Prompt Shield target

    In a minimal Python script, the engineer imports the Prompt Shield target from PyRIT, creates an instance that uses the endpoint and key loaded from the environment, and sets up PyRIT's memory so results are stored. Keep this script small so that any failure points to configuration rather than attack logic. Done when the script runs to completion without authentication or connection errors against the resource you identified in the first step.

  4. Send a benign and a known jailbreak test prompt

    Send one ordinary prompt and one well-documented jailbreak test string through the target, using inputs your security team has approved for testing. The benign prompt should come back with no jailbreak detected, and the jailbreak prompt should come back flagged. Read the detection flag in the response directly rather than assuming from the text. Done when the benign prompt is not flagged and the known test input is flagged. If both come back the same way, check the endpoint, the resource region and whether the input exceeds the field limit before going further.

  5. Check field length limits for encoded prompts

    Before testing converted prompts, confirm how long the input can be. Prompt Shields limits how many characters a single userPrompt or document field can hold, and ASCII art-encoded prompts can exceed that limit. Send a test input that is deliberately long and check whether the service returns an error or truncates the input. Record the behavior in your test notes so that a missing detection is not mistaken for a model that resisted the attack. Done when you know whether your encoded test inputs fit the field limit or need to be split or shortened.

  6. Wire the same component in as a scorer

    Attach the Prompt Shield component to an attack as a true/false scorer, so each response from the target under test is evaluated for whether a jailbreak was detected. Use the same instance and configuration you verified in the previous steps. Run a small attack with a few known-good and known-bad objectives so you can see both outcomes. Done when the attack output shows a true or false score for each response that matches what you saw when you tested the same prompts directly.

  7. Confirm scores are recorded in memory

    Query PyRIT memory after the scorer run to confirm that each score is stored alongside its conversation piece. Check that the stored value matches the detection flag from the direct test and that the record identifies the scorer used. If you plan to rely on these records as evidence, confirm with your compliance team which memory fields they need and how long the database must be kept. Done when you can retrieve the scored conversation from memory and reproduce the true or false result without rerunning the attack.

Choosing between the target and scorer roles

In the target role, Prompt Shields is the system under test. PyRIT sends it attack prompts, and the result records whether Prompt Shields flagged the input as a jailbreak attempt. Use this role when the question is how well Prompt Shields detects jailbreaks for a given set of prompts, configuration and content types. The measurement belongs to the detection service, not to any chat model behind it.

  • Recorded output: the detection decision for each prompt sent, stored in PyRIT memory with the original attack prompt.
  • Requirement: a deployed Azure AI Content Safety endpoint that the test identity can reach.
  • Input sensitivity: Prompt Shields limits the characters one userPrompt or document field can hold, so encoded prompts such as ASCII art may exceed the limit and produce results that reflect input rejection rather than detection.
  • Scope limit: the role says nothing about whether a downstream model would have answered the prompt.
  • Reporting: report the detection rate against a named prompt set, with the endpoint and field used.

Before reporting a detection rate, confirm the current field length limits in the Azure documentation for your API version and check how your harness handles rejected inputs.

Why field length limits change encoded prompt results

Prompt Shield limits how many characters a single userPrompt field or document field can hold. This matters for PyRIT tests because an encoding such as ASCII art expands a short request into a much longer string. A prompt that fits comfortably in plain text can exceed the limit once encoded, so the request may fail outright or be truncated before the shield evaluates the content it was meant to judge. A result from that run tells you about the length limit, not about the shield's jailbreak detection. Treat any encoded test that errors or returns an unexpected verdict as inconclusive until you have checked its length.

To detect the problem, log the input length of every converted prompt next to its outcome. Look for patterns in test runs: errors that appear only on the longest encoded inputs, responses that show no detection for prompts that a plain-text control correctly flagged, or verdicts that change when the same content is shortened. Keep the plain-text and encoded versions of each objective paired so a difference can be traced. When a long encoded prompt cannot fit, split the content across fields only where the target and scoring design allow it, and record exactly how the content was placed so the split is visible in your results.

The exact character limit depends on the API version you call, so confirm it in the current Microsoft documentation for Azure AI Content Safety before you set test lengths or write up findings. Record the API version, the endpoint configuration and the limit you confirmed alongside each test run. The goal is a valid measurement of detection: if a prompt cannot be evaluated as written, report that as a coverage gap and state how the content was adjusted, rather than presenting the result as a clean pass or fail.

How a PyRIT attack reaches Prompt Shields

The flow starts in a PyRIT attack, which sends each objective through a converter before the Prompt Shield target calls the Azure AI Content Safety endpoint. The endpoint returns a detection result that PyRIT records in memory and, when the Prompt Shield is used as a scorer, applies as a true/false verdict. The trust boundary sits between the test machine and the Azure resource, and the key is read from ~/.pyrit/.env before any request leaves the machine. Check that the converter output stays within the field length limits before reading the result.

Request path from a PyRIT attack to the Prompt Shield endpoint and back into memory.

Mapping PyRIT fields to Prompt Shield inputs and outputs

This table maps what PyRIT sends to Prompt Shield, what comes back, and how each result is stored in memory. Read it before building a target or scorer so you know which fields carry the attack prompt, which carry documents, and which length limits could silently change your results.

ItemPrompt Shield or PyRIT sideWhat to confirm or record
Attack prompt textSent as the userPrompt field when the Prompt Shield target analyzes a user turnConfirm the current field name and request shape in the Azure AI Content Safety API reference before coding
Document contentSent in the documents field for indirect injection tests, such as text pulled from a file or web pageConfirm how multiple documents are passed and whether each document has its own length limit
Length limitPrompt Shield caps the characters one userPrompt or document field can holdMeasure encoded prompts, such as ASCII art, before the run; oversized inputs may be rejected or truncated
Jailbreak detection resultMapped to a true/false score when Prompt Shield is used as a scorerRecord whether true means a jailbreak was detected, and test that polarity on known safe and unsafe samples
Target responseReturned by the target as the response piece in the conversationStore the raw detection output alongside the converted prompt so reviewers can trace each verdict
Conversation recordsPrompt, converter output and target response saved in PyRIT memory (SQLite or Azure SQL)Confirm the memory location and retention for your team before the first test run
Score recordsScorer output linked to the conversation piece it evaluatedKeep the scorer name and endpoint version with each score so results can be reproduced

Securing the Content Safety key and test data

A Prompt Shield test run touches three kinds of sensitive material: the Content Safety credential, the harmful prompts used as test input, and the prompt and response records PyRIT keeps in memory. The cards below cover how to store, scope, retain and handle each one before the test starts.

Store the key in the PyRIT env file

PyRIT reads credentials from ~/.pyrit/.env, so the Content Safety key lives in a file on the machine that runs the tests. Restrict that file to the account that runs PyRIT, keep it out of source control and shared drives, and check that CI runners do not write it to build logs. Confirm in the PyRIT setup documentation which other credential sources your version supports before you choose a store.

PyRIT documentation

Prefer managed identity or Key Vault

If your team already runs Entra ID or Azure Key Vault, use them instead of a static key where the Content Safety resource supports it. A managed secret reduces the number of copies of the key and makes rotation a platform task rather than a file edit on every tester's laptop. Confirm with the PyRIT setup docs which authentication paths the Prompt Shield target accepts in your release.

Scope the key to one resource

Create or use a Content Safety resource dedicated to testing, and give the test identity only the role needed to call Prompt Shield. Avoid keys for a subscription-wide account or a resource that also serves production traffic. If a key leaks, a dedicated resource limits what an attacker can call and how much it can cost you.

Decide what PyRIT memory keeps

PyRIT stores conversations and scoring results in SQLite or Azure SQL, so every Prompt Shield call can leave a record of the jailbreak text and the response. Decide before the run how long those records are kept, where the database file sits, and who can read it. Check that the retention period matches your data policy and any evidence requirements that apply to the results.

Handle harmful test prompts under policy

Encoded jailbreak prompts, including ASCII art variants, are harmful test content and may be captured in logs, exports and screenshots. Agree with your data owner where these prompts can be stored, who may open the datasets, and whether they can leave the test environment. Record the approval alongside the test plan so reviewers can see it was controlled.

Questions teams ask before testing Prompt Shields with PyRIT

How much effort does setting up Prompt Shields as a PyRIT target take?

The setup is mostly configuration rather than new code, but the effort depends on how many pieces you must connect. The main items are a deployed Azure AI Content Safety endpoint, a key or credential that PyRIT can read from ~/.pyrit/.env, and a target definition that matches the Prompt Shield request format. Effort rises when your Content Safety resource sits behind private networking, when you need separate resources per environment, or when you want Prompt Shields used as a scorer inside many attack runs. Ask your team to list those dependencies before estimating time, because each one adds a review step.

Can an internal engineer set this up without outside help?

Yes, a Python engineer who already works with Azure resources can usually set up a basic Prompt Shield target without outside help, as long as they can deploy or access a Content Safety endpoint. The harder parts are judgment calls: choosing which prompts count as jailbreak tests, deciding how to handle the field length limit for encoded prompts, and agreeing what a detection means for your product. If your team lacks someone who can own those decisions, a fixed-scope engagement from DeployOpen can cover planning, the build and handover. Ask any provider to write the scope and deliverables before work starts.

What does the team need to provide before testing starts?

Before testing starts, the team usually needs three things in place:

  • A deployed Azure AI Content Safety resource with an endpoint that PyRIT can reach, which is a prerequisite for using Prompt Shields at all.
  • Test data owners who approve the jailbreak and document prompts, and who confirm that the test content is allowed in your environment.
  • A written policy for harmful prompts that defines what is tested, where test records are stored, who can view them and how long they are kept.

Confirm these with your security and legal teams first, since they shape what the test runs may contain.

How should Prompt Shields results be read?

Read a Prompt Shields result as a detection signal for one input, not as a complete measure of safety. PyRIT records whether the service flagged a jailbreak attempt, and you can use that true or false result to compare detection across prompt sets. A missed detection does not prove that a model would comply, and a detection does not show that the application blocked harm end to end. Keep the prompt, the Prompt Shield result and the downstream model response together in memory, so a reviewer can see what was flagged and what happened next. Record the Content Safety resource and version used so results stay comparable over time.

Which prompts fail to test because of the field length limit?

Prompt Shields limits how many characters one userPrompt or document field can hold, so long encoded prompts, such as ASCII art, can exceed that limit and fail or be truncated. This matters because encoded jailbreaks are often the longest prompts in a test set. Before a run, check prompt lengths against the current limit in the Prompt Shield Target documentation and split or shorten inputs that exceed it, documenting each change. Treat a length failure as a test result to record, not as a pass, so the report does not overstate detection coverage.

What costs recur when testing Prompt Shields with PyRIT?

PyRIT itself is open source under the MIT license, so the recurring costs come from the cloud services and staff time around it. The main recurring item is Azure AI Content Safety usage, which is billed by Azure and may depend on the number of calls, your region and your agreement, so confirm the current billing terms with Microsoft before budgeting. Other recurring costs include the model endpoints used as objectives or scorers, storage for memory records, and the staff time to review results and rerun tests after changes. One-time costs include the initial setup and any test data preparation. Ask any partner for a quote that separates these items.

Does this test replace a security review of the whole application?

No. Prompt Shields testing measures one detection layer, and it works best as one part of a wider assessment of your AI feature. It does not cover the model's behavior after a prompt passes, your tool permissions, data exposure or access controls. Pair the Prompt Shield results with other PyRIT attacks and scorers, and with application security testing where the risk warrants it. Confirm with your security team which parts of the system the test will and will not cover, and state that boundary in the report.

Failures that break Prompt Shield runs and how to fix them

This table lists the failures most likely to stop a Prompt Shield run in PyRIT, with the cause and a concrete fix for each. Work through the rows in order, since the first three usually surface before any test prompt is sent. The main takeaway is that most failed runs trace to configuration, Python version, or prompt length, not to the model under test.

FailureLikely causeFix
Endpoint or key rejectedEndpoint URL points at the wrong resource, or the key belongs to a different Content Safety or Azure AI resource than the one deployedCopy the endpoint and key from the Content Safety resource you deployed, confirm the variable names PyRIT reads in ~/.pyrit/.env, and send one direct test request before running PyRIT
Authentication errors after a key rotationThe key stored in the environment file or secrets store is older than the rotated keyUpdate the stored value, restart the shell or process so the new value loads, and confirm which secret store your team uses as the source of truth
Unsupported Python versionThe interpreter is outside the range the PyPI package supports, or a different Python is picked up by the virtual environmentCreate a fresh virtual environment on Python 3.13, which the PyRIT documentation recommends, and confirm the version inside that environment before installing
Network blocked from the test hostFirewall, proxy or private network rules stop the test host from reaching the Content Safety endpointCheck egress rules for the endpoint hostname from the host that runs PyRIT, and confirm with your network team whether a proxy or private endpoint route applies
Oversized encoded prompt rejectedPrompt Shield limits how many characters one userPrompt or document field can hold, and ASCII art or other encoded prompts can exceed that limitSplit the prompt across fields where the target allows it, shorten the payload, or use a shorter encoding; confirm the current field limit in the PyRIT Prompt Shield Target documentation
Every prompt scored as detected or not detectedThe Prompt Shield was configured as a target when the test expected a scorer result, or the reverse, so the output does not match the roleConfirm whether the Prompt Shield sits in the target slot or the scorer slot, then check that the scorer's true/false output is read as the jailbreak detection result
Scorer results do not match the target responsesThe scorer receives a different piece of the conversation than the one the target answered, often after a converter changed the promptTrace one conversation in memory, confirm which piece the scorer reads, and align the scoring input with the text the target actually received

What a Prompt Shield detection result does and does not show

A Prompt Shield detection result tells you one narrow thing: whether the Azure AI Content Safety endpoint you called flagged this specific input as a jailbreak attempt under the configuration it ran with. It does not tell you whether the model behind your application refused, leaked, or complied. A prompt can be missed by Prompt Shields and still be refused by the model, or flagged and still produce a harmful answer in a system that has no shield in front of it. Treat the detection as one signal in a chain, and say so in your report so readers do not read a pass as a safety verdict.

To get a finding you can defend, pair every Prompt Shield result with the model response it was attached to and with the score that response received from a PyRIT scorer. When the target is used as a true/false scorer inside an attack, record which input was scored, which turn it belonged to, and what the attack did next. A flagged prompt with a compliant reply and an unflagged prompt with a refusal are both worth a finding, because they show where the layers disagree. Confirm with your own team how your application orders its filters, since a shield that runs after the model or on a different field changes what the result means.

Record the configuration with each run so the result can be repeated. Keep the endpoint name, the Content Safety resource and API version you used, the PyRIT version, the model and deployment behind the application, the seed prompts and converters, and the exact scorer settings. Store these alongside the conversation records in PyRIT memory. Note any input-length handling you applied, since a prompt truncated or split to fit field limits is a different test from the original. Without this record, a later rerun may produce a different count and no one will know why.

How DeployOpen scopes and delivers PyRIT testing

DeployOpen approaches Prompt Shields testing as a written, fixed-scope engagement: the Content Safety endpoint, the PyRIT target or scorer role, and the field limits that affect your encoded prompts are agreed before any test runs. The result is a test harness and evidence set your own team can rerun against your deployment.

How we scope

  • We confirm which Content Safety endpoint and deployment will be tested, and who owns it.
  • The written scope states whether Prompt Shields runs as a PyRIT target, a true/false scorer, or both.
  • Engagements are fixed-scope, and ongoing support is available as a retainer once the scope is agreed.

How we build

  • We set up PyRIT in your environment and connect it to a deployed Content Safety endpoint you control.
  • We map which converters and attack paths produce userPrompt or document fields that may exceed the character limit.
  • We check each jailbreak detection result against known test prompts before relying on it for findings.

How we hand over

  • You receive the PyRIT configuration, test prompts, and memory exports so results can be reproduced.
  • The report records detection results, field-length failures, and the endpoint settings used for each run.
  • Your team receives a walkthrough of how to rerun the tests after a Content Safety or PyRIT upgrade.

How we support

  • Retesting after fixes is included in the written scope where agreed, so you can confirm changes.
  • Retainers cover PyRIT upgrades, rerunning the Prompt Shields tests, and reviewing new results.
  • Before a quote, confirm with us which Content Safety regions and API versions your environment uses.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.