arXiv:2610.41878cs.SEOct 2026

Manuscript Offprint

arXiv:2610.41878

No More Import Errors: Constrained Decoding for Guaranteed Valid Imports in LLM-Generated Tests

Konstantinos Kitsios, Matteo Biagiola, Guido Salvaneschi, Alberto Bacchelli

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

Konstantinos Kitsios1^{1}, Matteo Biagiola2,3^{2,3}, Guido Salvaneschi2^{2}, Alberto Bacchelli1^{1}

1^{1}University of Zurich, Zurich, Switzerland

konstantinos.kitsios@uzh.ch, bacchelli@ifi.uzh.ch

2^{2}University of St. Gallen, St. Gallen, Switzerland

{guido.salvaneschi, matteo.biagiola}@unisg.ch

3^{3}Università della Svizzera italiana, Lugano, Switzerland

matteo.biagiola@usi.ch

Abstract—One of the most common failure reasons of LLM-based test generation pipelines is import errors, e.g., the LLM attempts to import a class that either does not exist or lives in a different package. Grammar constrained decoding can modify the probabilities of the LLM's final layer during token generation to guarantee valid imports. However, we find that it can lead to degenerate solutions, i.e., repetitive valid imports (import streaks) that exhaust the token budget before a test is generated. We propose Constrained Beam Decoding with Lookup (CBDL), a novel method that guarantees import validity while addressing import streaks by avoiding premature commitment to tokens that lead to repetitive imports. Because it operates at the decoding stage, CBDL can be seamlessly integrated into existing LLM-based test generation pipelines.

We implement CBDL for Python and Java and empirically evaluate it on two test generation tasks, showing that generated tests have 100% valid imports, outperforming both static (RAG and LIBRO) and dynamic (Agentic Repair) import-validity approaches that leave 45%, 10%, and 43%, of import errors unresolved. CBDL converts on average 14% of initial import errors into functionally correct tests, increasing overall test correctness by 1–4 percentage points over unconstrained generation.

Even when import validity does not directly lead to a correct test, it allows agentic repair pipelines to allocate the repair budget to remaining errors, increasing correct tests by 2–7 percentage points for Python and 1–6 for Java. Finally, we show that a hybrid pipeline, where a small local model equipped with CBDL generates imports while the frontier GPT-5.2 model generates the test body, outperforms generating both with GPT-5.2 alone by 1–3 percentage points.

Overall, CBDL removes a major bottleneck in LLM-based test generation: by guaranteeing import validity with only a small overhead (16–21 seconds per test), it improves test correctness and enables cost-effective hybrid generation.

Index Terms—testing, constrained decoding, import errors

I. INTRODUCTION

Large language model (LLM)-based test generation pipelines generate tests by feeding the code-under-test, related context, and a task-specific prompt to an LLM (or LLM-based agent), which generates a test consisting of import statements, a test prefix, and assertions. Then, the test is executed to check if it satisfies the target task, e.g., by failing in the buggy and passing in the patched code (F→P), in which case the test is returned as output, otherwise it is discarded or refined in an agentic repair loop.

Such pipelines are of increasing interest both in research and practice [1]. For example, Google generates issue-reproducing tests by feeding an issue description to an LLM pipeline and checking whether the generated test fails in the presence of the issue and passes in its absence, in which case the test is considered an issue-reproducing test, or an F→P test [2]. Similarly, Mozilla deployed a tool that is triggered on pull request (PR) creation and feeds the linked issue and patch to an LLM-based pipeline to generate an F→P test that guards against future regressions [3]. Finally, Meta uses an LLM pipeline to generate tests that pass in the current version of the code and improve the coverage of the existing test suite [4].

