Measuring LLM Code Generation Stability via Structural Entropy

Yewei Song, Tiezhu Sun, Xunzhu Tang, Prateek Rajput, Tegawende F. Bissyande, Jacques KleinarXiv 2025

TL;DR

This paper introduces a novel, reference-free framework to measure the stability and consistency of LLM code generation using structural entropy. By analyzing the statistical distribution of subtrees within a program's Abstract Syntax Tree (AST), the proposed metrics—Jensen-Shannon Divergence (JSD) and Structural Cross-Entropy (SCE)—quantify how much a model's structural choices fluctuate across multiple independent outputs for the same prompt.

What Problem Does This Paper Address?

Large Language Models (LLMs) are notoriously non-deterministic. Even with identical prompts and hyperparameters (and sometimes even at a sampling temperature of zero), an LLM can generate completely different code structures across multiple runs. This output instability undermines developer trust and complicates reproducibility in software engineering.

Existing evaluation metrics fall short of capturing this phenomenon. As shown in the paper's comparison table, traditional metrics like BLEU or CodeBLEU measure similarity against a single reference solution, while pass@k focuses purely on functional correctness via test execution. None of these metrics assess how consistent a model is with itself across multiple generations without relying on external test suites or ground-truth code.

Comparison of current stability metrics with the proposed structural entropy approach.

Methods

The authors propose a lightweight, three-phase pipeline that operates in $O(n \cdot d)$ time:

  1. Depth-Bounded Subtree Extraction: Code outputs are parsed into Abstract Syntax Trees (ASTs). The algorithm extracts fragments (subtrees) up to a fixed depth $d$. To balance generality and specificity, they introduce two encodings:
  2. Structure-only: Ignores identifier names and literals, focusing purely on control-flow shape.
  3. Structure+value: Retains exact source text tokens for leaf nodes, capturing fine-grained variations.
  4. Empirical Distribution Construction: The frequencies of these unique subtree patterns are collected into a multiset, forming a probability distribution that characterizes the "structural vocabulary" of the generated code.
  5. Entropy-Based Metrics: Stability is calculated using two distinct measures:
  6. Structural Cross-Entropy (SCE): A directed metric that measures how well one distribution explains another, highlighting missing high-probability patterns.
  7. Jensen-Shannon Divergence (JSD): A symmetric, bounded metric mapping to a $[0,1]$ similarity scale.

Datasets

The method was evaluated using two widely recognized benchmarks representing different programming paradigms:

  • BigCodeBench (Python): Used for evaluating general-purpose code generation tasks with complex instructions.
  • Spider (SQL): A cross-domain dataset used for evaluating text-to-SQL query generation consistency.

The authors benchmarked three representative models on these datasets: LLaMA-3.1 (8B-Instruct), Qwen-2.5 (7B-Instruct), and Qwen-2.5-Coder (7B).

Key Insights and Ideas

  • Reference-Free & Execution-Independent: Unlike pass@k, which requires a functioning runtime environment and unit tests, structural entropy can be calculated purely by parsing the text. This makes it highly lightweight and language-agnostic.
  • Isolating Token Noise from Logic: By comparing the "structural-only" variants against full-subtree encodings, the metrics can differentiate between superficial variations (like changing variable names or literals) and deep syntactic shifts (like changing a for loop to a while loop).
  • Complementary Sensitivity: JSD tends to remain consistently high, indicating that the macro-structure of the code is often stable. SCE, however, drops significantly when fine-grained token variations occur, making it a much more sensitive detector of subtle model drift.

Key Results

The quantitative evaluation revealed that even when models achieve high correctness, they suffer from structural instability. For instance, Qwen-2.5-Coder achieved a pass@1 of 0.373 but a pass@5 of 0.517 on Python tasks, indicating that the model frequently alternates between successful and failing code structures.

Correlation analysis (Figure 1) showed that the proposed JSD metric correlates strongly ($0.85 - 0.94$) with AST-based Tree Edit Distance (TSED), validating that the entropy-based approach accurately captures physical structural differences. Conversely, lexical metrics like BLEU showed poor correlation with structural metrics in SQL tasks ($\sim 0.51$), underscoring the limitations of token-matching for structural code analysis.

Heatmap showing the Pearson correlation coefficients between various stability evaluation metrics across Text-to-SQL and Python tasks.

Limitations and Future Work

While promising, the authors note several limitations:

  • Language and Benchmark Scope: The evaluation was restricted to Python and SQL; wider generalizability across more languages remains to be proven.
  • Hyperparameter Sensitivity: The choice of the subtree depth parameter $d$ directly influences the stability scores.
  • Lack of Semantic Awareness: The metric measures syntactic structure but does not explicitly account for semantic equivalence (e.g., two entirely different AST structures that perform the exact same function).

Future work will focus on incorporating data- and control-flow dependencies to capture behavioral stability, exploring adaptive subtree-weighting schemes, and scaling the evaluation to larger, multi-module codebases.

Summary updated

BibTeX
@misc{song2025measuring,
  author = {Yewei Song and Tiezhu Sun and Xunzhu Tang and Prateek Rajput and Tegawende F. Bissyande and Jacques Klein},
  title = {{Measuring LLM Code Generation Stability via Structural Entropy}},
  year = {2025},
  eprint = {2508.14288},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.14288},
}
Figure