SafetyNIAH / README.md
czyPL's picture
Add section icons to README
e056ad5 verified
|
Raw
History Blame Contribute Delete
7.22 kB
metadata
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

🛡️ SafetyNIAH

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

Paper Code License Samples

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.

from datasets import load_dataset

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

Or fetch the file directly:

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:

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


⚖️ 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.