PromptPex: Automatic Test Generation for Language Model Prompts

Reshabh K. Sharma, Jonathan de Halleux, Shraddha Barke, Ben ZornarXiv 2025 · DOI

Abstract

As large language model (LLM) prompts become increasingly complex, they function as "code-like artifacts" that require rigorous validation. However, prompt engineering remains largely a manual, "vibe-based" process. This paper introduces PromptPex, an automated framework that treats prompts as programs with formal pre- and post-conditions. PromptPex extracts specifications from prompts, generates challenging test cases—including "inverse rules" to provoke failures—and uses an LLM-as-a-judge to evaluate compliance. Our evaluation across 22 diverse prompts and four models shows that PromptPex achieves 96.8% specification agreement and significantly outperforms standard test generation methods in identifying model regressions and portability issues.

Introduction

In modern AI development, prompts are no longer simple queries; they are logic-heavy instructions that take inputs and produce structured outputs. Despite this, prompts are inherently non-deterministic and tightly coupled to specific models. A prompt optimized for a frontier model like GPT-4 may fail on a smaller, open-source model like Llama-3, leading to "silent failures" or constraint violations.

Figure 2: An example of prompt portability issues where different models produce varying results for the same part-of-speech tagging task.

The lack of automated testing tools makes it difficult for developers to define valid inputs or verify output constraints systematically. PromptPex addresses this gap by bringing unit testing rigor to prompt engineering, allowing for data-driven decisions regarding model suitability and prompt robustness.

Methodology

PromptPex operates as a four-stage pipeline designed to compile natural language prompts into checkable rules.

Figure 3: The PromptPex architectural workflow, illustrating the transition from input prompt to rule extraction, test generation, and automated evaluation.
  1. Specification Extraction: A state-of-the-art model analyzes the Prompt Under Test (PUT) to derive an Input Specification (IS) and a set of Output Rules (OR). These serve as the "assertions" for the test suite.
  2. Inverse Rule Generation: To stress-test the prompt, PromptPex generates "inverse rules"—semantic opposites of the output constraints. This "negative testing" approach is designed to tempt the model into violating its instructions.
  3. Test Generation: Using the IS and OR (including inverse rules), the system generates a diverse set of test inputs.
  4. Automated Evaluation: An LLM judge evaluates whether the model's output complies with the extracted rules, rather than checking against a single "gold" answer.

Experimental Setup

The framework was evaluated using a benchmark of 22 prompts covering tasks such as part-of-speech tagging, sentiment analysis, and creative writing. Portability was tested across four models: gpt-4o-mini, gemma2:9b, qwen2.5:3b, and llama3.2:1b.

Key Results

  • Enhanced Bug Detection: PromptPex consistently identified more non-compliant outputs than standard zero-shot test generators. The use of inverse rules was particularly effective; for gpt-4o-mini, detection rates for non-compliance were significantly higher when using the PromptPex approach.
  • High Fidelity Specification: The "Spec Agreement"—the degree to which extracted rules matched the original prompt's intent—reached 96.8%. This indicates that LLMs are highly capable of self-reflecting on their own logic.
  • Model Portability Insights: The results quantified the trade-offs between model size and reliability. While larger models generally followed instructions better, smaller models like qwen2.5:3b proved surprisingly effective for specific, well-defined tasks.

Discussion and Limitations

While PromptPex provides a robust foundation for prompt testing, several limitations remain. Currently, the system only supports single-string inputs and does not account for structured data or Retrieval-Augmented Generation (RAG) contexts. Furthermore, the "LLM-as-a-judge" component can exhibit "self-bias," potentially favoring outputs from its own model family. Future work will focus on multi-turn conversation support and integrating PromptPex into automated prompt optimization workflows.

Summary updated

BibTeX
@misc{sharma2025promptpex,
  author = {Reshabh K. Sharma and Jonathan de Halleux and Shraddha Barke and Ben Zorn},
  title = {{PromptPex: Automatic Test Generation for Language Model Prompts}},
  year = {2025},
  eprint = {2503.05070},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.05070},
}
Figure