Abstract
Large Language Models (LLMs) are widely used by software engineers for programming tasks. However, research shows that LLMs often lack a deep understanding of program semantics. Even minor changes to syntax, such as renaming variables, can significantly degrade performance across various tasks. In this work, we examine the task of type prediction: given a partially typed program, can a model predict a missing type annotations such that the resulting program is more typed? We construct a dataset of adversarial examples where models initially predict the correct types, but begin to fail after semantically irrelevant edits. This is problematic, as models should ideally generalize across different syntactic forms of semantically equivalent code. This lack of robustness suggests that models may have a shallow understanding of code semantics. Despite this, we provide evidence that LLMs do, in fact, learn robust mechanisms for type prediction—though these mechanisms often fail to activate in adversarial scenarios. By using activation steering, a method that manipulates a model's internal activations to guide it toward using latent knowledge, we restore accurate predictions on adversarial inputs. We show that steering successfully activates a type prediction mechanism that is shared by both Python and TypeScript, and is more effective than prompting with in-context examples. Across five different models, our comprehensive evaluation demonstrates that LLMs can learn generalizable representations of code semantics that transfer across programming languages.
Introduction: The Brittleness of CodeLLMs
Large Language Models (LLMs) are surprisingly good at predicting types in gradually typed languages like Python and TypeScript, but they are notoriously brittle. A simple change, such as renaming a variable from count to x_01, can cause a model that previously identified the variable as an int to suddenly guess it is a string or a generic any type.
This raises a fundamental question: Do these models actually understand program semantics—how data flows through code—or are they merely relying on "shallow" textual associations between names and types? If a model is easily fooled by a simple name change, it suggests its understanding is skin-deep. This research investigates whether a robust reasoning mechanism exists within the model and why it fails to trigger in adversarial scenarios.
Methodology: Activation Steering
To investigate the internal state of CodeLLMs, researchers employed a "white-box" approach using Activation Steering. The process involved three main steps:
- Adversarial Dataset Construction: Starting with code snippets the models solved correctly ("easy" tasks), the researchers applied semantics-preserving edits—such as renaming variables or removing other type hints—until the model failed ("hard" tasks).
- Finding the "Type Vector": By comparing internal activations during "easy" vs. "hard" versions of the same code, they calculated a steering vector ($v^\ell$). This vector represents the mathematical difference between the model "getting it right" and "getting it wrong."
- Steering at Inference: During a forward pass on a failing adversarial prompt, this steering vector was added to the model's hidden layers, "nudging" the internal state toward the robust reasoning path used for clean code.
Experimental Setup
The study utilized two primary sources of ground-truth code:
- Python: ManyTypes4Py, containing over 5,000 real-world projects with verified type annotations.
- TypeScript: A filtered subset of The Stack (v1.2), consisting of 1.1 million TypeScript files that pass type-checking.
From these, model-specific adversarial test sets (100 tasks each) were generated where baseline accuracy without steering was 0% by design. Evaluation spanned five models, including Qwen 2.5 Coder, CodeLlama, and StarCoder.
Key Results and Insights
The experiments yielded several critical insights into how LLMs process code:
- Latent Knowledge Restoration: On adversarial test sets where models initially had 0% accuracy, activation steering restored performance to 50%–80%. This proves that models possess the capacity for correct prediction even when distracted by superficial syntax.
- The "Middle Layer" Sweet Spot: Steering was most effective when applied to the later middle layers. This suggests a hierarchy where early layers handle raw syntax, middle layers manage semantic reasoning (like types), and final layers govern token generation.
- Language-Agnostic Representations: In a significant finding, steering vectors calculated from TypeScript examples successfully corrected errors in Python code. This suggests LLMs develop a universal internal representation of "types" that transcends specific programming languages.
- Steering vs. Prompting: Activation steering significantly outperformed In-Context Learning (ICL). Direct activation manipulation was more effective at triggering robust internal mechanisms than providing adversarial examples in the prompt.
Discussion and Limitations
While promising, the research highlights several areas for future work:
- Precision vs. Correctness: Steering helps models pick valid types, but not necessarily the most precise ones (e.g., predicting
dictinstead of a specific user-defined class). - Resource Bias: The study focused on high-resource languages (Python/TypeScript). It remains unclear if these robust mechanisms exist for low-resource languages with less training data.
- Edit Complexity: The automated edits used were relatively simple. Future research should explore if steering holds for complex semantic changes like logic reordering or control flow modifications.
Ultimately, this work suggests that improving LLM performance may not require more data, but rather better methods to access the sophisticated reasoning capabilities already latent within their hidden layers.
Summary updated