> 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/injec-guard.md).

# PIGuard

Binary prompt-injection classifier trained to mitigate over-defense.

Runs PIGuard's DeBERTa-v3 encoder classifier over a single user prompt and reports whether the text is a prompt-injection attempt. The model is a two-class sequence classifier whose unsafe class is labeled `"injection"`; the guardrail treats that class as the risky one. PIGuard adds the "Mitigating Over-defense for Free" (MOF) training strategy (ACL 2025), which reduces the trigger-word bias that makes prompt-injection guards falsely flag benign inputs.

Expected input: prompt-only text. `validate(input_text)` accepts a single string, or a `list[str]` to classify a batch in one call; there is no prompt+response or chat-message mode.

Verdict mapping onto `GuardrailOutput`:

* `valid` is `True` when the predicted class is not `"injection"` (i.e. the text looks safe).
* `score` is the model's probability of the `"injection"` class (canonical risk direction: higher = riskier).
* `categories` carries one `CategoryResult` per class label, each with its softmax `score` and a `triggered` flag marking the argmax class.
* No `spans` or `modified_text` are produced.

`leolee99/PIGuard` is the renamed, maintained successor to InjecGuard (the rename was for licensing reasons); `leolee99/InjecGuard` is the original repository, kept for backward compatibility. Both share the same DeBERTa-v3 architecture and `"injection"` label and ship custom model code, so the default provider loads them with `trust_remote_code=True`.

For more information, see:

* [PIGuard model card](https://huggingface.co/leolee99/PIGuard) (default)
* [InjecGuard model card](https://huggingface.co/leolee99/InjecGuard)
* [InjecGuard: Benchmarking and Mitigating Over-defense in Prompt Injection Guardrail Models (arXiv:2410.22770)](https://arxiv.org/abs/2410.22770)

## Supported Models

* `leolee99/PIGuard`
* `leolee99/InjecGuard`

## Constructor

| Parameter  | Type                                                 | Required | Default | Description                                                                                                                                                                                                                                                                                                                     |
| ---------- | ---------------------------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model_id` | \`str                                                | None\`   | No      | `None`                                                                                                                                                                                                                                                                                                                          |
| `provider` | `Optional[Provider[dict[str, Any], dict[str, Any]]]` | No       | `None`  | Execution backend that loads the model and runs inference. Defaults to a `HuggingFaceProvider` constructed with `trust_remote_code=True` (PIGuard ships a custom model class), targeting `AutoModelForSequenceClassification`. Supply your own to control device, dtype, or `cache_dir`, or to run against a different backend. |

Initialize the PIGuard 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.8       | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |
| notinject (unspecified)    | fpr    | native-valid | 0.0842105 | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 | ⚠️      |
| gandalf (unspecified)      | recall | native-valid | 0.955357  | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |
| bipia\_email (unspecified) | f1     | native-valid | 0.863755  | bir\@fd86c16            | measured:bir\@fd86c16            |         |
| bipia\_table (unspecified) | f1     | native-valid | 0.908286  | bir\@fd86c16            | measured:bir\@fd86c16            |         |

## License

* **Vendor:** leolee99
* **Default license:** `mit` (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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
