Are Large Language Models In-Context Graph Learners?

Jintang Li, Ruofan Wu, Yuchang Zhu, Huizhe Zhang, Liang Chen, Zibin ZhengarXiv 2025 · DOI

Abstract

Large Language Models (LLMs) have demonstrated remarkable capabilities in processing unstructured natural language, yet their performance on structured, non-Euclidean graph data remains a significant challenge. This paper investigates whether off-the-shelf LLMs can function as effective in-context graph learners. By reframing graph learning as a Retrieval-Augmented Generation (RAG) process—where a node's local neighborhood serves as the retrieved context—the authors demonstrate that LLMs can match or outperform specialized Graph Neural Networks (GNNs) without any fine-tuning. We present three frameworks—QueryRAG, LabelRAG, and FewShotRAG—that leverage graph topology as a structural prior for context selection.

Introduction

While LLMs excel at in-context learning (ICL) for language, they often stumble when faced with structured graph data. Traditional attempts to bridge this gap involve "textualizing" graphs into edge lists or adjacency matrices, which frequently results in the loss of critical structural information. Consequently, general-purpose LLMs have historically lagged behind specialized models like Graph Convolutional Networks (GCNs) or Graph Attention Networks (GATs).

The central question addressed in this research is whether LLMs are inherently capable of in-context graph learning. The authors argue that the performance gap is not merely a matter of input format but of how context is provided to the model. By bridging the conceptual gap between GNN "message passing" and LLM "retrieval," they aim to unlock latent graph reasoning capabilities in general-purpose models.

Methodology: GNNs as RAG Networks

The core innovation of this work is the formalization of GNN message-passing as a recursive RAG process. In a traditional GNN, a node updates its state by aggregating information from its neighbors. The authors translate this mathematical operation into a natural language prompting framework.

Conceptual mapping between Graph Neural Networks (GNNs) and Retrieval-Augmented Generation (RAG).

The GNN update function, typically expressed as $h_i = UPD(x_i, AGG(x_j))$, is conceptually mapped to the RAG formulation $r_i = GEN(q_i, AUG(c_j))$. Based on this mapping, three RAG-based prompting frameworks are proposed:

  1. QueryRAG: The LLM is provided with the textual attributes of a target node’s neighbors as reference context, mimicking the feature aggregation step in GNNs.
  2. LabelRAG: The LLM receives the labels (categories) of the neighbor nodes, implementing a natural language version of "label propagation."
  3. FewShotRAG: A hybrid approach that provides the LLM with both neighbor attributes and their corresponding labels as high-quality, structurally relevant few-shot examples.
Overview of the three proposed RAG-based prompting frameworks: QueryRAG, LabelRAG, and FewShotRAG.

Unlike standard RAG, which searches a massive external database for semantically similar text, these frameworks use the graph topology itself as the retrieval mechanism. This ensures that the context provided to the LLM is structurally relevant to the query node.

Experimental Setup

The frameworks were evaluated on eight diverse text-attributed graph (TAG) datasets, where nodes contain rich natural language descriptions and edges represent meaningful relationships:

  • Citation Networks: Cora, Pubmed, and Ogbn-Arxiv-TA (paper abstracts and citations).
  • E-commerce Benchmarks: Sports-Fitness, Ele-History, Ele-Computers, Books-Children, and Books-History (product descriptions and co-purchases).

Key Insights

Several profound insights emerged from the experimental results:

  • Structure as a Retrieval Prior: Using graph edges to select context is significantly more effective than using semantic similarity (standard RAG) or random selection. The graph structure acts as a "hard" prior that identifies the most relevant information for the LLM.
  • The Locality of Context: Standard few-shot prompting (using random examples) often decreased LLM performance compared to zero-shot. This suggests that for graphs, context is only beneficial if it is "local" context derived from the graph topology.
  • Label Propagation via Prompting: LabelRAG proved exceptionally powerful, indicating that LLMs are highly adept at identifying patterns in neighbor categories to classify a target node.

Results and Performance

The proposed RAG frameworks significantly boosted LLM performance, often surpassing supervised baselines. On the Cora dataset, DeepSeek-V3 using FewShotRAG achieved 86.5% accuracy, outperforming supervised GCN (81.3%) and GAT (82.1%). Furthermore, these RAG-augmented LLMs matched the performance of specialized "Graph LLMs" (such as GLEM or TAPE) without the need for expensive fine-tuning or complex multi-stage pipelines. These improvements were consistent across various backbone models, including Llama-3.1-8B, Qwen2.5, and Gemma-2.

Limitations and Future Work

Despite these promising results, several challenges remain. The current approach assumes "homophily"—the principle that connected nodes are similar—and may require adaptation for heterophilic graphs. Additionally, the experiments focused on one-hop neighbors; scaling to multiple hops may encounter token limit constraints or "lost in the middle" issues. Future research will explore dynamic graphs, noisy edge environments, and methods to reduce the computational cost of prompting LLMs for large-scale graph inference.

Summary updated

BibTeX
@misc{li2025are,
  author = {Jintang Li and Ruofan Wu and Yuchang Zhu and Huizhe Zhang and Liang Chen and Zibin Zheng},
  title = {{Are Large Language Models In-Context Graph Learners?}},
  year = {2025},
  eprint = {2502.13562},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.13562},
}
Figure