File size: 7,222 Bytes
0df138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e056ad5
0df138c
b576af3
0df138c
 
 
 
 
 
b576af3
 
 
0df138c
 
 
b576af3
 
 
0df138c
 
 
 
e056ad5
0df138c
 
 
 
 
 
 
 
 
 
 
 
e056ad5
0df138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e056ad5
0df138c
 
 
 
 
 
b576af3
0df138c
 
 
 
 
b576af3
0df138c
 
 
 
 
 
 
 
 
 
 
 
e056ad5
0df138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e056ad5
0df138c
 
 
 
 
 
 
 
 
e056ad5
0df138c
 
 
 
 
e056ad5
0df138c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---
license: cc-by-nc-4.0
task_categories:
  - text-classification
language:
  - en
tags:
  - safety
  - guardrail
  - long-context
  - needle-in-a-haystack
  - benchmark
pretty_name: SafetyNIAH
size_categories:
  - 10K<n<100K
configs:
  - config_name: default
    data_files:
      - split: test
        path: SafetyNIAH.parquet
dataset_info:
  features:
    - name: sample_idx
      dtype: int32
    - name: dataset
      dtype: string
    - name: label
      dtype: string
    - name: type
      dtype: string
    - name: target_length
      dtype: int32
    - name: relevance
      dtype: string
    - name: position_pct
      dtype: int16
    - name: prompt
      dtype: large_string
    - name: response
      dtype: large_string
    - name: origin_prompt
      dtype: large_string
    - name: origin_response
      dtype: large_string
    - name: needle_word_count
      dtype: int32
    - name: needle_char_start
      dtype: int32
    - name: needle_char_end
      dtype: int32
    - name: before_wc
      dtype: int32
    - name: after_wc
      dtype: int32
    - name: final_word_count
      dtype: int32
    - name: origin_sample_idx
      dtype: int32
    - name: is_duplicate
      dtype: bool
    - name: pad_skipped
      dtype: bool
  splits:
    - name: test
      num_examples: 30400
---

<!-- TODO before release: fill in the arXiv and GitHub links in the badges below,
     and the BibTeX entry at the bottom. -->

<div align="center">

<h1 align="center" style="font-size: 2.5em; font-weight: 700; margin: 0.2em 0;">🛡️ SafetyNIAH</h1>

*Safety Needle-in-a-Haystack: does a guardrail still find unsafe content when the context grows?*

