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

Qixun Wang, Yifei Wang, Xianghua Ying, Yisen Wang

TL;DR

Large Language Models (LLMs) don't actually "learn" brand-new tasks during in-context learning (ICL). Instead, they perform a sophisticated form of "algorithm selection," picking the best-fitting function from their pretraining data and refining it. This process ultimately fails when the model is faced with truly novel, out-of-distribution (OOD) logic.

What Problem Does This Paper Address?

In-context learning (ICL) is the "magic" that allows LLMs to improve their performance on a task simply by seeing a few examples in the prompt. While researchers have shown that LLMs can solve seemingly new tasks (like linear regression or classification with abstract labels), a debate has emerged: Is the model actually learning a new input-output mapping on the fly, or is it just identifying a task it already knows?

This paper investigates the limits of ICL when it encounters Out-of-Distribution (OOD) tasks—scenarios where the underlying logic or the data distribution differs significantly from what the model saw during training. The authors aim to uncover the actual mechanism behind ICL: does it generalize, or does it just "retrieve and refine"?

Methods

The authors combine controlled synthetic experiments with evaluations of real-world LLMs:

  1. Synthetic Function Learning: They trained a GPT-2 model from scratch on specific mathematical function classes (e.g., linear regression, quadratic regression, or ReLU neural networks). They then tested if a model trained on one class (like linear) could "learn" a different class (like quadratic) via ICL.
  2. Predict-then-Retrieve Analysis: To understand how models handle "abstract labels" (e.g., using "foo" instead of "positive"), they designed tasks that separate the logic of the task from the retrieval of the label.
  3. Theoretical Modeling: They used a "Mixed Gaussian" framework to mathematically prove how ICL weights different "algorithms" it learned during pretraining when faced with a new context.
  4. Real-World Validation: They tested Llama-2 and Llama-3 on tasks involving reversed letters and ambiguous classification criteria to see if the "algorithm selection" behavior holds in complex models.

Datasets

  • Synthetic Math: Linear, Quadratic, ReLU NN, Square Root, Cubic, and Sigmoid regression tasks.
  • Retrieval Tasks: Custom-built sequences using random word embeddings to test the model's ability to map queries to specific tokens in the context.
  • NLP Benchmarks: Standard datasets for Antonyms, Country-Capital, and Translation (English-French/German), modified with reversed labels.
  • Ambiguous Classification: A custom dataset where sentences can be classified by sentiment, type, or location, used to test which "criterion" the model selects.

Key Insights and Ideas

  • The "Hypothesis Space" Limit: ICL doesn't invent new functions. If a model was pretrained on linear functions, it will try to solve a quadratic problem by finding the "best-fitting linear function" using gradient descent in its head. It stays within its pretraining "hypothesis space."
  • Abstract Labels are an Illusion: The famous ability of LLMs to use "foo/bar" as labels isn't "task learning." It’s a two-step process: the model identifies the task (sentiment analysis) and then uses its retrieval mechanism to swap the standard label for the one provided in the context. If the underlying task logic is OOD, this ability vanishes.
  • Algorithm Selection: When a model is trained on multiple tasks, ICL acts as a selector. It calculates which of its internal "programs" has the lowest error on the provided examples and gives that program the most weight.
  • Low-Test-Error Preference: Theoretically and empirically, the authors show that ICL is biased toward the pretraining function that minimizes error on the current context, rather than adapting to a truly novel mapping.

Key Results

  • Failure on OOD Math: A GPT-2 model trained on linear regression exhibits a "double descent" error curve even when tested on quadratic tasks, proving it is strictly implementing a linear solver regardless of the context's complexity.
Evaluation on synthetic math tasks
  • Llama-3's Hard Ceiling: Llama-3-8B achieved near-random accuracy (10%) on a synthetic OOD vector classification task but performed excellently on a retrieval version of the same task. This suggests that even state-of-the-art models struggle with novel logic but excel at pattern matching.
  • Preference for ID over OOD: In reversed-label tasks, Llama-3 was more likely to simply reverse the query word (an "In-Distribution" task it knows well) than to follow the "OOD" logic of predicting a reversed label.
Accuracy on reversed-label NLP tasks

Limitations and Future Work

  • Model Scale: The study primarily uses GPT-2 and 7B/8B parameter models. It remains an open question whether massive models like GPT-4 or Claude 3 Opus—which have seen vastly more diverse data—might possess a "threshold" of diversity that enables true OOD generalization.
  • Pretraining Objective: The models were trained specifically on ICL data for the synthetic parts. Real LLMs are trained on general text, which might create different internal "algorithms."
  • Future Direction: The authors suggest exploring whether increasing the diversity of pretraining tasks (rather than just the volume of data) can eventually force a model to learn a "meta-algorithm" that truly generalizes to OOD scenarios.

Summary updated

BibTeX
@inproceedings{wang2025can,
  author = {Qixun Wang and Yifei Wang and Xianghua Ying and Yisen Wang},
  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