NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CodeBERTScore: Evaluating Code Generation with Pretrained Models of Code

Shuyan Zhou, Uri Alon, Sumit Agarwal, Graham Neubig

Abstract

CodeBERTScore is a novel evaluation metric for AI-generated code that addresses the limitations of traditional text-matching metrics. By leveraging pretrained models of code and incorporating natural language instructions as context, CodeBERTScore captures semantic similarity rather than just surface-level overlap. Our evaluation across multiple programming languages and datasets demonstrates that CodeBERTScore achieves significantly higher correlation with human preference and functional correctness compared to metrics like BLEU or CodeBLEU.

Introduction

The rapid advancement of Large Language Models (LLMs) for code generation, such as GitHub Copilot and OpenAI's Codex, has created an urgent need for robust automated evaluation metrics. Code is fundamentally different from natural language; it is highly structured, and minor textual changes can lead to catastrophic functional failures. Conversely, two code snippets can be textually distinct yet functionally identical—for instance, a for loop versus a while loop, or the use of different but equivalent library functions.

Existing evaluation methods typically fall into two categories, both of which have significant drawbacks. Token-matching metrics like BLEU, ROUGE, and chrF were originally designed for machine translation. They often penalize correct code simply because it uses different variable names or formatting than a reference solution. On the other hand, execution-based evaluation, which involves running code against unit tests, is the "gold standard" for accuracy but is computationally expensive, requires manually written test cases, and introduces security risks when executing unvetted model outputs. CodeBERTScore is proposed as a middle ground: a metric that understands code semantics without the overhead of execution.

Methodology

CodeBERTScore builds upon the BERTScore framework, adapting it specifically for the nuances of programming languages. The pipeline involves encoding both the reference and generated code using a pretrained model and calculating similarity between their token embeddings.

Overview of the CodeBERTScore evaluation pipeline, showing the encoding of natural language instructions and code snippets.

The authors introduced several critical modifications:

  • Contextual Encoding: Unlike standard BERTScore, which processes code in isolation, CodeBERTScore encodes the natural language prompt alongside the code. This "anchors" the evaluation, ensuring the metric assesses whether the code fulfills the user's specific intent.
  • Language-Specific Pretraining: The researchers performed additional self-supervised pretraining on the base CodeBERT model using massive corpora of Python, Java, C++, C, and JavaScript. This allows the model to develop a specialized understanding of the syntax and standard libraries unique to each language.
  • Smart Filtering: To focus on logic, the metric ignores punctuation and natural language tokens within the code blocks during similarity calculations.
  • The $F_3$ Score: While $F_1$ is common, the authors found that the $F_3$ score—which weights recall three times higher than precision—correlates better with functional correctness. In software development, it is often more vital that a model captures all necessary logic (recall), even if it includes some extraneous "noise" (precision).

Experimental Setup

The metric was validated using a diverse set of benchmarks:

  • CodeParrot: Used for the language-specific pretraining phase.
  • CoNaLa: A dataset of Python snippets used to measure correlation with human preference.
  • HumanEval and MultiPL-E: These provide programming problems with unit tests across multiple languages (Python, Java, C++, JS) to measure correlation with functional correctness.
  • ShareCode: A dataset from coding competitions used to test "distinguishability"—the metric's ability to differentiate between solutions to distinct problems.

Analysis and Insights

A significant contribution of this work is the investigation into the "Goldilocks" layer of transformer models. The authors analyzed which layer's embeddings provide the most effective representation for code evaluation.

Layer-wise correlation performance across different programming languages, showing peak performance in middle-to-late layers.

As shown in the analysis, performance typically peaks in the middle-to-late layers (e.g., layers 7 through 10). Early layers tend to capture surface-level syntax, while the final layers may become too specialized for the model's original training objective. Furthermore, the inclusion of natural language instructions was found to consistently improve performance, acting as a functional guide for the embedding process.

Results

CodeBERTScore consistently outperformed traditional baselines across all tested benchmarks:

  • Human Preference: On the CoNaLa dataset, CodeBERTScore achieved a Kendall-Tau correlation of 0.517, significantly higher than the next best metric, chrF (0.470).
  • Functional Correctness: The improvements were even more pronounced in languages like C++, where CodeBERTScore showed a Spearman correlation of 0.393, nearly double that of CodeBLEU (0.201).
  • Community Impact: The researchers released five language-specific models on the Hugging Face Hub, which have already seen over 1,000,000 downloads, indicating rapid adoption by the research community.

Conclusion and Limitations

CodeBERTScore represents a significant step forward in automated code evaluation. However, it is not without limitations. Because it requires a GPU forward pass, it is more computationally intensive than simple string-matching metrics. Additionally, its performance is inherently tied to the quality of the underlying pretrained model. While it serves as a highly effective proxy for correctness, it cannot entirely replace execution-based testing for mission-critical systems where 100% functional verification is mandatory. Future work will likely involve integrating even more powerful backbones like StarCoder or CodeLlama.

Summary updated

BibTeX
@inproceedings{zhou2023codebertscore,
  author = {Shuyan Zhou and Uri Alon and Sumit Agarwal and Graham Neubig},
  title = {{CodeBERTScore: Evaluating Code Generation with Pretrained Models of Code}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.emnlp-main.859},
}
Figure