--- 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

๐Ÿ›ก๏ธ SafetyNIAH

*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)
The official benchmark of **"LongGuard: Mechanistic Analysis and Training-Free Mitigation of Long-Context Failure in Safety Guardrails" (EMNLP 2026 Main)**.
A guardrail catches an unsafe request in a short context and misses the same request once it is embedded in a long one
## ๐Ÿ“– 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.
Composition of SafetyNIAH across data sources, audited side, label, haystack type, context length and needle position
## ๐Ÿ“ฅ 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.