KG2Code: Bridging Knowledge Graphs and Large Language Models via Executable Code for Question Answering

Yike Wu, Nan Hu, Guilin Qi, Guohui Xiao, Chen Jiang +9 more
6/26/2026
cs.AI
rag

No confident code implementation found for this paper.

Abstract

Recent research has explored the integration of knowledge graphs (KGs) with large language models (LLMs) to enhance their performance on downstream knowledge-intensive tasks, particularly knowledge graph question answering (KGQA). Existing approaches primarily combine LLMs with KGs through retrieval-augmented generation (RAG)-based, agent-based, and SPARQL-based methods.

Show full abstract

Although these methods have achieved notable success, they still suffer from several limitations, including structural information loss, unfaithful reasoning, and limited flexibility and generalization. To address these challenges, this paper proposes KG2Code, a novel approach that transforms knowledge graphs into a code-based representation, preserving structural semantics while naturally aligning with the code-aware pretraining of modern LLMs. Based on KG2Code, KG2Code-QA is further introduced as a KGQA framework that formulates KGQA as a code generation task. This formulation enables the generation of verifiable reasoning traces and executable code, thereby substantially mitigating the impact of hallucinations. In addition, an automated pipeline is developed to construct a large-scale, high-quality code corpus for effectively training open-source LLMs on KG2Code-QA. After training, LLMs are able to perform KGQA in zero-shot scenarios. Extensive experiments demonstrate that the proposed approach significantly outperforms existing KG-enhanced LLM methods for KGQA, while exhibiting strong generalization to unseen KGs. The code and data are available at Github.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{wu2026kgcode,
  title  = {KG2Code: Bridging Knowledge Graphs and Large Language Models via Executable Code for Question Answering},
  author = {Yike Wu and Nan Hu and Guilin Qi and Guohui Xiao and Chen Jiang and Xinchun Zou and Yuchen Lu and Songlin Zhai and Yongrui Chen and Yuyang Zhang and Xiaoguang Li and Lifeng Shang and Jiaoyan Chen and Jeff Z. Pan},
  year   = {2026},
  eprint = {2607.22652},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2607.22652v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2607.22652.svg)](https://codeofpaper.com/paper/2607.22652)

Discussion