| --- |
| 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?* |
|
|
| []() |
| []() |
| [](https://creativecommons.org/licenses/by-nc/4.0/) |
| [](#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. |
|
|