> For the complete documentation index, see [llms.txt](https://docs.mozilla.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mozilla.ai/any-guardrail/api-reference/index/prompt-injection/prompt-guard.md).

# Prompt Guard 2

Encoder classifier for prompt-injection and jailbreak detection.

Binary encoder classifier (mDeBERTa / DeBERTa) that labels a single prompt string `benign` (index 0) or `malicious` (index 1, i.e. a prompt-injection or jailbreak attempt). Meta's v2 collapsed Prompt Guard 1's separate "injection" class and focuses on explicit jailbreak / injection attacks. It screens prompt text only — it is not designed to judge model responses.

Verdict mapping onto `GuardrailOutput`:

* `valid` is `True` when the predicted class is `benign` (argmax index != 1).
* `score` (canonical risk: higher = riskier) is the `malicious` probability.
* `categories` carries one `CategoryResult` per class, each with its probability and a `triggered` flag on the predicted class. The gated repos publish only the generic `LABEL_0` / `LABEL_1` names, so categories fall back to those when the provider surfaces no label list.

Expected inputs: a single prompt string, or a `list[str]` for batched classification (the inherited `validate` dispatches list input to `_validate_batch`). The 86M default is multilingual; the 22M variant is English-only.

The repos are gated under the Llama 4 Community License — accept the terms on the model page and authenticate with `hf auth login` before first use.

For more information, please see the model cards:

* [Llama-Prompt-Guard-2-86M](https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M) (default) — mDeBERTa-base, multilingual.
* [Llama-Prompt-Guard-2-22M](https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-22M) — DeBERTa-xsmall, English, \~75% lower latency.

## Supported Models

* `meta-llama/Llama-Prompt-Guard-2-86M`
* `meta-llama/Llama-Prompt-Guard-2-22M`

## Constructor

| Parameter  | Type                                                 | Required | Default | Description                                                                                                                                                             |
| ---------- | ---------------------------------------------------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model_id` | \`str                                                | None\`   | No      | `None`                                                                                                                                                                  |
| `provider` | `Optional[Provider[dict[str, Any], dict[str, Any]]]` | No       | `None`  | Optional pre-configured provider. If `None`, a default `HuggingFaceProvider` (targeting `AutoModelForSequenceClassification`) is built and the model is loaded eagerly. |

Initialize the Prompt Guard 2 guardrail.

## validate

Default validation pipeline: preprocess -> inference -> postprocess.

**Parameters**

| Parameter    | Type  | Required     | Default | Description |
| ------------ | ----- | ------------ | ------- | ----------- |
| `input_text` | \`str | list\[str]\` | Yes     | —           |

**Returns:** `GuardrailOutput | list[GuardrailOutput]`

## Benchmarks

### Prompt Injection

| Dataset (rev)             | Metric | Threshold    | Value     | Harness                 | Source                           | Contam. |
| ------------------------- | ------ | ------------ | --------- | ----------------------- | -------------------------------- | ------- |
| deepset\_pi (unspecified) | f1     | native-valid | 0.235294  | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |
| notinject (unspecified)   | fpr    | native-valid | 0.0315789 | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |
| gandalf (unspecified)     | recall | native-valid | 0.973214  | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |

## License

* **Vendor:** Meta
* **Default license:** `llama-4` (of the default model/service)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mozilla.ai/any-guardrail/api-reference/index/prompt-injection/prompt-guard.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
