Manuscript Offprint

arXiv:2610.41755

CypherGuard: Database-Grounded Correction for Reliable Text-to-Cypher

Zebin Chen, Dong Wen, Makbule Gulcin Ozsoy, Wenjie Zhang

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Zebin Chen

University of New South Wales

Sydney, AU

zebin.chen1@unsw.edu.au

Makbule Gulcin Ozsoy

Neo4j

London, UK

makbule.ozsoy@neo4j.com

Dong Wen

University of New South Wales

Sydney, AU

dong.wen@unsw.edu.au

Wenjie Zhang

University of New South Wales

Sydney, AU

wenjie.zhang@unsw.edu.au

ABSTRACT

Text-to-Cypher translates natural-language questions into Cypher queries over property graphs. Graph database systems increasingly adopt this translation as a native query interface. Large language models generate fluent Cypher but stay unreliable. On Cypher-Bench a small model answers only 31.4% of questions correctly, and even a strong model plateaus at 71.76%. The dominant failure modes are structural rather than linguistic. For example, a generated query may parse correctly and follow the schema, yet still return zero rows because it traverses a relationship against the stored graph direction. To address this issue, we present CypherGuard, a training-free framework that detects and corrects these structural faults. Our approach uses two core components: a graph-native error taxonomy to guide repair, and a mined rule library to guide query generation. At inference time, CypherGuard first rewrites schema-detectable faults, then re-executes each draft and repairs it only when the query errors or returns nothing. On all 2,348 CypherBench test questions, across seven graphs, CypherGuard reaches 88.50% overall execution accuracy, +25.08% over the same-backbone zero-shot floor, surpassing an RL-fine-tuned 7B specialist and the strongest training-based system that we compare against.

1 INTRODUCTION

Querying data in natural language is a long-standing ambition of the database community, pursued on the relational database [28, 47, 58] and, as question answering over knowledge graphs, on the graph [2, 54]. Large language models have made the drafting step cheap, and the relational side has industrialized accordingly. Text-to-SQL systems, open models, and benchmark-scale evaluations are standing subjects at data-management venues [14, 26, 30]. The graph side has reached the same moment. Graph DBMS vendors ship text-to-Cypher datasets, fine-tuned baselines, and integration tooling [25, 37], and GraphRAG pipelines demonstrate graph-backed retrieval at scale [11].

Text-to-Cypher methods are not yet reliable, and the techniques that made text-to-SQL trustworthy do not transfer to text-to-Cypher directly. Text-to-Cypher still falls far short of high execution accuracy. On Spider, the standard text-to-SQL benchmark, GPT-4 with retrieved few-shot examples reaches 86.6% execution accuracy [14]. On CypherBench, an execution-scored text-to-Cypher benchmark,

Q: How many countries does Germany maintain diplomatic relations with?
✗ Naive draft (reads a mutual relation as undirected)
MATCH (:Country {name: 'Germany'}) - [:hasDiplomaticRelationWith] - (c:Country) RETURN count(c)
→ wrong: stored both ways. The undirected match therefore counts each partner twice. Execution returns rows, no alarm.
✓ CypherGuard (storage-reciprocity gate: the probe measures the relation reciprocal, and undirected becomes directed)
MATCH (:Country {name: 'Germany'}) - [:hasDiplomaticRelationWith] - (c:Country) RETURN count(c)
→ correct.

Figure 1: A schema-consistent draft on a CypherBench relation runs without error yet returns a wrong count, a fault no execution signal exposes.

claude-3.5-sonnet reaches only 61.6% and gpt-4o only 60.2% zero-shot [12]. RL-tuned Cypher-RI reaches only 69.6% [50] and CypherSmith reaches only 74.8% [66].

