AIO Apex
en

Prompt Guard 2: a calibrated head on frozen embeddings

A logistic-regression head trained on Prompt Guard 2's frozen pooled embeddings detected 99.9% of held-out HackAPrompt injections at a 0.7% false-positive rate on benign prompts, and 59.6% of injections in deepset's test split. The model's representation already separates most injections; its native classification head is the weaker part.

By Mohammad Safari · Last updated

What the numbers do and do not compare

Prompt Guard 2's native classifier detected 22.8% of the injections in the deepset/prompt-injections corpus (662 rows) at a 0.25% false-positive rate. The 99.9% result was measured on a different test set: HackAPrompt injections and Dolly benign prompts. The native classifier was not run on that set, and deepset is also the training source for the new head. So 22.8% and 99.9% are not a before-and-after pair on the same data; the like-for-like figure on deepset is 59.6%.

Why does Prompt Guard 2 miss injections?

Prompt Guard 2 (meta-llama/Llama-Prompt-Guard-2-86M, a DeBERTa-v2 classifier) has low injection recall on deepset. The hypothesis was that this is a limitation of its decision head, not of its encoder: if the frozen 768-dimensional embedding that feeds the head already separates injections from benign text, a simple head trained on it should recover the recall.

Data

DatasetRoleLicenceSize used
deepset/prompt-injectionsTraining (train split), validation (test split)Apache-2.0546 train / 116 test
Lakera/gandalf_ignore_instructionsTraining, injections onlyMIT777
Open-Orca/OpenOrca (questions)Training, benignMIT800 sampled
databricks/databricks-dolly-15kHeld-out benign + threshold calibrationCC BY-SA 3.02,000 calibration + 2,000 test
hackaprompt/hackaprompt-datasetHeld-out injectionsMIT800 per run

The held-out set comes from sources not used for training: HackAPrompt injections (a crowdsourced competition) and Dolly instructions as benign text, unlike the OpenOrca benign prompts used for training. Texts were deduplicated by normalised hash within and across splits, and held-out items within cosine similarity 0.95 of any training item were dropped. Each run used freshly sampled held-out data disjoint from the earlier runs.

Method

  • Embeddings: Prompt Guard 2's pooled output (the representation its native classifier consumes), frozen, inputs truncated at 512 tokens.
  • Head: scikit-learn LogisticRegression with balanced class weights, trained on 2,110 examples (979 injections, 1,131 benign).
  • Threshold: set on separate benign calibration data. In the final run, the 99.5th percentile of head scores on 2,000 Dolly prompts, a 0.5% false-positive budget; the measured rate on the test set was 0.7%.
  • Success bar, fixed before training in an internal planning document: held-out recall of at least 50% at a false-positive rate of at most 1%, with the recall confidence interval clear of the baseline's.

Results

ModelTest set (injections / benign)RecallFalse-positive rateNote
Native Prompt Guard 2 classifierdeepset, all 662 rows (263 / 399)22.8% [18.2, 28.3]0.25% [0.04, 1.41]Baseline
Frozen head, run 5adeepset test split (57 / 45)59.6% [46.7, 71.4]0.0% [0.0, 7.9]In-distribution
Frozen head, run 5aHackAPrompt / Dolly (363 / 499)99.7% [98.5, 100]2.2% [1.2, 3.9]Missed FPR bar
Frozen head, run 5a-bisHackAPrompt / Dolly (800 / 1,999)99.9% [99.3, 100]1.2% [0.8, 1.7]Missed FPR bar
Frozen head, run 5a-terHackAPrompt / Dolly (800 / 2,000)99.9% [99.3, 100]0.7% [0.4, 1.2]Met the pre-set bar

The three held-out runs were made in sequence, each with fresh data and a stricter threshold after the previous one missed the false-positive bar; all three are reported. In the final run the head missed 1 of 800 injections and flagged 14 of 2,000 benign prompts (precision 98.3%, AUROC 0.999).

The embedding matters

The same head and threshold procedure on a general-purpose embedding model recovers far less, which supports the hypothesis that Prompt Guard 2's own representation carries the signal:

Embeddings (run 5a-ter, logistic head)RecallFPRAUROC
Prompt Guard 2 pooled (frozen)99.9% [99.3, 100]0.7%0.999
snowflake-arctic-embed-m (general purpose)29.9% [26.8, 33.1]0.4%0.887

Limitations

  • One held-out injection source (HackAPrompt); results on other attack styles are untested.
  • A static corpus: no claim of robustness against attackers who adapt to the head.
  • The near-perfect recall is specific to HackAPrompt-style injections; on deepset's test split it is 59.6%.
  • The planning document fixing the success bar is not independently timestamped: it first appears in the same commit as the results.
  • The training set was reused across the three runs; only held-out and calibration data were fresh.
  • No per-example error analysis was saved, and the model revision and library versions are not pinned.
  • The threshold should be re-calibrated on real deployment traffic before use.

Reproduce

The full pipeline (corpus build, embedding extraction, the three runs) is in the repository; this is the core, condensed:

import numpy as np, torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from sklearn.linear_model import LogisticRegression

M = "meta-llama/Llama-Prompt-Guard-2-86M"          # gated: needs a Hugging Face token
tok = AutoTokenizer.from_pretrained(M)
model = AutoModelForSequenceClassification.from_pretrained(M).eval()

@torch.no_grad()
def embed(texts, bs=32):
    out = []
    for i in range(0, len(texts), bs):
        e = tok(texts[i:i + bs], padding=True, truncation=True, max_length=512, return_tensors="pt")
        out.append(model.pooler(model.deberta(**e).last_hidden_state).numpy())
    return np.concatenate(out)                      # (N, 768) frozen pooled embeddings

clf = LogisticRegression(max_iter=2000, class_weight="balanced")
clf.fit(embed(train_texts), y_train)
t = np.percentile(clf.predict_proba(embed(calib_benign))[:, 1], 99.5)   # ~0.5% FPR budget
flagged = clf.predict_proba(embed(test_texts))[:, 1] >= t

Upstream

The approach was proposed to Meta as PurpleLlama issue #257 and pull request #263 (an optional calibrated-head mode). As of 23 November 2026 both are open, with no review from a Meta maintainer; the pull request is not merged.

Sources