CoDet-M4: Detecting Machine-Generated Code in Multi-Lingual, Multi-Generator and Multi-Domain Settings

Daniil Orel, Dilshod Azizov, Preslav Nakov

Introduction

As Large Language Models (LLMs) like GPT-4 and Llama become ubiquitous in software development, the distinction between human and machine authorship is increasingly blurred. This shift introduces a "trust gap" across education, hiring, and model training. To address this, researchers developed CoDet-M4, a large-scale benchmark and framework designed to detect machine-generated code across diverse programming languages, generators, and domains.

Existing detection tools often suffer from narrow focus—typically limited to Python or a single model like ChatGPT—and fail to generalize to "out-of-domain" scenarios. CoDet-M4 bridges this gap by providing a robust, multi-lingual evaluation framework trained on a massive dataset of approximately 500,000 samples.

Methodology and Dataset

The researchers introduced the CoDet-M4 dataset, comprising 499,467 code snippets across Python, Java, and C++. Human code was curated from GitHub (pre-2019 to avoid AI contamination), LeetCode, and CodeForces. AI-generated samples were produced using five distinct models: CodeLlama (7B), Llama 3.1 (8B), CodeQwen 1.5 (7B), Nxcode-orpo (7B), and GPT-4o.

Detection was approached through three primary methods:

  1. Traditional Machine Learning: Models like SVM and CatBoost were trained on over 500 handcrafted features, including statistical markers (line length, whitespace) and structural markers.
  2. Pre-trained Language Models (PLMs): Specialized code encoders—CodeBERT, UniXcoder, and CodeT5—were fine-tuned to capture the semantics and structure of the code.
  3. LLM-based Detection: Zero-shot and few-shot prompting were tested using GPT-4o.

The Structural Fingerprint of AI Code

A key insight from the study is that AI-generated code leaves a "structural fingerprint." Machine-authored code tends to be slightly less complex than human code. Features such as Abstract Syntax Tree (AST) depth and the density of assignment operations serve as strong indicators of authorship.

Figure 1: Comparison of AST Depth between Human and AI-generated code across Python, Java, and C++. AI code consistently shows lower structural complexity.

As shown in the structural analysis, human-written code generally exhibits a wider range and higher median AST depth compared to AI-generated counterparts, regardless of the programming language.

Detection Performance

The standout performer in the study was UniXcoder. By leveraging cross-modal representations that integrate both raw text and AST structure, UniXcoder effectively captured the subtle stylistic markers left by different LLMs.

Figure 4: Performance metrics (Precision, Recall, F1-score, and Accuracy) for various detection models on the CoDet-M4 test set.

On the core test set, UniXcoder achieved a remarkable 98.65% accuracy, significantly outperforming traditional ML baselines and general-purpose LLMs. Interestingly, while specialized models excelled, GPT-4o performed poorly as a detector, achieving only ~42% accuracy in zero-shot settings. This suggests that smaller, specialized encoders are far more effective for this specific task than large-scale generative models.

Key Insights and Challenges

  • Authorship Identification: Beyond binary detection, the models could identify the specific generator (e.g., CodeLlama vs. GPT-4o) with up to 79.35% accuracy.
  • Cross-Language Generalization: When trained on Python, Java, and C++ and tested on unseen languages like Go or PHP, UniXcoder maintained a respectable accuracy of approximately 88.96%.
  • The Hybrid Difficulty: The most significant challenge remains "hybrid" code—collaborative efforts where a human writes a base and an AI completes it. As the proportion of human-written code increases, detection accuracy drops sharply.

Limitations and Future Work

While CoDet-M4 represents a major advancement, the authors note that models still struggle with very short code snippets where stylistic signals are minimal. Furthermore, as LLMs evolve to be more "human-like," the identified features may become less effective. Future research will focus on expanding the dataset to more languages and exploring contrastive learning to improve robustness against adversarial code designed to bypass detection.

Summary updated

BibTeX
@inproceedings{orel2025codet,
  author = {Daniil Orel and Dilshod Azizov and Preslav Nakov},
  title = {{CoDet-M4: Detecting Machine-Generated Code in Multi-Lingual, Multi-Generator and Multi-Domain Settings}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure