L ong S afety: Enhance Safety for Long-Context LLM s (2025)

Abstract: Recent advancements in model architectures and length extrapolation techniques have significantly extended the context length of large language models (LLMs), paving the way for their application in increasingly complex tasks. However, despite the growing capabilities of long-context LLMs, the safety issues in long-context scenarios remain underexplored. While safety alignment in short context has been widely studied, the safety concerns of long-context LLMs have not been adequately addressed. In this work, we introduce ${textbf{LongSafety}}$, a comprehensive safety alignment dataset for long-context LLMs, containing 10 tasks and 17k samples, with an average length of 40.9k tokens. Our experiments demonstrate that training with LongSafety can enhance long-context safety performance while enhancing short-context safety and preserving general capabilities. Furthermore, we demonstrate that long-context safety does not equal long-context alignment with short-context safety data and LongSafety has generalizing capabilities in context length and long-context safety scenarios.

## introduction recently, thanks to more advanced model architectures @xcite and length extrapolation @xcite , the context length of large language models (llms) has been extended significantly @xcite . although the capabilities of long-context llms have attracted widespread attention @xcite @xcite , their safety issues have been seldom discussed. @xcite introduces manyshotjailbreak and first reveals safety issues in long-context llms. despite safety alignment for short-context scenarios has been widely discussed @xcite , long-context safety alignment still lacks depth-in analysis @xcite . first, there is a lack of clarification of longcontext safety scenarios since existing work has only revealed issues in specific safety tasks for long-context llms @xcite . second, from a training perspective, there is also a lack of improvement attempts in long-context safety. unlike short-context safety, which has developed various aligned training corpora @xcite @xcite , long-context safety lacks corresponding solutions and analysis on whether long-context safety can be generalized from shortcontext safety alignment @xcite . correspondingly, from an evaluation perspective, there is also a lack of open-sourced, multi-task benchmarks for long-context safety. to address them, we first categorize the possible scenarios of long-context safety tasks given the harmfulness of context, namely query harmful (qh), partially harmful (ph), and fully harmful (fh). then, we introduce longsafety, a longcontext safety alignment dataset, constructed by three scalable pipelines specialized for three longcontext safety scenarios. longsafety includes 17k samples and 8 tasks, covering the above scenarios, with an average length of 40.9k tokens. we use longsafety to train several mainstream longcontext llms and evaluate its effectiveness. to assess the long-context safety performance of llms, we also propose longsafetybench, an evaluation dataset that includes 1k test samples and 10 tasks, covering both in-domain and out-of-domain (ood) tasks for longsafety, with an average length of 41.9k tokens. the evaluation is conducted in a multiple-choice format with two metrics, har-mawareness (ha), representing llm's ability to recognize harmful information, and saferesponse (sr), indicating the llm's ability to provide a safe response after recognizing harmful information. the task categorization of longsafety and longsafetybench from the perspective of longcontext scenarios can be found in experiments show that training with longsafety enhances both long-context and short-context safety while maintaining general capabilities. compared with other common safety alignment datasets, longsafety can enhance safety in both long and short contexts, when mixed with long-context alignment data. further analysis also shows that longcontext safety can not be achieved by mixing shortcontext safety and long-context alignment data. moreover, training with longsafety can also enhance the safety performance on ood tasks in longsafetybench and contexts longer than the tuning context length, thus demonstrating a certain level of generalization. in summary, the main contributions of our work are as follows: ## method in this section, we first clarify long-context safety scenarios and then discuss the methodology in longsafety, involving data construction pipeline, training dataset, and evaluation benchmark. compared with short-context scenarios, long-context safety faces more information and as noted by @xcite and @xcite , llms are more susceptible to attacks in long-context scenarios. based on the harmfulness of context in these attacks, we classify long-context safety into the following three categories, as shown in figure 1: query harmful, where the context itself is harmless, but the question is misleading, partially harmful, where partial contexts contain harmful information, fully harmful, where the entire context contains harmful information. to produce alignment and evaluation data, we design three data construction pipelines for these three long-context safety scenarios respectively. ## conclusion in this work, we conduct an in-depth analysis of long-context safety issues. we first categorize longcontext safety scenarios into three types and design corresponding data construction pipelines for each type. based on these pipelines, we design a series of tasks and introduce longsafety, a safety alignment dataset designed specifically for longcontext llms. given the need for effective evaluation methods for long-context safety, we propose longsafetybench, an evaluation benchmark tailored for the safety of long-context llms. we conduct a series of experiments to investigate the effectiveness, necessity, and efficiency of longsafety. our experiments demonstrate that training with longsafety significantly enhances the safety performance of these llms without sacrificing performance on general tasks, whether they involve long or short contexts. at the same time, using longsafety for long-context safety alignment demonstrates some degree of generalizability, both in terms of tasks and context length. in the future, further refinement of longsafety and longsafetybench, particularly by incorporating additional safety-related tasks and diverse harmful scenarios, will be crucial for enhancing the safety performance of long-context llms in realworld applications.