Towards More Trustworthy Deep Code Models by Enabling Out-of-Distribution Detection

Yanfu Yan, Viet Duong, Huajie Shao, Denys Poshyvanyk

Abstract

Modern deep learning models for software engineering often operate under a "closed-world assumption," assuming that test data will mirror training distributions. However, real-world code frequently introduces domain shifts, syntactic noise, and semantic bugs that cause these models to fail silently or provide overconfident, incorrect predictions. This paper introduces COOD and COOD+, two frameworks designed to detect Out-of-Distribution (OOD) samples in code-related tasks. By leveraging multi-modal contrastive learning and weak supervision, these frameworks significantly enhance the reliability of code models, achieving high AUROC scores across various OOD scenarios and improving downstream task performance.

Introduction

As deep learning models become integral to software development—powering everything from code search to automated documentation—their reliability is paramount. A critical vulnerability in these models is their tendency to produce "overconfident" outputs when encountering data that differs from their training set. Whether it is a snippet from an unfamiliar platform like StackOverflow or code containing subtle semantic mutations, standard models often lack a "safety valve" to signal when they are out of their depth.

This research addresses this gap by enabling Out-of-Distribution (OOD) detection for code models. By identifying when an input is fundamentally different from the training distribution, developers can prevent incorrect predictions and flag suspicious code for human review, ultimately building more trustworthy AI tools for software engineering.

The OOD Problem in Code Intelligence

Most code models are trained on high-quality, curated datasets like GitHub repositories. In practice, however, they encounter several types of OOD instances:

  1. Domain Shifts: Differences in coding styles and contexts, such as user-centric snippets from StackOverflow versus developer-centric code in professional repositories.
  2. Misalignment: Scenarios where a natural language comment does not accurately describe the accompanying code block.
  3. Syntactic Noise: Inputs containing shuffled or ungrammatical comments that disrupt the model's linguistic understanding.
  4. Semantic Bugs: Code containing logic errors, such as variable misuse, which the model may not have been trained to recognize as "incorrect."

When faced with these inputs, standard models typically fail to express uncertainty, leading to potentially dangerous silent failures in production environments.

Methodology: COOD and COOD+

The researchers developed two versions of their detection framework, both built upon the GraphCodeBERT architecture, to identify these outliers.

COOD (Unsupervised)

The base COOD framework is trained exclusively on "normal" (in-distribution) data. It utilizes multi-modal contrastive learning (InfoNCE loss) to align the representations of natural language (comments) and programming language (code) in a shared embedding space. During inference, the model calculates the cosine similarity between the code and its comment; a low similarity score indicates that the pair is likely OOD.

COOD+ (Weakly-supervised)

COOD+ improves upon the unsupervised model by incorporating a tiny amount of labeled OOD data—specifically, just 1% of the training set. This version introduces two primary innovations:

  • Margin-based Contrastive Loss: This loss function explicitly pushes OOD pairs away from the in-distribution clusters by a defined margin, creating a clearer boundary between "known" and "unknown" data.
  • Binary OOD Rejection (BC) Module: A dedicated three-layer neural network acts as a binary classifier. It works in tandem with the contrastive module to produce a final OOD score, combining similarity metrics with direct classification probability.

Experimental Setup

The frameworks were evaluated using CodeSearchNet (CSN) for Java and Python as the In-Distribution (ID) data. To simulate real-world OOD challenges, the researchers used:

  • TLCS: A dataset from StackOverflow to test domain shifts.
  • Synthetic Scenarios: Custom datasets featuring misaligned pairs, shuffled comments, and "Buggy-code" generated via semantic mutation algorithms.

Key Results and Insights

High Detection Accuracy

COOD+ demonstrated exceptional performance, achieving an AUROC of over 96% across most OOD scenarios for both Java and Python. It significantly outperformed traditional baselines like CuBERT and standard energy-based models. Notably, in the difficult "Buggy-code" scenario, COOD+ maintained an AUROC between 91% and 97%, whereas unsupervised methods often struggled, performing near the level of random chance.

The Value of "A Little Bit of Bad"

A major takeaway from the study is the disproportionate impact of weak supervision. By exposing the model to a mere 1% of OOD data, the researchers were able to bridge the gap in detecting subtle semantic inconsistencies that unsupervised models missed. This suggests that even minimal exposure to negative examples can drastically harden a model against outliers.

Improving Downstream Reliability

OOD detection is not merely a defensive layer; it actively improves the utility of the primary task. By filtering out "garbage" OOD inputs in a code search task, the researchers recovered nearly 5% in Mean Reciprocal Rank (mRR). This demonstrates that a model's ability to abstain from a prediction is just as important as its ability to predict correctly.

Conclusion and Future Work

By enabling OOD detection, this research provides a path toward more robust and trustworthy deep code models. While the current study focused on Java and Python using Transformer-based encoders, the methodology is largely language-agnostic.

Future research directions include testing these frameworks on a broader array of programming languages (such as C++ or Go) and scaling these techniques to massive generative Large Language Models (LLMs). Ensuring that models like GPT-4 or Llama can recognize their own limitations is the next frontier in making AI-assisted programming a safe and reliable reality.

Summary updated

BibTeX
@inproceedings{yan2025towards,
  author = {Yanfu Yan and Viet Duong and Huajie Shao and Denys Poshyvanyk},
  title = {{Towards More Trustworthy Deep Code Models by Enabling Out-of-Distribution Detection}},
  booktitle = {ICSE},
  year = {2025},
  doi = {10.1109/icse55347.2025.00177},
}
Figure