CoDe-KG: Code-Driven Knowledge Graphs
- CoDe-KG is a framework that uses code as a structured medium to represent, execute, and reason over knowledge graphs.
- It integrates schema-aware prompts, DSL-based tool calls, and object-oriented mapping to enhance triple generation and KG construction.
- The approach demonstrates improved efficiency and precision, addressing the limitations of flat textual representations in KG and LLM integration.
CoDe-KG can be understood as an umbrella label for research programs that use code, code-like programs, or executable program structures as the representation medium, reasoning substrate, or interface for knowledge graphs. In this line of work, code is not merely an output format: it is used to encode triples as class instances, formulate multi-hop KG reasoning as executable tool calls, represent KG schemas as Python classes, and couple retrieval with verifiable execution. The term therefore spans several closely related directions: generative knowledge graph construction from text, autonomous reasoning over existing KGs, knowledge-graph-augmented code generation, and dynamic co-augmentation of KGs and LLMs (Bi et al., 2023, Jiang et al., 2024, Chen et al., 6 Aug 2025, Ding et al., 2 Jun 2026, Li et al., 2024).
1. Conceptual scope and defining characteristics
A recurring premise across CoDe-KG-style systems is that flat textual serialization is a poor interface for structured knowledge. CodeKGC states this explicitly: traditional generative KGC methods “flatten natural language into serialized texts or a specification language,” whereas code is a structured, semantically constrained language in which triples can be expressed as objects and extraction can be cast as code completion (Bi et al., 2023). Code-on-Graph makes an analogous argument from the KGQA side: predefined operators are limited in compositional expressiveness, and direct prompt injection of factual triples is unscalable; representing KG schemas as Python classes and reasoning over instantiated objects is proposed as a remedy (Ding et al., 2 Jun 2026).
Within this scope, CoDe-KG is not one fixed architecture. In CodeKGC, the central task is end-to-end generation of triples from text using schema-aware code prompts and code LLMs (Bi et al., 2023). In KG-Agent, the key abstraction is a domain-specific programming language over a KG, executed by a KG-based executor and controlled by an LLM planner (Jiang et al., 2024). In KGA-ECoT, the emphasis shifts to code generation guided by a domain KG of mathematical APIs, coupled with a Structured Task Graph, GraphRAG retrieval, and external execution (Chen et al., 6 Aug 2025). In DALK and SAC-KG, the focus is KG construction and maintenance, with LLMs serving as relation extractors, rerankers, verifiers, and selective expanders (Li et al., 2024, Chen et al., 2024).
This convergence suggests a common design intuition: code supplies an intermediate formalism that is more expressive than fixed tool inventories and more compact than raw triple injection. A plausible implication is that CoDe-KG is best viewed as a family of interfaces between symbolic graph structure and neural sequence models, rather than as a single named method.
2. Code as a representation language for triples and schemas
In CodeKGC, the input text and target graph are converted into a code-structured pair . The generation objective is formulated as
where is a schema-aware prompt and is a Python program representing the triples (Bi et al., 2023). Text is embedded in a docstring, schema information is encoded by class definitions, and the model completes a list of Triple(...) instances rather than emitting a flat textual serialization.
The schema-aware prompt is central. Entity and relation types are represented as subclasses such as Person(Entity), Organization(Entity), Disease(Entity), or AdverseEffect(Relation), while triples are instantiated through a typed constructor. This arrangement exposes type information and relation constraints directly in code. Rationales are optionally added by factorizing generation through an intermediate rationale sequence , with explicit steps for relationship identification, entity extraction, and knowledge graph construction (Bi et al., 2023).
Code-on-Graph generalizes the same idea from instances to schemas. For each predicate in a retrieved subgraph, the framework infers domain and range, then maps entity types into Python classes and relations into typed attributes. Retrieved facts are instantiated as objects only at execution time, so the prompt carries compact class definitions rather than large triple lists (Ding et al., 2 Jun 2026). This is a different representation regime from CodeKGC, but the underlying principle is similar: code is used to expose structural regularities in a form that LLMs already model well.
A related representation move appears in KGA-ECoT, where the domain KG is not a factual entity graph but a SymPy-based DAG of “Catalogue Nodes” and “Callable Nodes.” Here the KG supplies code-level knowledge—function names, signatures, parameters, return types, and usage notes—so that code generation is grounded in a graph of executable APIs rather than in free text alone (Chen et al., 6 Aug 2025).
3. Programmatic reasoning over knowledge graphs
KG-Agent formalizes KG reasoning as a sequence of executable tool calls over a DSL. Its toolbox includes extraction tools such as get_relation({e}), get_head_entity({e}, r), get_tail_entity({e}, r), get_entity_by_type(t), and get_candidate_entity(m); logic tools such as count({e}), intersect([{e}]), union([{e}]), judge({e}, r, o, v), and end({e}); and semantic tools such as retrieve_relation({r}) and disambiguate_entity({e}) (Jiang et al., 2024). An instruction-tuned LLaMA2-7B planner emits one function call at a time, a KG-based executor runs it, and a structured knowledge memory stores question state, toolbox definitions, intermediate KG information, and the history program.
This formulation differs from textual chain-of-thought in that the intermediate reasoning objects are executable symbolic operations. The program trace becomes the reasoning process itself, not merely a verbal explanation. The resulting agent is autonomous in the sense used by the paper: no fixed human-designed multi-round workflow is imposed, and the model decides when to stop by producing end({e}) (Jiang et al., 2024).
KGA-ECoT introduces a different but related programmatic pattern. It first decomposes a math problem into a Structured Task Graph , where nodes are subtasks and edges are dependencies. It then retrieves code-level knowledge from a SymPy documentation KG by GraphRAG, using hierarchical graph embeddings defined recursively as
with 0 the semantic embedding of node 1 and 2 the parent embedding (Chen et al., 6 Aug 2025). The retrieved callable descriptions are injected into the coding prompt, executable Python is generated, and the code is run in a Docker-based sandbox. If execution fails, the framework falls back to answer verification and textual reasoning.
Code-on-Graph pushes programmatic reasoning further by replacing fixed tools with synthesized Python over schema-derived classes. Its three-stage loop—Planning, Coding, Executing—iteratively decomposes a question, retrieves a bounded subgraph, maps the subgraph schema into classes, generates code, instantiates objects from the retrieved facts, and uses execution feedback for correction (Ding et al., 2 Jun 2026). This design is explicitly motivated by two bottlenecks of prior LLM-KG integration: inflexibility of predefined operators and unscalability of direct factual injection. A key reported result is a 40–47× improvement in Token Utility Rate over PoG, indicating that many more question-relevant factual units can be processed per token through class-based abstraction and execution rather than through triple serialization (Ding et al., 2 Jun 2026).
4. KG construction, evolution, and co-augmentation
DALK presents CoDe-KG in a bidirectional form: LLMs construct an evolving Alzheimer’s Disease KG from literature, and the resulting KG is then used to augment LLM inference on AD question answering (Li et al., 2024). The construction phase uses PubTator Central for entity recognition and two relation-extraction modes: pair-wise RE, yielding 3, and generative RE, yielding 4. On 9,764 AD-related papers since 2011, 5 contains 20,545 nodes, 3,651 relation types, and 53,585 triples, whereas 6 contains 13,509 nodes, 3,952 relations, and 171,431 triples (Li et al., 2024).
At inference time, DALK performs coarse-to-fine knowledge selection. The LLM first extracts entities from the question; Sentence-BERT embeddings then support entity linking into the KG; path-based and neighbor-based exploration produce candidate subgraphs; finally, a self-aware knowledge retrieval prompt asks the LLM to rerank the triples and return at most 7 important and relevant ones (Li et al., 2024). These triples are converted into natural-language evidence and supplied to the answering prompt. The reported ablation is notable: DALK with self-aware retrieval reaches 72.6% average accuracy on ADQA, compared with 70.6% without self-aware retrieval and 67.1% for vanilla GPT-3.5-turbo; moreover, the smaller but more precise 8 outperforms the larger 9, indicating that scale without denoising can be counterproductive (Li et al., 2024).
SAC-KG addresses domain KG construction more directly. It treats LLMs as “Skilled Automatic Constructors” organized in a Generator–Verifier–Pruner pipeline (Chen et al., 2024). For each entity, the Generator retrieves top sentences from domain corpora and few-shot examples from DBpedia, then produces candidate triples. The Verifier applies quantity, format, head-entity, head-tail contradiction, and RuleHub-based conflict checks, and can reprompt the LLM with error-specific instructions. The Pruner is a T5 classifier that labels tail entities as "growing" or "pruned" so that only selected tails become heads at the next level (Chen et al., 2024).
The multi-level expansion process yields a tree-like domain KG. In the rice-domain experiments, SAC-KG automatically constructs a KG at the scale of over one million nodes and reaches a precision of 89.32%, with an “over 20% increase in precision rate” compared to existing state-of-the-art methods for KG construction (Chen et al., 2024). This is an especially clear instance of CoDe-KG as controlled, iterative graph building: generation is not trusted in isolation, but is constrained by retrieval, rule-based verification, and learned expansion control.
5. Representative systems and reported empirical profiles
The empirical literature associated with CoDe-KG is heterogeneous in task definition and metric, but several systems report strong gains within their own settings.
| System | Mechanism | Reported outcome |
|---|---|---|
| CodeKGC | Schema-aware code prompts and rationale-enhanced triple generation | On CoNLL04 zero-shot, text-davinci-003 rises from 30.5 F1 with Vanilla Prompt to 41.6 F1 with CodeKGC |
| KG-Agent | DSL tool calls with executor and memory | LLaMA2-7B tuned on 10K samples reaches WebQSP F1 81.0 and GrailQA Overall F1 86.1 |
| DALK | LLM→KG construction plus KG→LLM self-aware retrieval | ADQA average accuracy improves from 67.1% for GPT-3.5-turbo to 72.6% |
| SAC-KG | Generator–Verifier–Pruner for multi-level domain KG construction | Precision 89.32% and over one million nodes |
| KGA-ECoT | Structured Task Graph, GraphRAG, executable Python | On GSM8K with DS-7B, EMA reaches 87.49% |
| Code-on-Graph | Schema-to-class mapping and iterative executable code | Up to 10.5% improvement over prior state-of-the-art models |
CodeKGC evaluates on ADE, CoNLL04, and SciERC using strict triple-level micro F1. In few-shot settings with text-davinci-003, it reports 64.2 on ADE versus 58.8 for Vanilla Prompt, 49.6 on CoNLL04 versus 43.2, and 24.7 on SciERC versus 18.8 (Bi et al., 2023). The paper also reports a re-structured KG pre-training corpus with 22,765,433 sentences, 50,105,623 triples, and 71,413 relation types, used to analyze model-size effects for smaller code models (Bi et al., 2023).
KG-Agent reports that only using 10K samples for tuning LLaMA-7B can outperform state-of-the-art methods using larger LLMs or more data. On WebQSP it reaches Hits@1 83.3 and F1 81.0; on CWQ, Hits@1 72.2 and F1 69.8; on GrailQA, Overall F1 86.1; and on KQA Pro, overall accuracy 92.15 (Jiang et al., 2024). These results are presented as evidence that a compact DSL and executor can make small open models competitive.
KGA-ECoT evaluates on GSM8K, MATH-500, and SVAMP using Exact Match Accuracy or Semantic Equivalence Accuracy. The full framework outperforms CoT, Prompt Selection, and CODEPLAN in most reported settings; for example, on MATH-500 with DS-7B it improves from 82.60% for CODEPLAN to 89.40%, and ablations show that removing external code execution causes the largest performance drops (Chen et al., 6 Aug 2025).
Code-on-Graph reports Hits@1 on WebQSP, CWQ, and GrailQA. With DeepSeek-V3.2, it reaches 88.7 on WebQSP, 79.1 on CWQ, and 91.0 on GrailQA Overall, outperforming prior prompting baselines and showing large gains over ablations that remove iterative planning, error correction, or code reasoning (Ding et al., 2 Jun 2026). The ablation with JSON abstraction performs worse than Python classes, which the paper interprets as evidence that object-oriented structure is especially well aligned with current coding-capable LLMs.
6. Limitations, recurrent failure modes, and terminological breadth
Despite the reported gains, the literature identifies consistent failure modes. CodeKGC notes dependence on strong code-trained backbones, prompt-length sensitivity when rationales are added, and persistent difficulty with acronyms, complex medical terminology, overlapping patterns, and long-range dependencies on ADE (Bi et al., 2023). KGA-ECoT highlights dependency on KG quality and coverage, residual embedding mismatch despite hierarchical propagation, infrastructure costs and latency from Docker execution, and the possibility that the LLM still generates syntactically wrong or semantically incorrect code (Chen et al., 6 Aug 2025). DALK emphasizes the coverage-versus-noise trade-off in automatically constructed KGs, the reliance on PubTator for entity recognition, and the fact that the system is research-oriented rather than ready for clinical deployment (Li et al., 2024). SAC-KG similarly stresses residual factual errors beyond rule coverage, cost of LLM generation and verification, and the possibility of lower recall when prioritizing high precision and high domain specificity (Chen et al., 2024).
Programmatic KG reasoning systems show their own characteristic errors. KG-Agent can fail through wrong tool choice or argument composition, since its policy is learned from step-level code generation over a small DSL (Jiang et al., 2024). Code-on-Graph reports subtask planning errors, retrieval errors, reasoning errors, and max-attempt failures, and explicitly notes that its performance depends strongly on the coding ability of the backbone model (Ding et al., 2 Jun 2026). These observations collectively indicate that CoDe-KG does not remove reasoning brittleness; rather, it relocates it into planning quality, schema grounding, retrieval adequacy, and execution robustness.
A common misconception would be to treat CoDe-KG as a single standardized method. The evidence instead points to a loose family of code-centric interfaces between LLMs and graphs: code-format triple generation, DSL-based graph traversal, KG-augmented executable reasoning, and closed-loop LLM↔KG co-development (Bi et al., 2023, Jiang et al., 2024, Chen et al., 6 Aug 2025, Li et al., 2024, Chen et al., 2024, Ding et al., 2 Jun 2026). A distinct synthetic use of the label also appears in coding theory, where “CoDe-KG” is used to organize the study of checkable codes in group algebras 0 (Borello et al., 2019). This suggests that the term is not yet terminologically fixed across research areas.
Within the LLM-and-KG literature, however, the central pattern is stable: code is used to preserve structure, expose schema, enable execution, and reduce the reliance on raw textual serialization of graph facts. The main significance of CoDe-KG lies in this shift from prompt-level fact injection to program-level interaction with structured knowledge.