[![Paper](https://img.shields.io/badge/paper-arXiv-b31b1b.svg)]()
[![Code](https://img.shields.io/badge/code-GitHub-181717.svg?logo=github)]()
[![License](https://img.shields.io/badge/license-CC%20BY--NC%204.0-blue.svg)](https://creativecommons.org/licenses/by-nc/4.0/)
[![Samples](https://img.shields.io/badge/samples-30%2C400-green.svg)](#download)

<table>
<tr><td align="center" style="border:1px solid #ccc; border-radius:8px; padding:12px 20px;">

The official benchmark of **"LongGuard: Mechanistic Analysis and Training-Free
Mitigation of Long-Context Failure in Safety Guardrails" (EMNLP 2026 Main)**.

</td></tr>
</table>

<img src="assets/overview.png" width="88%" alt="A guardrail catches an unsafe request in a short context and misses the same request once it is embedded in a long one" />

</div>

## 📖 Overview

Guardrails are the last line of defence in front of a deployed language model,
yet they are evaluated almost entirely on short text. SafetyNIAH turns that gap
into a controlled experiment: a short unsafe (or safe) sample — the **needle** —
is embedded in a benign Wikipedia **haystack**, and only the surrounding context
changes. The needle's text is byte-identical across every context length, so any
change in the guard's verdict comes from the context alone.

Across six production guardrails, unsafe recall falls from **78.6% at 256 words
to 28.8% at 32k words**, while safe recall barely moves (85.3% → 84.2%). Guards
do not get noisier as context grows; they get **quietly permissive**.

## 🧪 Design axes

30,400 samples fully cross five controlled factors, so any single factor can be
isolated by filtering:

| Axis | Column | Values |
|---|---|---|
| Context length (words) | `target_length` | 256 · 512 · 1k · 2k · 4k · 8k · 16k · 32k |
| Needle position | `position_pct` | 0% · 25% · 50% · 75% · 100% |
| Haystack type | `relevance` | `random` (unrelated filler) · `related` (topically similar filler) |
| Audited side | `type` | `prompt` (user turn) · `response` (assistant turn) |
| Ground truth | `label` | `unsafe` (17,600) · `safe` (12,800) |

`relevance` and `position_pct` exist to rule out the two obvious alternative
explanations, semantic interference and lost-in-the-middle positional bias.
Neither is the driver: the degradation tracks length at every haystack type and
every position.

<div align="center">
<img src="assets/distribution.png" width="100%" alt="Composition of SafetyNIAH across data sources, audited side, label, haystack type, context length and needle position" />
</div>

## 📥 Download

One Parquet file, `SafetyNIAH.parquet`, 30,400 rows, 489 MB.

```python
from datasets import load_dataset

ds = load_dataset("caskcsg/SafetyNIAH", split="test")
```

Or fetch the file directly:

```bash
hf download caskcsg/SafetyNIAH --repo-type dataset --local-dir data
```

If only the design cell is needed, reading the metadata columns skips the long
text fields — a few MB instead of 489 MB:

```python
import pyarrow.parquet as pq

meta = pq.read_table("SafetyNIAH.parquet",
    columns=["sample_idx", "label", "type", "target_length", "relevance", "position_pct"])
```

## 📋 Fields

**What the guard sees**, selected by `type`:

| Field | Description |
|---|---|
| `prompt` | The user turn. For `type="prompt"` this is the padded long context containing the needle. |
| `response` | The assistant turn. For `type="response"` this is the padded long context; empty otherwise. |

For `type="response"` samples the user turn should be `origin_prompt`, the
original short request — the needle is in the answer, not the question.

**Label and design cell**: `label`, `type`, `target_length`, `relevance`,
`position_pct`, `dataset` (the needle's source benchmark), and `sample_idx`, a
stable primary key.

**Provenance and offsets**:

| Field | Description |
|---|---|
| `origin_prompt`, `origin_response` | The needle in its original short form, before padding. |
| `origin_sample_idx` | Needle index, shared by every sample derived from it. |
| `needle_char_start`, `needle_char_end` | Character span of the needle inside the padded field. |
| `needle_word_count` | Length of the needle in words. |
| `before_wc`, `after_wc` | Haystack words inserted before and after the needle. |
| `final_word_count` | Realised word count. |
| `is_duplicate`, `pad_skipped` | Needle text also appears elsewhere / needle was already long enough, so no padding was added. |

The character offsets are what make the paper's attention analysis possible: they
mark exactly which input positions are the needle, so the attention mass a head
places on unsafe evidence can be measured directly.

## 📚 Needle sources

Needles are sampled from 17 public safety benchmarks, with `@input` / `@output`
marking which side of a pair was taken. Each keeps its original license.

`JailbreakBench` · `WildGuardTest` · `XSTest` · `Aegis2.0` · `HarmBench` · `OpenAIModeration` · `ToxicChat` · `WildJailbreak` · `Aegis` · `OR-Bench` · `BeaverTails` · `SafeRLHF` · `SimpleSafetyTests` · `SaladBench` · `AirBench` · `StrongReject` · `S-Eval`

Haystacks are drawn from English Wikipedia, chunked and then screened by multiple
guardrails so that no filler passage carries unsafe content of its own.

## 📄 Citation

```bibtex

```

## ⚖️ License

The benchmark construction, the haystack pipeline and the metadata are released
under **CC BY-NC 4.0**. Needle text remains under the license of its source
benchmark, listed above; check those before redistributing derived subsets.