Can In-context Learning Really Generalize to Out-of-distribution Tasks?

Qixun Wang, Yifei Wang, Yisen Wang, Xianghua Ying · DOI

Introduction

In-context learning (ICL) is often celebrated as the mechanism that allows Large Language Models (LLMs) to solve novel tasks using only a few examples provided in a prompt. However, a fundamental question remains: are these models truly learning new logic on the fly, or are they simply recycling patterns from their massive pretraining data? This paper investigates whether ICL can achieve true out-of-distribution (OOD) generalization or if it is strictly constrained by the "pretraining hypothesis space."

Methodology

The researchers employed a multi-faceted approach combining synthetic experiments, real-world LLM probing, and theoretical analysis:

  • Synthetic Function Learning: GPT-2 models were trained from scratch on specific mathematical function classes, such as Linear Regression (LR), Quadratic Regression (QR), and 2-layer ReLU networks. These models were then tested on different function classes to observe if they could adapt to OOD logic.
  • Abstract Label & Retrieval Tasks: To distinguish between learning and retrieval, the authors designed tasks where labels were replaced with meaningless symbols or reversed (e.g., predicting "evitisop" instead of "positive").
  • Real-World Probing: Llama-2 and Llama-3 were evaluated on synthetic word classification tasks and modified NLP benchmarks to test their limits on truly OOD data.
  • Theoretical Framework: Using a mixed Gaussian pretraining setup, the authors mathematically modeled how ICL selects between learned "algorithms" based on provided context.

Key Insights: The Pretraining Hypothesis Space

The central finding is that ICL is largely an optimization process within a model's existing library of functions. When a model trained on linear functions is presented with quadratic examples, it does not "learn" quadratic logic; instead, it implements the best linear approximation possible via an internal process resembling gradient descent.

Algorithm Selection and Low-Test-Error Preference

The paper demonstrates that ICL acts as a Bayesian-like selector. When faced with an ambiguous prompt, the model identifies which of its pretrained "algorithms" yields the lowest error on the provided examples and applies that specific logic. This "low-test-error preference" explains how models appear to adapt to different tasks within their known distribution.

Performance of ICL across different function classes (LR, QR, ReLU NN) compared to gradient descent (GD) baselines. The plots show that models converge toward the performance of their native function class optimized via GD.

Retrieval vs. True Learning

The authors argue that the ability of LLMs to use abstract labels (like "foo/bar") is not evidence of learning a new task but is instead a sophisticated retrieval mechanism. During pretraining, models learn that if a novel label is associated with a specific input earlier in the prompt, they should copy that association. If the underlying logic is truly OOD and lacks these retrieval cues, the models typically fail.

Real-World Evidence

Testing on reversed-label tasks (e.g., Antonym, Country-capital) revealed that performance is often tied to the model's ability to retrieve the correct mapping from the context rather than generalizing the "reversed" rule to new queries.

Top-5 accuracy on reversed-label tasks across various NLP benchmarks. The disparity between 'rvs-query' and 'rvs-target-label' highlights the reliance on retrieval cues over rule-based generalization.

Key Results

  • Llama-3 Failure: On a truly OOD synthetic word classification task, Llama-3-8B performed at chance levels (10% accuracy). However, performance improved significantly when the task was framed as a retrieval problem where the answer was explicitly present in the context.
  • Convergence to Gradient Descent: In synthetic math tasks, as context length increased, the ICL performance of a Transformer trained on function class $A$ closely approached the performance of a standalone model of class $A$ trained via Gradient Descent on those same examples.
  • Double Descent: The researchers observed a "double descent" error curve in OOD tasks, a classic machine learning phenomenon that confirms the Transformer is behaving like a specific model class being optimized on the fly.

Conclusion and Future Work

This research suggests that ICL is not a "blank slate" learner but an efficient algorithm selector constrained by its pretraining. While models can perform impressive feats of retrieval and optimization, their ability to generalize to truly OOD logic is limited. Future work should explore how to expand the pretraining hypothesis space or develop architectures capable of breaking out of their training distributions to achieve genuine novel task learning.

Summary updated

BibTeX
@inproceedings{wang2025can,
  author = {Qixun Wang and Yifei Wang and Yisen Wang and Xianghua Ying},
  title = {{Can In-context Learning Really Generalize to Out-of-distribution Tasks?}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=INe4otjryz},
}
Figure