The Larger They Are, the Harder They Fail: Language Models do not Recognize Identifier Swaps in Python

Antonio Valerio Miceli Barone, Fazl Barez, Ioannis Konstas, Shay B. Cohen · DOI

Introduction

While Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation, their underlying mechanisms often rely on statistical shortcuts rather than a fundamental grasp of programming logic. This research highlights a compelling instance of Inverse Scaling: as models like GPT-3 and OPT increase in size and perceived capability, they paradoxically become less effective at generating code when standard built-in functions are redefined. These models tend to adhere to ingrained patterns even when explicitly instructed that the rules of the environment have changed.

The Problem: Semantic Understanding vs. Pattern Matching

The study investigates whether LLMs truly comprehend the abstract semantics of programming languages or if they function primarily as "stochastic parrots" mimicking training data. A fundamental principle in computer science is α-equivalence—the notion that renaming identifiers (such as variables or functions) should not alter a program's logic, provided the renaming is consistent. While human developers can readily adapt to a library that redefines a function name, LLMs appear heavily biased by the vast amount of "standard" code in their training sets. This bias prevents them from reasoning through counterfactual scenarios, such as when len is redefined to print text and print is redefined to calculate length.

Methodology: The Builtin Identifier Swap Task

To test this hypothesis, the researchers designed a "Builtin identifier swap" task. The experimental setup involves providing a Python code snippet that explicitly swaps two built-in functions (e.g., len, print = print, len). The model is then given a function declaration and a docstring (e.g., "Print the length of x") and must generate the function body. Evaluation is based on the likelihood of a "Good" completion (respecting the swap) versus a "Bad" completion (using functions in their traditional sense).

Figure 2: An illustration of the identifier swap task where the LLM incorrectly prefers the standard usage over the redefined logic.

The researchers evaluated a diverse set of model families, including GPT-3, InstructGPT, Meta’s OPT, Salesforce’s CodeGen, and Google’s FLAN-T5, alongside state-of-the-art chat models like GPT-4 and Claude.

Dataset Construction

The evaluation dataset was constructed through a multi-step automated pipeline involving scraping 559 Python repositories from GitHub and extracting 1,000 top-level functions that contained docstrings and at least two calls to Python built-in functions. Automated syntactic substitution was then used to create the "correct" (swapped) and "incorrect" (standard) versions of these function bodies.

Figure 5: The data generation pipeline used to create the evaluation dataset from GitHub repositories.

Key Findings

Inverse Scaling and Shortcut Learning

The most significant finding is the presence of inverse scaling in several model families, particularly OPT and base GPT-3. As these models grow larger, they become more "confident" in incorrect completions. This suggests that the models' internal priors for built-in functions are so dominant that they override explicit prompt instructions. This phenomenon indicates that LLMs rely on lexical correlations (e.g., the string len always associating with length) rather than constructing a mental model of execution flow.

Performance Across Model Types

  • Code-Specific Pretraining: Models like CodeGen, specifically trained on code, exhibited "flat" scaling. While they did not degrade with size, they also failed to show significant improvement, suggesting that code-specific training does not necessarily resolve semantic reasoning failures.
  • Chat Model Limitations: Even advanced models like GPT-4 struggled. In interactive settings, GPT-4 might acknowledge the swap linguistically but fail to apply it computationally, often generating incorrect code immediately after explaining the swap correctly.

Quantitative Results

  • Model Size Correlation: GPT-3 and OPT families showed a strong Pearson correlation (0.94 to 0.97) between model size and increased task loss.
  • Accuracy Metrics: In likelihood-based tests, most models preferred the incorrect output 100% of the time, leading to zero classification accuracy.
  • Top Performers: Anthropic’s Claude models achieved the highest accuracy, though still limited to 10-18%. OpenAI’s GPT-4 and GPT-3.5-turbo remained below 4% accuracy.
  • Non-Builtin Control: When obscure, custom function names were swapped instead of common built-ins, the inverse scaling effect diminished, confirming that the failure is driven by the high frequency of built-ins in training data.

Conclusion and Future Work

The study highlights a critical limitation in the reasoning capabilities of current transformer-based LLMs. Their reliance on statistical patterns over logical consistency poses challenges for tasks requiring strict adherence to non-standard rules. Future research may explore whether this "semantic blindness" persists across other programming languages or more complex quasi-invariances.

Summary updated

BibTeX
@inproceedings{barone2023larger,
  author = {Antonio Valerio Miceli Barone and Fazl Barez and Ioannis Konstas and Shay B. Cohen},
  title = {{The Larger They Are, the Harder They Fail: Language Models do not Recognize Identifier Swaps in Python}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  doi = {10.18653/v1/2023.findings-acl.19},
}
Figure