Graph Query Generation with Constraint-guided Large Language Agents

Mengying Wang, Nicolaas Jedema, Rahul Pandey, RaviKiran Krishnan, Jens Lehmann, Yinghui WuarXiv 2026

TL;DR

UniQGen is a training-free, schema-less framework that leverages LLM agents guided by a classic database algorithm—Chase & Backchase—to generate precise graph queries across different languages like Cypher and SPARQL. By decoupling user intent extraction from language-specific syntax, it eliminates "graph-model lock-in" and significantly outperforms heavily fine-tuned baselines on complex, multi-hop questions.

What Problem Does This Paper Address?

Knowledge Graph Question Answering (KGQA) systems help users query massive databases using natural language. However, the industry is split between two paradigms: the Resource Description Framework (RDF) model (queried via SPARQL) and Labeled Property Graphs (LPG) (queried via Cypher). This creates a "graph-model lock-in" effect, where choosing one stack makes migrating later incredibly costly.

While Large Language Models (LLMs) are great at translating text to code, applying them directly to graph query generation introduces major challenges:

  1. Hallucination/Misalignment: Generating queries that don't match the actual graph schema or topology.
  2. Missing Pragmatic Constraints: Failing to capture implicit user intents (e.g., realizing "Which drugs treat tetany?" implies looking for approved drugs).
  3. High Maintenance Costs: Relying on schema-specific fine-tuning that breaks whenever the graph updates.
Fig. 1: Given a query: “Which drugs are used to treat tetany?” Rule‑based methods assume a non-existent relation and returns None; Learning-based methods reach the disease but over-includes Ca, failing to address pragmatic constraints; LLM‑assisted, constraint-based methods generation captures implicit intent and respects ontology, yielding clinically appro- priate answers.

Methods

The authors introduce UniQGen, an autonomous pipeline that avoids fine-tuning entirely by combining LLM reasoning with a formal database optimization technique called Chase & Backchase (CaB).

The framework operates in three main stages:

  1. Query-Centric Fact Extraction: The system performs entity linking on the natural language query and extracts a local, multi-hop subgraph to serve as factual context.
  2. CTable Construction: An LLM agent ($a_1$) builds a language-agnostic Constraint Table (CTable). This intermediate representation captures triple patterns, value filters, and implicit pragmatic constraints, assigning an uncertainty score to each.
  3. Chase & Backchase Loop: A second agent ($a_2$) uses beam search to refine the query:
  4. Query Chase (QChase): A top-down phase that starts with all constraints and iteratively relaxes them (dropping constraints one by one) to maximize completeness against a reference answer set provided by an LLM oracle.
  5. Query Backchase (QBackchase): A bottom-up phase that reintroduces essential constraints to ensure soundness and minimality (removing redundant clauses).

Finally, a pluggable rendering layer translates the optimized logical plan into the target language (Cypher or SPARQL).

Fig. 2: UniQGen Framework Overview

Datasets

The approach was evaluated using a massive snapshot of Freebase (2.9 billion triples, 116 million entities) deployed on Amazon Neptune. The authors tested UniQGen on three popular KGQA benchmarks using only their test sets (since UniQGen requires no training):

  • GraphQ: A low-resource, characteristic-rich testbed.
  • GrailQA: A benchmark covering three generalization settings (i.i.d., compositional, and zero-shot).
  • WebQSP: A diverse dataset reflecting real-world, multi-hop user queries.

Key Insights and Ideas

  • Marrying LLMs with Formal DB Theory: Pure prompt engineering often fails at complex code generation. By wrapping the LLM inside a formal Chase & Backchase loop, UniQGen provides mathematical guarantees for relative completeness and soundness at runtime.
  • The Power of a Language-Agnostic IR: Abstracting queries into a CTable allows the system to remain entirely modular. The same logical intent can be effortlessly rendered into either SPARQL or Cypher depending on the underlying database engine.
  • Schema Bias Matters: The authors discovered that the "winning" query language flips depending on the dataset. Cypher performed better on GraphQ because its patterns map cleanly to labeled-node/edge models, while SPARQL won on WebQSP due to its frequent interaction with RDF-specific structures like mediator/CVT nodes.

Key Results

UniQGen consistently outperformed state-of-the-art, fine-tuned baselines (like Pangu and ArcaneQA) across multiple benchmarks:

  • GraphQ: Achieved an absolute F1 gain of 31.6% over the closest baseline.
  • GrailQA: Delivered a 4.9% F1 improvement overall, showing remarkable strength in zero-shot and i.i.d. splits.
  • Graceful Degradation: As query complexity scaled up (from 1-hop to 3-hop questions), UniQGen's performance degraded much more gracefully than traditional neural methods, proving the value of execution-based validation.

In terms of efficiency, UniQGen averaged around 40 seconds per query generation with a median knowledge graph execution latency of just 0.25 seconds, demonstrating strong viability for enterprise workloads.

Fig. 3: Effectiveness & Query Quality

Limitations and Future Work

The primary limitation of UniQGen is its dependency on an LLM oracle reference answer set to guide the Chase/Backchase loop. If the initial oracle answers contain errors (false positives or negatives), those errors can propagate through the pipeline, causing the framework to overfit to an incorrect reference set.

The authors suggest that future work should focus on integrating higher-trust sources for the reference set, such as multi-prompt consensus, external fact-checking mechanisms, or gold-standard query memories.

Summary updated

BibTeX
@misc{wang2026graph,
  author = {Mengying Wang and Nicolaas Jedema and Rahul Pandey and RaviKiran Krishnan and Jens Lehmann and Yinghui Wu},
  title = {{Graph Query Generation with Constraint-guided Large Language Agents}},
  year = {2026},
  eprint = {2605.00845},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.00845},
}
Figure