arXiv:2610.30432cs.SEOct 2026

Manuscript Offprint

arXiv:2610.30432

TrustedCode: Evaluating Large Language Models for Developing Trusted Code through Automated Program Verification

Muhammad Danial, Muhammad Taimoor Khan

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

Muhammad Danial

School of Computing and Mathematical Sciences

University of Greenwich

London, UK

md7114u@gre.ac.uk

Muhammad Taimoor Khan

Centre for Sustainable Cyber Security

University of Greenwich

London, UK

m.khan@greenwich.ac.uk

Abstract—Recent reports, such as "Who is using AI to code?", indicate that approximately 30% of Python code contributions to GitHub are AI-generated. Given the inherent unreliability of this code, formal verification emerges as a promising approach to establish trust in software correctness. Although leveraging large language models (LLMs) for formal verification has garnered increasing attention, practical adoption remains limited due to the challenges in writing formal specifications and verifying program correctness against them. Existing tools predominantly support verification for languages such as Java and C, with minimal support for Python. We present TrustedCode, an automated framework for verifying AI-generated Python code. TrustedCode uses LLMs to (i) type-check Python code, (ii) translate it into WhyML via the Why3 framework, and (iii) verify it against formal specifications using Why3 back-end automated theorem provers. Our method integrates chain-of-thought prompting through LangGraph to identify and address LLM limitations in specification generation and proving the specified programs. To evaluate TrustedCode, we conducted experiments on ten Python programs of varying complexity. Our analysis reveals that 50% of the observed errors stem from domain knowledge gaps, 30% from syntactic inaccuracies, and the remaining 20% from semantic and type-related issues. These findings underscore the importance of targeted improvements in LLM-guided formal verification workflows for Python. TrustedCode is an open source tool available at: github.com/mtaimoorkhan/TrustedCode. The video demonstration is available at:youtu.be/Nr5I2g8ec-s

I. INTRODUCTION

Formal reasoning about software is a widely used technique to establish trust in the software. Owing to the difficulties in writing formal specification and establishing corresponding proofs, it has not been widely accepted in industry [1]. Since software complexity is increasing, traditional testing is becoming increasingly inadequate, necessitating formal verification to improve software reliability.

Human and complier feedback to repair proofs has demonstrated efficacy in several studies. For instance, [2] exhibited automatic proof repair using Coq to maintain verified systems. In [3], the premise selection task is devolved to automated theorem proving while the proof generation strategy is handled by the LLM's. While [4] introduces Baldur, which generates complete proofs using LLM's fine-tuned on formal verification, and attempts to correct the proof in an iterative manner.

In this context, there is potential for LLMs to be used not only for code generation, but also for error correction and ensuring that the relevant predicates and theories are developed. Therefore, in this paper, we aim to answer a research question as follows:

What are the capability gaps of a state-of-the-art LLM (Anthropic Claude Sonnet 4) in translating python programs into a automatically verifiable WhyML specified code, and what is its effectiveness when operating within a self-correction loop?

To answer this question, we present TrustedCode, a framework within the confines of a closed-loop system to translate Python program into WhyML specification language, noted for its strengths as a formal verification framework and support for back-end provers. This will incorporate an LLM, the Why3 software as well as an iterative repair sequence using LangGraph to produce a provably correct program. To correct LLM errors, we integrate a method to identify and classify capability gaps. This method moves beyond the current binary pass-fail metric to a concrete and structured classification methodology to specifically isolate the LLMs gaps in its understanding of types, theory, syntax, and semantics. Finally, we provide a practical analysis on the performance of the framework, including evaluating the capacity gaps of the LLM and the way they were fixed. This provided a holistic overview of the current state and limitations of LLM models for this application.