← Lab /Software

Retrieval-augmented revision improves security of LLM-generated code

Their inference-time retrieval-augmented revision makes LLM-generated code more secure without introducing new vulnerabilities

Software arXiv 2 Mar 2026 Defense Prototype evidence

Published as “Inference-Time Safety For Code LLMs Via Retrieval-Augmented Revision”

Why it matters

The method offers a practical, inference-time safety layer that can be added to existing code assistants to reduce insecure outputs without costly model updates.

Summary

SOSECURE improves the security of LLM-generated code compared to prompting alone and does not introduce new vulnerabilities as measured by static analysis. The system retrieves security-relevant Stack Overflow discussions after code generation and feeds them to the LLM for revision, requiring no model retraining. Evaluation on three datasets (SALLM, LLMSecEval, LMSys) shows higher fix rates than prompt-only baselines while keeping introduction rates at zero. Evidence is based on static analysis tools (CodeQL, Bandit) rather than functional testing.

What they found 1

  1. SOSECURE improves security of generated code without adding new vulnerabilities

    “Across real-world and benchmark datasets, our approach improves the security of LLM-generated code compared to prompting alone, while introducing no new vulnerabilities as measured by static analysis.”

Caveats 4

  • Our evaluation relies on static analysis tools, which are known to exhibit false positives and false negatives.
  • SOSECURE depends on the quality and relevance of retrieved Stack Overflow discussions.
  • Our retrieval mechanism is primarily lexical and does not explicitly model semantic or structural code properties.
  • While we did not observe cases where SOSecure broke functionality in these checks, this process does not provide formal guarantees.

What’s new here 4

  • Retrieves community security discussions after generation (post-generation) to guide code revision.
  • Uses a lightweight BM25 lexical retriever over a curated Stack Overflow knowledge base.
  • Provides safety without any model fine-tuning or retraining.
  • Treats retrieved content as advisory context rather than ground-truth code.