# AI agents formally verify CPython in Lean, uncovering 418 bugs including 55 crash vulnerabilities

> **Key takeaway:** A project using AI agents to rewrite CPython's internals in the Lean proof assistant has produced 613,318 lines of verified code and exposed 418 bugs in Python 3.15.2, 55 of which can crash the interpreter or corrupt memory through ordinary Python code.

**Published:** 2026-11-10T20:13:02+00:00 
**Source:** Adafruit Electronics 
**Category:** open-hardware 
**Canonical URL:** https://fosswire.org/news/ai-agents-formally-verify-cpython-in-lean-uncovering-418-bug.html 

## Summary
A formal verification project led by broyojo.com has employed AI agents to translate CPython's C source code into Lean, a dependently-typed language capable of machine-checked mathematical proofs. The resulting codebase spans 613,318 lines across 1,539 files, containing 18,898 theorems and lemmas that model CPython's behavior including integer overflow semantics. The verification workflow follows four stages: automated translation, differential testing against real Python execution, formal proof generation, and human review designed to catch vacuous proofs.

The project has achieved 15.7% coverage of the 26,004 functions deemed worth verifying, with 4,071 functions completed. The methodology has already proven its value by identifying 418 reproducible bugs in Python 3.15.2, including 55 security-relevant issues that permit interpreter crashes or memory corruption through unprivileged Python code. The work is publicly available on GitHub and represents one of the largest applications of AI-assisted formal methods to a production language runtime.

## Verification methodology and technical findings
- **Lean formalization scope:** 613,318 lines of Lean code across 1,539 files, with 18,898 theorems and lemmas modeling CPython 3.15.2 internals, including faithful reproduction of C integer wraparound behavior.
- **Four-stage verification pipeline:** Functions progress through automated copying from C, differential testing against real Python execution, formal proof generation, and human review specifically designed to reject vacuously true proofs.
- **Vacuous proof detection:** Reviewers apply a counterfactual test asking whether a broken implementation could satisfy the same proof, addressing an early failure mode where agents generated correct but empty proofs for crashing functions.
- **Bug discovery rate:** 418 confirmed bug reports with minimal reproduction scripts, including 55 interpreter crashes or memory corruption vulnerabilities exploitable through ordinary Python code without native extensions.
- **Coverage progress:** 4,071 of 26,004 target functions formally verified, representing 15.7% completion of the intended verification scope.

## Why it matters
This project demonstrates that AI-assisted formal verification can scale to production language runtimes and find exploitable vulnerabilities that escape conventional testing, potentially establishing a new baseline for CPython security assurance. For Linux distributions and embedded systems relying on Python for system tooling, the identified crash and memory corruption bugs warrant immediate backport attention, while the open methodology offers a template for verifying other critical open-source infrastructure components.