Enhancing LLM Code Generation with Ensembles: A Similarity-Based Selection Approach

Tarek Mahmud, Bin Duan, Corina S. Pasareanu, Guowei Yang

TL;DR

This paper introduces EnsLLM, a novel ensemble approach for Large Language Models (LLMs) in code generation. By combining outputs from multiple LLMs and using a sophisticated voting mechanism based on syntactic, semantic, and behavioral similarity, EnsLLM significantly boosts code generation accuracy, outperforming even the best single LLMs on challenging benchmarks.

What Problem Does This Paper Address?

Large Language Models (LLMs) have become incredibly powerful at generating code, but they are not perfect. They can produce code with subtle syntax errors, logical flaws, or missed edge cases, leading to unreliable outputs. Even state-of-the-art models like GPT-4 only achieve around 82% accuracy on standard benchmarks. This unreliability is a major hurdle for their widespread adoption in real-world software development. The paper addresses the challenge of improving the reliability and correctness of LLM-generated code by moving beyond single-model predictions.

Methods

The core of EnsLLM is a structured ensemble approach. Instead of relying on a single LLM's output, it:

  1. Generates Multiple Candidates: It prompts several different LLMs to generate code for the same problem.
  2. Filters Syntactically Invalid Code: A preliminary step removes code that doesn't even parse correctly.
  3. Similarity-Based Voting: This is the novel part. It uses two key metrics to score and rank the remaining candidate programs:
  4. CodeBLEU: This metric assesses syntactic and semantic similarity. It goes beyond simple word matching by analyzing Abstract Syntax Trees (ASTs) for structural similarity and data flow for logical consistency.
  5. Behavioral Similarity (using CrossHair): This uses differential analysis to check if programs behave identically. CrossHair generates counterexamples (inputs that cause different outputs) to detect functional inconsistencies and logic errors. A "behavioral similarity" score is derived from the number of counterexamples.
  6. Aggregated Similarity Score: The scores from CodeBLEU and behavioral similarity are combined (with equal weighting in their experiments) to create an overall similarity score for each candidate program.
  7. Selection: The program with the highest aggregated similarity score is chosen as the final output. This assumes that correct programs will tend to agree with each other on multiple fronts, while incorrect ones will diverge.

Datasets

The paper evaluates EnsLLM on two established code generation benchmarks:

  • HumanEval: A widely used benchmark with 164 Python programming problems designed to evaluate functional correctness.
  • LiveCodeBench: A more challenging benchmark with 511 programming problems covering a broader range of real-world coding scenarios, including API usage and algorithmic challenges.

Key Insights and Ideas

  • Ensembling is Key for Reliability: The paper strongly suggests that combining multiple LLM outputs is a more effective strategy for achieving reliable code generation than relying on a single, even powerful, model.
  • Beyond Syntax: Behavioral Equivalence Matters: Simply having syntactically correct code isn't enough. The ability to detect and penalize programs that behave differently, even if they look similar, is crucial for filtering out subtle bugs.
  • Complementary Strengths: CodeBLEU and CrossHair, while different, complement each other. CodeBLEU helps identify structurally and semantically similar code, while CrossHair acts as a rigorous functional validator.
  • Open-Source Models Can Be Powerful in Ensembles: Even when restricted to free, open-source LLMs, the ensemble approach significantly boosts performance, demonstrating its viability in resource-constrained environments.

Key Results

  • Significant Accuracy Boost: EnsLLM achieved 90.2% accuracy on HumanEval and 50.2% on LiveCodeBench, outperforming the best standalone LLM (GPT-4o) by a considerable margin (83.5% and 43.4%, respectively).
  • Effective with Free Models: When using only open-source LLMs, EnsLLM reached 80.5% on HumanEval and 41.6% on LiveCodeBench, still significantly outperforming the best individual free model.
  • Equal Weighting is Optimal: An ablation study showed that giving equal weight to CodeBLEU (syntactic/semantic) and behavioral similarity (CrossHair) yielded the best results.
  • Qualitative Analysis: In failed cases, the paper found that EnsLLM often failed when multiple LLMs made identical mistakes, reinforcing the assumption that diverse model behaviors are key to ensemble success.

Limitations and Future Work

  • Dependence on Candidate Quality: EnsLLM cannot generate a correct solution if none of the LLMs produce one. Its effectiveness is tied to the quality of the initial LLM outputs.
  • Identical Mistakes: If multiple LLMs make the same incorrect prediction, the ensemble might select the wrong output.
  • Pairwise Similarity Limitation: If only two candidate programs remain after filtering, the current pairwise similarity mechanism might not differentiate them effectively.
  • CrossHair's Scope: CrossHair's effectiveness can be limited by external dependencies or non-deterministic behavior in the code.
  • Future Work: The authors plan to extend EnsLLM with more code quality metrics (security, performance, maintainability), explore more efficient ensemble execution strategies (e.g., selective model invocation), and investigate system-level solutions for concurrent LLM execution. They also aim to expand the evaluation to a broader set of models and languages.

Summary updated

Abstract

Ensemble learning has been widely used in machine learning to improve model robustness, accuracy, and generalization, but has not yet been applied to code generation tasks with large language models (LLMs). We propose an ensemble approach for LLMs in code generation. Instead of relying on the output of a single model, we generate multiple candidate programs from different LLMs and apply a structured voting mechanism to select the most reliable solution. For voting, we compute syntactic and semantic similarity using CodeBLEU and behavioral equivalence using CrossHair's differential behavior analysis. By aggregating these similarity scores, we select the program that best aligns with the consensus among the candidates. We show through experiments that our ensemble approach consistently outperforms standalone LLMs on the well-known HumanEval and the more challenging LiveCodeBench datasets, achieving an accuracy of 90.2% and 50.2%, respectively, on the two datasets. In comparison, the best-performing LLM (GPT-4o) has an accuracy of 83.5% and 43.4%, respectively. Furthermore, even when restricted to free open-source models, our method achieves an accuracy of 80.5% and 41.6%, respectively, demonstrating the viability of our approach in resource-constrained settings.
BibTeX
@misc{mahmud2025enhancing,
  author = {Tarek Mahmud and Bin Duan and Corina S. Pasareanu and Guowei Yang},
  title = {{Enhancing LLM Code Generation with Ensembles: A Similarity-Based Selection Approach}},
  year = {2025},
  eprint = {2503.15838},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.15838},
}
Figure