Abstract
Formal verification of Python remains a significant challenge due to the language's dynamic nature and complex runtime. We present PyVeritas, a framework that utilizes Large Language Models (LLMs) to bridge the gap between Python and mature C-based verification ecosystems. By transpiling Python into high-level, human-readable C code, PyVeritas enables the application of bounded model checking (BMC) and automated fault localization. Our evaluation on competitive programming and student assignment benchmarks demonstrates that PyVeritas achieves verification success rates of up to 92%, significantly outperforming native Python verification tools.
Introduction
Formal verification—the mathematical proof of code correctness—is a cornerstone of high-assurance software engineering. While languages like C benefit from robust tools such as CBMC, Python developers lack equivalent exhaustive symbolic reasoning capabilities. Existing native Python verifiers often struggle with fundamental language features like nested loops or complex data structures.
The primary obstacle is Python's high-level abstraction. Traditional transpilers like Cython generate low-level C boilerplate that is too complex for symbolic analysis. PyVeritas addresses this by employing LLMs to perform "high-level transpilation," producing concise C code that preserves program logic while remaining mathematically digestible for model checkers.
The PyVeritas Framework
The PyVeritas pipeline integrates LLMs with classical formal methods through a multi-stage process.
- LLM-Based Transpilation: The system takes a Python program, its natural language description, and formal specifications (assertions). An LLM (e.g., Qwen2.5-Coder) translates these into equivalent C code.
- Validation: The generated C code is executed against original Python test cases to ensure semantic fidelity.
- Bounded Model Checking (BMC): PyVeritas runs CBMC on the C code to exhaustively explore execution paths and detect assertion failures.
- Fault Localization: If a bug is detected, the tool employs CFAULTS (a MaxSAT-based tool) to identify the responsible C statements.
- Back-Mapping: An LLM maps the identified C-level faults back to the original Python source, providing actionable feedback to the developer.
Experimental Evaluation
We evaluated PyVeritas using two primary datasets:
- LiveCodeBench: 479 correct programs from competitive programming platforms like LeetCode.
- Refactory: A collection of 1,783 buggy and 2,447 correct Python programs from student assignments.
To assess bug localization, we introduced semantics-altering mutations, such as operator swaps (e.g., < to >) and constant assignment duplications.
Results and Discussion
PyVeritas demonstrated high efficacy across benchmarks, particularly when paired with larger LLMs.
As shown in the results, Qwen2.5-Coder (32B) achieved a verification success rate of 92% on the Refactory benchmark and 83.7% on LiveCodeBench. In contrast, state-of-the-art native tools like ESBMC-Python failed to verify most programs due to unsupported language features.
A key insight from our study is the trade-off between models. While Qwen2.5-Coder was superior in producing valid C code, GraniteCode exhibited higher "structural fidelity"—maintaining a C structure closer to the original Python—which proved more effective for precise bug localization. We also observed a "bug-fixing paradox" where advanced LLMs occasionally corrected bugs during the transpilation process, a behavior beneficial for coding assistants but challenging for verification tools intended to analyze existing buggy code.
Conclusion and Future Work
PyVeritas successfully demonstrates that LLM-based transpilation can unlock the power of mature C verification tools for Python. While the current framework excels at small-to-medium functions, future work will focus on scaling to multi-file projects and handling complex Python-specific features like classes and third-party libraries (e.g., NumPy). Additionally, improving "bug-preservation" during translation remains a priority to ensure the verifier analyzes the intended logic.
Summary updated