Figure 1 illustrates one common failure pattern in text2cypher. Consider asking a model to count how many diplomatic partners a country has. While hasDiplomaticRelationWith appears naturally mutual and undirected, the graph stores it as two directed edges. Consequently, an undirected query pattern matches each relationship twice. The generated query parses correctly, follows the schema, and executes without errors, yet silently returns doubled counts. Prompting the model again cannot expose the double counting. Similarly, simple execution fails to catch the bug because the query still returns rows. Detecting this mismatch requires measuring the actual relationship storage pattern in the database.

Prior work covers only some faults. Models frequently make subtle errors in NL2Cypher task. They traverse relationships backwards, connect labels unsupported by the schema, inflate counts through path fan-out, or add unprompted ORDER BY and LIMIT clauses. However, syntactically and at the schema level, these queries remain valid. Static schema checks accept such queries, and running them often raises no error either. Prior work already checks a few of these faults. LangChain's Cypher corrector rewrites relationship directions that contradict the schema [25]. [36] filter sampled candidates with an openCypher grammar and a relationship-direction check. They also report an iterative refinement loop whose verifiers include a direction check, an execution check, and an LLM judge [35]. Existing taxonomies categorize these errors purely post-hoc [12, 52]. However, no text-to-Cypher method uses these categories to steer query generation.

Existing work improves text-to-Cypher accuracy along two routes. The first route trains the model on Cypher data. [50] fine-tunes the model with execution-based rewards using Reinforcement learning (RL). Cypher pairs [66] trains the model on question via Supervised fine-tuning (SFT). The second route leaves the model unchanged and works at inference time. Multi-agent frameworks assign query generation, result checking, and repair to separate LLM agents [18]. Execution-feedback methods regenerate a query after the database reports an error [23]. Both routes have limitations. Training needs gold-labeled data. RL requires a gold query and an executable database for every training question, and has been trained on a benchmark training split. SFT faces a data shortage. Public Cypher datasets hold at most tens of thousands of examples, and recent work synthesizes hundreds of thousands with LLMs. Inference-time methods take their correction signal from the database's error message or from an LLM critic's judgment of the returned rows. A silent fault raises no error, and the critic's judgment of the returned rows is not checked against the database.

We present CypherGuard, an admission-control layer between the LLM and the graph database. Admission here means screening queries and rewrites. Our pipeline consists of three stages: (1) Draft: The LLM generates an initial query guided by mined error-prevention rules and retrieved demonstration examples. (2) Schema Fixes: A deterministic module corrects structural faults revealed by schema constraints or storage statistics, such as an undeclared relationship type. (3) Execution Loop: If a query fails or returns an empty result, the LLM identifies the error type from our Cypher taxonomy and rewrites the query. The layer's rule library is mined once per dataset, by prompting an LLM agent over the backbone's failures on that dataset's train split.

Our contributions are:

  • Repairs backed by database signals. We frame reliable text-to-Cypher as admission control. The framework consults multiple signals: question phrasing, schema constraints, stored-data statistics, and execution feedback. Each signal triggers only targeted repairs for the specific error it detects. Furthermore, the system accepts a proposed rewrite only if it passes re-verification or replaces an already-failed query.

  • A transferable Cypher error taxonomy. We distil recurring text-to-Cypher failures into eight families and 32 fine-grained codes. Seven families, spanning 28 codes, name faults that leave the query executable, such as a reversed relationship direction, a wrong pattern topology, or an aggregation over fan-out-inflated rows.

  • A graph-mined rule library. We automatically mine preventive rules to guide query generation. Specifically, an LLM agent analyzes execution-verified failures from the backbone model across training graphs. The agent extracts one rule per recurring failure mechanism using a standardized prompt. To ensure broad applicability, an independent generality validator filters these rules to keep them graph-agnostic.

  • Dataset validation. On CypherBench, CypherGuard reaches 88.50% overall execution accuracy (Section 5). This performance marks a +25.08% improvement over the same-backbone zero-shot baseline. Applying our framework to stronger backbone yields a comparable performance gain. Furthermore, CypherGuard outperforms the reported RL-fine-tuned 7B Cypher specialist.

References (36)