arXiv(opens in a new tab)cs.CRAug 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2608.24857

Prompt Structure Redistributes, Not Reduces: An Empirical Analysis of Security-Weaknesses in LLM-Generated Python Code

Maitreyee Das Urmi, Jessica Pourleyli, Fabio Santos, Glaucia Melo

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Maitreyee Das Urmi*, Jessica Pourleyli*, Fabio Santos†, Glaucia Melo*

  • Toronto Metropolitan University, Toronto, Canada

{maitreyee.urmi, jessica.pourleyli, glaucia}@torontomu.ca

† Colorado State University, Fort Collins, USA

fabio.deabreusantos@colostate.edu

Abstract—Large Language Models (LLMs) increasingly generate code from natural-language prompts, making prompt engineering a key mechanism for shaping the security of generated software. Structured and security-oriented prompts are widely used to encourage safer code, yet their effects extend beyond whether detected weaknesses are simply present or absent. Using 424 security-sensitive Python tasks, we generate solutions with GPT-4o and LLaMA 3.1-8B under five prompt variants that progressively add structural and security guidance, and evaluate them with Bandit and CodeQL along two axes: generation compliance and security weakness prevalence, severity, and CWE distributions. Structured prompting substantially reduces refusals (e.g., GPT-4o invalid outputs drop from 338 of 424 to 37-52), enabling large-scale analysis, but security-oriented refinements do not consistently reduce overall weakness prevalence. For GPT-4o, stronger prompts primarily redistribute risk: high-severity findings fall (20.8%→13.6%) while low-severity findings rise (32%→43.5%); LLaMA shows weaker, less consistent shifts. We also observe security-driven semantic drift, where stricter prompts silently remove or rewrite explicitly requested unsafe constructs. Overall, prompt structure improves compliance but is an unreliable substitute for robust security controls in LLM-assisted development.

Index Terms—large language models, software security, prompt engineering, vulnerability detection

I. INTRODUCTION

Large Language Models (LLMs) have rapidly transformed modern software engineering by enabling developers to generate, refactor, and analyze code via natural-language instructions. However, as LLMs are increasingly integrated into production pipelines, their outputs are not free from risk. Studies have shown that these models often produce inconsistent or error-prone code, requiring manual verification and iteration to ensure correctness [1], [2]. More critically, when prompts are vague or underspecified, LLMs may introduce unsafe imports, weak authentication logic, or injection vulnerabilities, raising important questions about how prompt design impacts code security.

Prompt engineering has emerged as a key factor influencing model performance [3]. Structured prompts that incorporate explicit context, constraints, or role definitions have been shown to improve code quality, readability, and maintainability, particularly when combined with persona-driven prompting or semantic patterning. However, existing studies largely emphasize functional correctness rather than the security properties of generated code. At the same time, research on prompt robustness demonstrates that LLM outputs are highly sensitive to prompt phrasing: even minor perturbations can yield unstable or incorrect responses [4], and adversarially crafted prompts can introduce backdoors into prompt-based models [5]. Together, previous research reveals both the power and fragility of prompt design, suggesting an overlooked opportunity: if structured prompting can improve correctness and consistency, it may also shape how security weaknesses manifest during code generation, influencing their distribution even when overall prevalence remains largely unchanged.

To address that, rather than treating prompts solely as interfaces for improving accuracy or efficiency, we investigate them as potential mechanisms for influencing the security characteristics of generated code and find that the dominant effect is not reduction but redistribution. We inquire into the prevailing aggregate-reduction narrative in secure code generation research: prior work often reports decreases in static-analysis findings under security-oriented prompting and concludes that prompting improves security. To test this hypothesis, we design an empirical study that systematically assesses prompt structures for identical programming tasks, generates code snippets using representative proprietary and open-weight LLMs, and evaluates them using Bandit 1^{1} and CodeQL 2^{2}, static analysis tools for security-weakness pattern detection. Our goal is to characterize how prompt structure influences both the likelihood that models generate code for security-sensitive tasks and the severity of security weaknesses in the resulting outputs, thereby establishing an evidence-based understanding of what prompt engineering can and cannot achieve for secure code generation.

This work addresses the following research questions, each motivated by the need to understand how prompt structure shapes the prevalence, severity, and distribution of security weaknesses in LLM-generated code.

1^{1}https://bandit.readthedocs.io/en/latest/index.html

2^{2}https://codeql.github.com/

RQ1: How does prompt structure affect code-generation compliance for security-sensitive tasks?

Prior work shows that structured prompts can improve correctness and maintainability [1], [2] and that prompt formulation can meaningfully affect model behaviour [3], [4]. We measure model compliance by distinguishing between valid, executable Python code and refusal/non-code outputs, where refusals are plain-text responses in which the model declines to generate code for security-sensitive programming tasks that could lead to insecure implementations.

RQ2: Conditioned on valid code, how does prompt structure affect security risk in generated outputs?

Conditioned on valid code, we evaluate whether a security-oriented prompt structure changes the prevalence, severity, and CWE distributions of security weaknesses in the resulting code.

Our findings highlight important limitations of prompt-level security guidance and motivate the need for complementary mitigation mechanisms. In summary, this study contributes:

  1. A two-stage evaluation framework that separates code-generation compliance from downstream security analysis, revealing that prompt refinements can substantially affect the population of analyzable outputs and thereby confound weakness comparisons.

  2. Empirical evidence that structured prompting is primarily a compliance lever, dramatically reducing refusals for GPT-4o but not consistently for LLaMA 3.1-8B.

  3. A semantic-drift audit as a new analytical lens, showing that security-guided prompts silently alter explicitly requested constructs in ways that satisfy static analyzers while potentially violating functional requirements.

  4. A characterization of prompt-level security effects, showing that structural, security, framework, and adversarial guidance influence compliance and security weaknesses in distinct ways rather than producing uniform improvement.

  5. A publicly available replication package containing all prompts, generated outputs, and static-analysis results across both models and five prompt variants, enabling independent verification and reuse [6].

References (27)