Structured prompts cut refusals but shift weaknesses to lower severity
Prompt structuring redistributes, not reduces, security weaknesses in LLM-generated Python code
arXiv 25 Aug 2026 Measurement Simulation evidence
Published as “Prompt Structure Redistributes, Not Reduces: An Empirical Analysis of Security-Weaknesses in LLM-Generated Python Code”
Why it matters
Prompt engineering can shift the severity and composition of security risks but cannot replace dedicated security analysis, especially for high-frequency weaknesses that persist across prompt styles.
Summary
Structured prompting dramatically reduces refusal rates for GPT-4o (invalid outputs drop from 338 to 44) but has inconsistent effects for LLaMA 3.1-8B, where refusals sometimes increase again. Across prompt variants, GPT-4o shows a shift toward lower-severity security findings (high-severity drops from 20.8% to 13.6%) while overall vulnerability prevalence changes only modestly. The study reveals that prompt engineering redistributes, rather than eliminates, security weaknesses, and that dominant CWE classes (e.g., CWE-78, CWE-502) remain common regardless of prompt style. Evidence is based on a controlled experiment with 424 security-sensitive Python tasks evaluated by Bandit and CodeQL.
What they found 2
- Structured prompting (Prompt 1.0) cuts GPT-4o invalid outputs from 338 to 44.
“Introducing structured prompting in Prompt 1.0 leads to a sharp shift in generation behaviour for GPT-4o. The number of invalid outputs decreases from 338 to 44, while the number of valid Python files increases accordingly.”
- High-severity Bandit findings for GPT-4o drop from 20.8% to 13.6% when moving from Prompt 1.0 to Prompt 4.0.
“For GPT-4o, the proportion of high-severity findings decreases from 20.8% under Prompt 1.0 to 13.6% under Prompt 4.0.”
Caveats 5
- LLaMA 3.1-8B shows inconsistent compliance gains: after Prompt 1.0 reduces invalid outputs from 126 to 51, invalid outputs rise again in Prompts 2.0-4.0 (115, 89, and 150 files, respectively).
- For LLaMA 3.1-8B, the proportion of HIGH-severity findings remains stable across prompts (21.2%, 22.9%, 21.5%, 24.4%), indicating no clear severity reduction.
- Consequently, security weakness prevalence derived from Bandit and CodeQL should be interpreted as a lower-bound estimate of security risk.
- We limited the study to five variants because the goal was to compare a small set of interpretable prompting strategies rather than exhaustively explore the prompt design space.
- While this limits variance estimation, it enables a clean isolation of prompt-structure effects.
What’s new here 4
- Separates code-generation compliance from downstream security analysis to isolate prompt effects.
- Shows that structured prompts mainly affect refusal behavior and severity distribution, not overall vulnerability prevalence.
- Introduces a semantic-drift audit to detect when security-oriented prompts cause models to replace unsafe constructs with safer alternatives.
- Evaluates both a proprietary high-capacity model (GPT-4o) and an open-weight model (LLaMA 3.1-8B) across five systematic prompt variants.