CodeBLEU

On this page 7

CodeBLEU is an automatic evaluation metric for generated source code that compares a candidate program with one or more reference programs by combining four signals: n-gram overlap, keyword-weighted n-gram overlap, abstract syntax tree matching and data-flow matching. In AI for coding, it was designed to fix the main weakness of plain BLEU, which treats code as ordinary text and ignores grammar and semantics.

The metric was introduced in 2020 by researchers at Microsoft Research Asia and adopted in the CodeXGLUE benchmark suite for tasks such as code translation, code refinement and text-to-code generation. It remains a common reference-based score in artificial intelligence research, although execution-based metrics like pass@k have since become the preferred measure of functional correctness.

How CodeBLEU computes its score

CodeBLEU is a weighted sum of four component scores, each ranging from 0 to 1. The standard formula is CodeBLEU = α · BLEU + β · BLEU_weight + γ · Match_ast + δ · Match_df, and the original paper uses equal weights of 0.25 as the default setting.

Each component captures a different aspect of similarity between candidate and reference. The following table describes the four components and what each one detects.

ComponentWhat it comparesWhat it captures
BLEUToken n-grams up to length 4Surface lexical similarity
Weighted n-gram matchUnigrams with keywords weighted higherCorrect use of language keywords
Syntactic AST matchSubtrees of the abstract syntax treeGrammatical structure of the program
Semantic data-flow matchVariable dependency graphsHow values flow between variables
The four components of CodeBLEU

The weighted n-gram component assigns keywords such as if, return or for a weight several times higher than identifiers, typically five to one, so that a missing control-flow keyword costs more than a renamed variable. The syntax component parses both programs with a language grammar and counts the fraction of reference subtrees found in the candidate, ignoring leaf identifiers.

Why data flow matters

The data-flow component is the most distinctive part of CodeBLEU. It builds a graph in which nodes are variable occurrences and edges show where each value comes from, then normalizes variable names and counts how many reference edges appear in the candidate.

Because variable names are normalized, two functions that compute the same result with different identifiers score well, while two functions that look similar but assign values in a different order score poorly. This makes CodeBLEU more sensitive to semantic errors than any purely token-based metric.

The original authors reported that, on text-to-code, code translation and code refinement tasks, CodeBLEU achieved a higher correlation with programmer judgments than BLEU or exact match. The improvement was meaningful but not absolute, and later studies found that the correlation varies by task and language.

CodeBLEU compared with other code metrics

CodeBLEU belongs to the family of reference-based similarity metrics, which measure resemblance to a known solution rather than whether the code actually works. The comparison below places it among the main alternatives used to evaluate code models.

MetricBasisNeeds testsMain weakness
Exact matchIdentical token sequenceNoRejects valid alternatives
BLEUToken n-gram precisionNoIgnores syntax and semantics
CodeBLEUN-grams, AST and data flowNoStill rewards resemblance, not behavior
CodeBERTScoreEmbedding similarityNoOpaque and model-dependent
pass@kExecution against unit testsYesRequires reliable test suites
Computational accuracyOutput equality after executionYesRequires runnable inputs
Common metrics for evaluating generated code

Benchmarks such as HumanEval, MBPP and LiveCodeBench rely on pass@k, because a program that passes its tests is useful regardless of how it is written. CodeBLEU remains valuable where tests are unavailable, for example in code translation corpora or in refinement datasets drawn from real commits.

Using CodeBLEU in practice

Applying CodeBLEU correctly requires a parser for the target language, a keyword list and a data-flow extractor. Reference implementations support languages such as Python, Java, JavaScript, C#, Go, PHP, Ruby and C, usually through tree-sitter grammars.

The choice of weights should reflect the evaluation goal. The decision table below gives common configurations.

Evaluation goalSuggested weightsReason
General comparison with published results0.25 for each componentMatches the standard setting
Code translation between languagesHigher AST and data-flow weightsSurface tokens differ across languages
Small edits and bug fixesHigher BLEU weightsMost tokens should stay unchanged
Semantic faithfulness studyData-flow weight of 0.4 or moreEmphasizes value dependencies
Choosing CodeBLEU weights

Scores should always be reported with the weights, language and implementation version, because different tokenizers and parsers can shift results by several points. Comparing CodeBLEU numbers across papers without this information is a frequent source of misleading claims.

Limitations and common misconceptions

The central limitation is that CodeBLEU measures similarity to a reference, not correctness. A functionally perfect solution written in a different style may receive a low score, while a nearly identical program with an off-by-one error may score highly.

  • Single reference bias: most datasets offer one reference, although many valid programs exist.
  • Parse failures: syntactically broken candidates lose the AST and data-flow components entirely.
  • No execution: runtime errors, wrong outputs and performance problems remain invisible.
  • Score inflation by copying: models that reproduce boilerplate gain points without solving the task.
  • Not a quality guarantee: a high score is not evidence of safe or secure code.

Experienced evaluators therefore treat CodeBLEU as a complementary signal, combined with execution tests, static analysis and human review.

The short version

CodeBLEU is a way to grade computer-written code by comparing it with a model answer. It does not run the code. It looks at the two programs side by side.

It checks four things. Do they use the same words? Do they use the important keywords? Is the grammar shaped the same way? Do values move between variables in the same pattern? Each check gives a score, and the four scores are averaged. A high result means the code resembles the answer, which is a good hint but not proof that it works.

Where it makes a difference

A research team is training a model to translate legacy Java services into C#. They have 8,000 paired functions from an earlier manual migration, but almost none come with unit tests, so execution-based metrics are not possible for most of the data.

The team evaluates two model versions with CodeBLEU, using a weight of 0.3 for both the AST and data-flow components. Plain BLEU rates the versions almost equally at 0.61 and 0.62. CodeBLEU separates them clearly, 0.58 against 0.67, because the newer model preserves variable dependencies in loops and exception handlers far more often.

To confirm the result, engineers write tests for a random sample of 200 functions. The newer model passes 71 percent of them and the older one 54 percent, which supports the CodeBLEU ranking. The team adopts the newer model and keeps CodeBLEU as a fast nightly check, while every migrated service still goes through test-based validation before release.