However, such pipelines often fail to generate an appropriate test. Literature [5]–[11] consistently ranks import errors as one of the most prominent causes for this failure. An import error occurs when the LLM-generated test imports an entity (e.g., class, module, package) that does not exist in the runtime environment. For example, running import com.tools.checkstyle.utils.CommonUtil yields an import error if the CommonUtil class does not exist or resides in a different package. Chen et al. [6] analyzed eight state-of-the-art LLM-based agents and found that import errors were the leading cause of failure, accounting for 1515 (23%) errors across all agents and dataset instances, followed by type errors at 992 (15%). Our results corroborate these findings: even the state-of-the-art GPT-5.2 produces an import error in 18/103 Java test generation tasks.

Motivated by the prevalence of this problem, we develop an approach to guarantee import validity for existing LLM-based test generators. We first observe that all the aforementioned pipelines have access to the execution environment, which allows for valid imports to be statically extracted. We find that the number of valid import statements in popular Python and Java projects is on the order of 100 000, making it impractical to include them directly in the LLM prompt. Indeed, such long prompts would result in “lost-in-the-middle” effects [12] that degrade performance without offering guarantees that the LLM would follow instructions [13].

To guarantee import validity efficiently we formulate this problem as a constraining problem: generated import statements must belong to a set extracted from the execution

Figure

(a) Task 1: F→P test generation from issue description and patch.

Fig. 1. The two test generation tasks used for evaluation.

environment. However, we find that enforcing this constraint through greedy grammar constrained decoding [14] leads to import streaks, i.e., degenerate solutions where the model repeatedly generates valid imports without proceeding to the test body, aligned with prior findings on low quality output of grammar constrained decoding [15], [16]. Our analysis suggests that import streaks are caused by prematurely committing to a locally optimal import, motivating Constrained Beam Decoding with Lookup (CBDL). By retaining alternative continuations, beam search avoids local optima, substantially reducing import streaks. A complementary lookup mechanism preserves generated class names when they exist under a different package and activates constrained decoding only when lookup cannot resolve the import. Together, these components address both import validity and the repetitive outputs introduced by enforcing it.

We implement CBDL for Java and Python and empirically evaluate how it improves two existing test generation methods [3], [17] across two test generation tasks, namely F→P test generation and regression test generation, using three LLMs. We find that CBDL guarantees that all imports are valid, i.e., that the imported entity exists in the runtime environment. In contrast, other import validity approaches, like retrieval-augmented generation (RAG), LIBRO's dependency resolution [18], and Agentic Repair [19], leave 45%, 10%, and 43% of import errors unresolved, respectively. CBDL also converts 14% of the import errors into functionally correct tests on average, leading to an overall increase in functional correctness of 1–4 percentage points compared to unconstrained generation. Even when the tests are not functionally correct, the absence of import errors helps subsequent repair attempts focus on the remaining errors, leading to 1–7 percentage points increase in functional correctness over unconstrained generation.

Furthermore, delegating import generation to a local CBDL-equipped model while generating the test body with the frontier GPT-5.2 yields more correct tests than generating both with GPT-5.2. Finally, we show that CBDL adds an average overhead of 16–21 seconds per test as it enforces validity at generation time instead of relying on costly repair loops. Overall, our work eliminates a major failure reason in LLM-based test generation through a novel, guarantee-based approach, leading to the following contributions:

  • A formulation of import validity as constraining problem.
  • CBDL, a novel approach that guarantees import validity, mitigates import streaks via beam search, and can be integrated with existing LLM-based test generators.
  • Empirical evidence that CBDL outperforms five static and

Figure

(b) Task 2: Regression test generation with implementation as oracle.

dynamic import-validity approaches in terms of import validity and test correctness.

  • Empirical evidence that letting a small local model with CBDL write the imports and a remote frontier LLM write the test body outperforms fully remote generation.

Algorithm 1: Unconstrained LLM Generation.

Input: Language model fθf_\theta, prompt cc Output: Generated sequence yy

1 y←[[y \leftarrow [[; 2 while True do 3 compute next-token distribution Pt=fθ(c,y)P_t = f_\theta(c, y); 4 select next token yt=arg⁡max⁡(Pt)y_t = \arg\max(P_t); 5 append yty_t to yy; 6 if yty_t is EOS or limit reached then 7 break; 8 end 9 end 10 return yy

References (47)