Discrete Prompt Optimization Using Genetic Algorithm for Secure Python Code Generation

Pintor, Maura
Second
;
2026-01-01

Abstract

Large language models (LLMs) have become powerful tools that enable novice developers to generate production-level code. However, research has highlighted the security risks associated with such code generation, due to the high volume of generated software vulnerabilities. Recent studies have explored various techniques for automatically optimizing prompts to elicit desired responses from LLMs. Among these methods, Genetic Algorithms (GAs), which search for optimal solutions by evolving an initial population of candidates through iterative mutations, have gained attention as a lightweight and effective prompt optimization approach that does not require large datasets or access to model weights. However, their potential has not yet been examined in the context of secure code generation. In this paper, we use GA to develop a discrete prompt optimization pipeline specifically designed for secure code generation. We introduce two domain-specific prompt mutation techniques and assess how incorporating these security-focused mutations alongside general-purpose techniques, such as back translation and paraphrasing, affects the security of Python code generated by LLMs. Results demonstrate that our security-specific mutation techniques led to prompts with richer security context compared to the generic mutation techniques. Furthermore, combining these techniques with generic mutations substantially reduced the number of security weaknesses in the LLM-generated code. We also observed that prompts optimized for a particular LLM tend to perform best on that same model, highlighting the importance of model-specific prompt optimization.
2026
2025
Inglese
232
112682
1
24
24
Esperti anonimi
scientifica
LLMs;L Secure code generation; Prompt optimization; Genetic algorithms
Tony, Catherine; Pintor, Maura; Kretschmann, Max; Scandariato, Riccardo
1.1 Articolo in rivista
info:eu-repo/semantics/article
1 Contributo su Rivista::1.1 Articolo in rivista
262
4
open
   Cybersecurity for AI-Augmented Systems
   Sec4AI4Sec
   European Commission
   Horizon Europe Framework Programme
   101120393
Files in This Item:
File Size Format  
1-s2.0-S0164121225003516-main_2.pdf

open access

Type: versione editoriale
Size 4.36 MB
Format Adobe PDF
4.36 MB Adobe PDF View/Open

Items in DSpace are protected by copyright, with all rights reserved, unless otherwise indicated.

Questionnaire and social

Share on:
Impostazioni cookie