Abstract
Prompting with natural language instructions has recently emerged as a popular method of harnessing the capabilities of large language models (LLM). Given the inherent ambiguity present in natural language, it is intuitive to consider the possible advantages of prompting with less ambiguous prompt styles, like pseudo-code. In this paper, we explore if prompting via pseudo-code instructions helps improve the performance of pre-trained language models. We manually create a dataset of pseudo-code prompts for 132 different tasks spanning classification, QA, and generative language tasks, sourced from the Super-NaturalInstructions dataset. Using these prompts along with their counterparts in natural language, we study their performance on two LLM families - BLOOM, CodeGen. Our experiments show that using pseudo-code instructions leads to better results, with an average increase (absolute) of 7-16 points in F1 scores for classification tasks and an improvement (relative) of 12-38% in aggregate ROUGE-L scores across all tasks. We include detailed ablation studies which indicate that code comments, docstrings, and the structural clues encoded in pseudo-code all contribute towards the improvement in performance.
Introduction
Natural language (NL) serves as the primary interface for Large Language Models (LLMs), yet it is inherently prone to ambiguity and under-specification. This often necessitates extensive "prompt engineering" to achieve desired outcomes. Conversely, modern LLMs are heavily pre-trained on structured, logical codebases like GitHub. This research investigates whether reformatting instructions into pseudo-code—utilizing function prototypes, docstrings, and structured logic—can reduce ambiguity and help models better follow complex instructions for non-coding tasks.
Methodology
The researchers developed a systematic schema to translate standard natural language instructions into pseudo-code prompts. This schema consists of four main components:
- Function Prototype: Defines the task name, input variables, and expected return types (e.g.,
def generate_sentiment(sentence: str) -> str:). - DocString: A high-level description of the task, providing semantic context.
- Function Definition: The core logic of the prompt, describing sub-steps using code-like constructs such as if/else statements and sub-function calls.
- Pre-processor: An executable snippet used to format raw dataset inputs into the arguments required by the pseudo-code function.
Dataset and Evaluation
The study sampled 132 diverse NLP tasks from the Super-NaturalInstructions dataset, covering 28 categories and 72 unique datasets. These tasks were categorized into classification, question answering, and generative language tasks.
Performance was evaluated using two model families: BLOOM (general-purpose) and CodeGen (code-optimized), with model sizes ranging from 2B to 7B parameters. To ensure statistical robustness, 1,000 instances were sampled per task.
Key Results and Insights
The quantitative improvements were substantial across all task types:
- Classification: An absolute increase of 7–16 points in F1 scores.
- Generative/QA: A relative improvement of 12–38% in aggregate ROUGE-L scores.
Beyond raw metrics, the study revealed that pseudo-code acts as a reasoning scaffold, helping models decompose tasks through structural cues like indentation and logic flow. A significant finding was the "Code-Model Synergy": CodeGen, the model specifically trained on programming tasks, saw the most dramatic gains. Furthermore, the researchers found that 0-shot pseudo-code prompts often outperformed 2-shot natural language prompts, suggesting that structural clarity can be more valuable than additional examples.
Conclusion and Future Work
This work demonstrates that pseudo-code prompts can significantly enhance the performance of pre-trained LMs by leveraging their exposure to structured code during training. While effective, the authors note that writing these prompts requires technical expertise, which may limit accessibility for non-programmers. Future research will explore the efficacy of this approach on larger models (e.g., GPT-4) and the potential for "Pseudo-Code Chain-of-Thought" reasoning.
Summary updated