AI
New method uses generated unit tests to catch AI code hallucinations and control safety
Researchers propose a selective code generator that abstains from uncertain outputs by automatically validating correctness through dynamic testing.
Code generation models tend to produce logically invalid or nonsensical outputs—a phenomenon known as hallucination—which poses a serious challenge for systems that demand high safety standards. A key difficulty lies in reliably determining whether generated code actually works, since the output is inherently hard to evaluate without execution.
Addressing the hallucination problem#
Researchers have now tackled this bottleneck by leveraging code's executable nature. Rather than trying to predict correctness from the model output alone, they automatically generate unit tests using dynamic code analysis tools to verify whether the generated code behaves as intended. This approach treats functional correctness as a measurable, testable property instead of an abstract quality judgment.
The selective generation framework#
The team proposes a selective code generator that refuses to produce answers when confidence is low. By comparing generated code against dynamically created unit tests, the model can determine when to abstain from returning a result. This controlled abstention allows the system to theoretically bound the false discovery rate—the proportion of incorrect answers among those that are ultimately delivered—providing guarantees about the correctness of non-abstained outputs.
FuzzEval: testing as both evaluation and training#
Beyond validation, the researchers introduce FuzzEval, a new evaluation paradigm that harnesses generated unit tests in both assessment and learning phases. Using the same test cases for model evaluation and training enables more precise measurement of code correctness and tighter feedback loops during model development.
The method demonstrates practical effectiveness in controlling code hallucinations while maintaining reasonable selection efficiency, suggesting a pathway toward more reliable AI-assisted code generation for safety-critical applications.

