InteracSPARQL : An Interactive System for SPARQL Query Refinement Using Natural Language Explanations Xiangru Jian, Zhengyuan Dong, M. Tamer Özsu Abstract In recent years, querying semantic web data using SPARQL has remained challenging, especially for non-expert users, due to the language’s complex syntax and the prerequisite of understanding intricate data structures. To address these challenges, we propose InteracSPARQL , an interactive SPARQL query generation and refinement system that leverages natural language explanations (NLEs) to enhance user comprehension and facilitate iterative query refinement. InteracSPARQL integrates LLMs with a rule-based approach to first produce structured explanations directly from SPARQL abstract syntax trees (ASTs), followed by LLM-based linguistic refinements. Users can interactively refine queries through direct feedback or LLM-driven self-refinement, enabling the correction of ambiguous or incorrect query components in real time. We evaluate InteracSPARQL on standard benchmarks ( QALD-9 and QALD-10 ), demonstrating significant improvements in query accuracy, explanation clarity, and overall user satisfaction compared to baseline approaches. Our experiments further highlight the effectiveness of combining rule-based methods with LLM-driven refinements to create more accessible and robust SPARQL interfaces. 1 Introduction Querying RDF (Resource Description Framework) data with SPARQL has long been challenging for users lacking substantial technical expertise Li et al. 2023 ; Amsterdamer and Callen 2021 ; Arenas and Ugarte 2017 ; Diaz et al. 2016 ; Mohamed et al. 2022 . Although SPARQL is a powerful language for working with linked data, its complex syntax, coupled with the need to understand underlying data organization without a clear schema, often creates a steep learning curve. Moreover, RDF resources are typically identified by Internationalized Resource Identifiers (IRIs) , a superset of URIs that accommodates a broader range of characters, which can further complicate query formulation for less experienced users. The difficulty of using SPARQL has been identified as a major issue in user surveys Bonifati et al. 2019 ; Bonifati et al. 2017 ; Arias et al. 2011 . As organizations increasingly adopt RDF-based systems in domains such as life sciences (e.g., Bio2RDF Bio2RDF Project ), social graphs, and knowledge bases (e.g., Wikidata Vrandečić and Krötzsch 2014 and DBpedia Morsey et al. 2011 ), the need for user-friendly interfaces grows Helal et al. 2021 . Figure 1: The overview of InteracSPARQL . While similar challenges also apply to other query languages such as SQL, our focus on SPARQL is motivated by several distinct factors. SPARQL’s extensive use in querying RDF-based linked datasets differs from SQL, particularly due to its graph-oriented query structure, extensive reliance on IRIs, and semantic web applications. Moreover, SPARQL’s complexity, which includes advanced constructs like property paths and complex navigational constructs, makes it particularly challenging for non-experts, as indicated in an overview of SPARQL evaluation Cohen and Kim 2013 . Given these challenges, natural language to SPARQL query (NL2SPARQL) provides the most natural, easy, and practical interface for users to write SPARQL queries. A typical NL2SPARQL pipeline involves interpreting a user’s natural language question, identifying and grounding entities and properties in the target knowledge graph, and composing a complete SPARQL query that reflects the user’s intent. However, existing NL2SPARQL systems suffer from relatively low accuracy (often only 20–40% F1 on challenging benchmarks) Li et al. 2023 ; Jiang et al. 2023 ; Liu et al. 2024 ; Diallo et al. 2024 ; Angles et al. 2022 , and provide little transparency or explainability, making it difficult for users to verify or refine the generated queries. Bridging natural language to SPARQL is particularly challenging because SPARQL is a low-resource language. The performance of these systems is likely to improve with human-in-the-loop assistance based on meaningful explanation and feedback from the system. To address these gaps, recent NL2SPARQL research has explored one-turn SPARQL generation Omar et al. 2023 ; Jiang et al. 2023 ; Xie et al. 2022 ; Yu et al. 2023 , which directly transforms a natural language question into a complete SPARQL query in a single step without interactive refinement. Despite its convenience, most works in this approach typically yields low accuracy due to the complexity and ambiguity of user queries, and offers no means for users to inspect or correct the query logic. For some specialized models Rony et al. 2022 ; Xie et al. 2022 can perform well on certain benchmark, they all need intensive training and thus lack of generalization ability to others. Building on these efforts, a number of tools have adopted an interactive SPARQL query refinement paradigm Amsterdamer and Callen 2021 ; Abramovitz et al. 2018 ; Ochieng 2020 ; Letelier et al. 2012 . Such tools let users iteratively adjust their queries—often by inspecting intermediate results or invoking domain-specific heuristics—to converge on the desired logic. However, they typically support only a subset of SPARQL’s full feature set (for example, omitting property paths or complex filtering) and rarely supply clear, structured natural-language explanations of each query component. As a result, novice users still struggle with opaque syntax, and even expert users find the limited feature coverage and feedback mechanisms leave critical queries unrefined. In this paper, we introduce InteracSPARQL , an interactive SPARQL query generation and refinement system that addresses these challenges with a holistic design (Fig. 1 ). InteracSPARQL is intended to be used either as a companion to NL2SPARQL models to assist in generating correct queries, or as part of training systems that help users learn SPARQL on all RDF-based systems with minimal adaptation needed. Our key contribution is to develop an interactive tool that assists users in refining their queries by providing natural language explanations (NLE) for each section of a SPARQL query. By combining a rule-based method for deriving a structured, deterministic explanation from the original SPARQL query (or its abstract syntax tree, AST) with a subsequent LLM-based refinement step, InteracSPARQL yields explanations that are both accurate and linguistically polished. The interactive refinement is done either by direct oversight or through an LLM “self-refinement”. As a result, corrections to mislabeled entities, ambiguous filters, or incomplete clauses are achieved in real time. Furthermore, InteracSPARQL supports users who wish to author SPARQL directly by guiding them step-by-step through query construction, providing continuous, targeted feedback and effectively serving as an interactive learning guide to SPARQL. This educational dimension is woven throughout the paper: our methodology formalizes a step-by-step construction mode, our experiments evaluate not only accuracy but also the usefulness of explanations as a learning aid, and our human study explicitly measures the extent to which users gain confidence and understanding of SPARQL syntax through interactive guidance. The design of InteracSPARQL tackles several core difficulties. One is how to systematically split SPARQL syntax into interpretable modules—such as Basic Graph Patterns (BGPs) , FILTER clauses, or advanced constructs like GROUP BY —so that users can see and modify each component. Another challenge is ensuring that iterative refinements stay aligned with the user’s goals, especially when advanced features (like subqueries or property paths) are involved. Additionally, controlling the interaction to minimize the number of required user revisions is also essential, since excessive revisions often deter users due to frustration and inefficiency. To address these challenges, our approach couples incremental improvements with dynamic tool-based entity and property lookups , letting the user or an LLM swiftly resolve uncertain references and ambiguities in real time. Ultimately, the synergy of NLEs and multi-round refinement aims to make SPARQL more transparent and approachable for novices, while retaining robust capabilities for domain experts who need complex queries. Our work presents a fresh perspective on integrating modular explanation and iterative refinement into the SPARQL generation pipeline. Specifically, we: 1. propose a two-stage NLE framework , which systematically parses SPARQL queries into structured explanations via rule-based AST analysis, followed by linguistic refinement using LLMs. This design ensures accuracy, interpretability, and fluency of generated explanations. 2. develop an interactive query construction and refinement framework that (i) primarily supports iterative improvements via direct user input or automated LLM-driven self-refinement, aligning queries closely with user intent; (ii) also guides users through each step of SPARQL authoring, providing continuous, targeted feedback as an educational aid. (iii) and can be easily adopted by any RDF-based system without any training. 3. introduce a dynamic, tool-assisted entity and property linking mechanism , invoked during interactive refinement, which efficiently resolves domain-specific ambiguities and contributes to stable convergence toward correct and precise IRIs. 4. conduct comprehensive experimental evaluations using the QALD benchmarks, demonstrating significant improvements in query accuracy, iterative refinement capability, and overall user satisfaction compared to state-of-the-art methods. Importantly, we further validate the quality and practical utility of our explanations through a human evaluation study, confirming substantial improvements in clarity, completeness, correctness, and utility. Additional technical details and extended examples are provided in a supplementary Appendix (denote as Appendix in the paper) available at: https://bit.ly/interacSparql_app. 2 Background RDF and SPARQL are foundational technologies of the Semantic Web, a framework that aims to create a more intelligent, interconnected web. RDF, introduced by the World Wide Web Consortium (W3C), is a standard model for data representation and interchange on the web, facilitating the integration and sharing of information across different domains Ali et al. 2022 ; Wylot et al. 2018 ; Abdelaziz et al. 2017 ; Angles and Gutierrez 2008 ; Arenas and Pérez 2011 ; Shen et al. 2015 ; Liu et al. 2021 . RDF represents data as triples, consisting of a subject, predicate, and object (usually represented as (s,p,o) ), which together form a graph structure. This simple yet flexible model allows for the expression of complex data relationships and supports interoperability between heterogeneous data sources. Formally, an RDF dataset and an RDF graph can be defined as in Definitions 1 and 2 . Definition 1 . Let \mathcal{I},\mathcal{B},\mathcal{L}, and \mathcal{V} denote the sets of all URIs, blank nodes, literals, and variables, respectively. A triple (s,p,o)\in(\mathcal{I}\cup\mathcal{B})\times\mathcal{I}\times(\mathcal{I}\cup\mathcal{B}\cup\mathcal{L}) is an RDF triple, where s , p and o are called subject, property (or predicate) and object. A set of RDF triples form a RDF dataset. Definition 2 . A RDF graph G=\langle V,L_{V},f_{V},E,L_{E},f_{E}\rangle is a six-tuple , where 1. V=V_{r}\cup V_{l} is a collection of vertices that correspond to all subjects and objects in RDF data, where V_{r} and V_{l} are collections of resource vertices and literal vertices, respectively. 2. L_{V} is a collection of vertex labels. 3. A vertex labeling function f_{V}:V\rightarrow L_{V} is a bijective function that assigns to each vertex a label. The label of a vertex u\in V_{l} is its literal value, and the label of a vertex u\in V_{r} is its corresponding URI or the blank node identifier. 4. E=\{\overrightarrow{u_{1},u_{2}}\} is a collection of directed edges that connect the corresponding subjects and objects. 5. L_{E} is a collection of edge labels. 6. An edge labeling function f_{E}:E\rightarrow L_{E} is a bijective function that assigns to each edge a label. The label of an edge e\in E is its corresponding predicate (or called property). An edge \overrightarrow{u_{1},u_{2}} is an attribute property edge if u_{2}\in V_{l} ; otherwise, it is a link edge. SPARQL, also standardized by the W3C W3C 2006 ; Angles and Gutierrez 2008 ; Pérez et al. 2009 ; Arenas and Pérez 2011 ; Hartig et al. 2009 ; Harris and Seaborne 2013 ; Hartig 2012 , is a powerful query language designed to retrieve and manipulate RDF data. An intuitive definition of SPARQL is given in Definition 3 . Definition 3 . A SPARQL query typically includes five parts: 1. The prefix declarations are used to simplify and shorten the URIs (Uniform Resource Identifiers) that are commonly used in RDF data. Prefix declarations allow the user to define a shorthand notation (a prefix) for a namespace URI, making the query more readable and easier to write. 2. The output part of a SPARQL can be in the form of a table of values of variables ( SELECT ), or in a RDF graph specified by a graph template substituting for the variables by each query solution in the graph template ( CONSTRUCT ), or testing whether or not a query pattern has a solution ( ASK ). 3. The dataset definition refers to the specification of the RDF dataset that a query operates on and is identified in the FROM clause. 4. The graph pattern matching part is specified in the WHERE clause and includes a set of triple patterns to be matched as well as OPTIONAL , UNION and FILTER operators. The data source to be matched is specified by FROM in this part. 5. The solution modifier part includes projection, distinct, order and limit operators defined over the graph pattern matching results. If the graph pattern matching part consist of only triple patterns (no OPTIONAL , UNION or FILTER ), this is called basic graph pattern (BGP) query. SPARQL has undergone significant enhancements since its original release in 2008. The current version, SPARQL 1.1 Group 2013 , includes support for updates, property paths, aggregates, subqueries, negation, and nested queries, among other features. Listing 1: A SPARQL Query Example in QALD-10 ⬇ SELECT ? tvShow WHERE { ? tvShow wdt : P31 wd : Q5398426 ; ? tvShow wdt : P161 wd : Q23760 ; ? tvShow wdt : P2437 ? seasons ; ? tvShow wdt : P580 ? startDate . FILTER (? seasons = 4 ) FILTER ( YEAR (? startDate ) = 1 9 8 3 )} The semantics of SPARQL are based on graph pattern matching using homomorphism, where the query engine searches for graph patterns specified in the WHERE clause against the RDF data. If the pattern matches, the variables in the query are bound to corresponding values from the RDF graph, and the query result is constructed accordingly. SPARQL supports various forms of graph pattern matching, including BDPs, optional patterns, and union patterns. To illustrate the syntax and semantics of SPARQL, consider the query in Example 1 . This query retrieves the answer to the natural language question “ What is the TV-show that starred Rowan Atkinson, had 4 seasons and started in 1983? ”. This is formulated in SPARQL query where the answer is bound to the variable ?tvShow that satisfies four conditions: they must be an instance of ( wdt:P31 ) the television series ( wd:Q5398426 ) via the triple pattern ?tvShow wdt:P31 wd:Q5398426 ; they must feature Rowan Atkinson ( wd:Q23760 ) as a cast member ( wdt:P161 ) via ?tvShow wdt:P161 wd:Q23760 ; they must have their number of seasons ( wdt:P2437 ) bound to ?seasons via ?tvShow wdt:P2437 ?seasons ; and they must have their start date ( wdt:P580 ) bound to ?startDate via ?tvShow wdt:P580 ?startDate . The first FILTER clause FILTER(?seasons = 4) ensures only shows with exactly four seasons are considered, while the second FILTER clause FILTER(YEAR(?startDate) = 1983) restricts results to those that began in the year 1983. Finally, the SELECT clause returns each matching ?tvShow . 3 InteracSPARQL System Figure 2: The proposed pipeline for InteracSPARQL . The input is the raw generation of GPT-4o over the natural language question: What is the TV-show that starred Rowan Atkinson, had 4 seasons and started in 1983? , which is incorrect. The output query is produced by InteracSPARQL (Example 1 ) and is identical to the ground truth. In this section, we describe InteracSPARQL that implements a pipeline to generate SPARQL query explanations and perform iterative query refinement upon them. InteracSPARQL addresses critical shortcomings of existing systems, namely, the lack of transparent explanations and a robust mechanism for iterative query improvement. Further comparisons with existing systems are discussed in Section 5 . InteracSPARQL pipeline is depicted in Figure 2 . The steps involved in this process are as follows: 1 parsing the original SPARQL into a structured JSON-based Abstract Syntax Tree (AST), thus providing a machine-readable blueprint for the query; 2 apply the AST to produce concise, hierarchical rule-based NLEs with IRIs replaced by human-readable labels via on-demand lookups; 3 Use an LLM with specially curated few-shot examples to refine rule-based NLEs into a fluent, structured JSON explanation detailing overall intent, query type, variable descriptions, clause modules, and prefix clarifications; 4 Use an interactive query refinement loop to flag problematic clauses or stale entities based on LLM-refined NLEs, gather targeted feedback (from a human or via self-refinement), apply incremental fixes through RDF interaction tools, and repeat until the query’s results and explanation are semantically accurate and align with the user’s intent. In the following, we focus on steps 2 , 3 , and 4 , since these are the technically interesting ones. Step 1 is essential but technically well understood. Furthermore, rather than discuss each of these steps one by one, we take a more integrated approach and discuss the two challenges that these steps address: Natural Language Explanation (Section 3.1 ) and Interactive Query Refinement (Section 3.2 ). Within this organization, Section 3.1.1 explains 2 , Section 3.1.2 focuses on 3 , and Section 3.2 covers 4 . Other details (e.g. details for step 1 ) can be found in Section in Appendix. 3.1 Natural Language Explanation Producing clear, accurate, and intuitive explanations for SPARQL queries poses significant challenges, particularly when the objective is to support iterative refinement by both human users and language models. To address this, we introduce a two-stage approach (illustrated in Fig. 2 ) that carefully balances clarity, accuracy, and computational efficiency. Initially, we employ a structured, rule-based technique to extract precise, deterministic explanations directly from AST (Section 3.1.1 ). This foundational step ensures each SPARQL query element is clearly represented, thereby providing an interpretable, reliable baseline. Subsequently, these structured explanations are passed to an LLM, enriched with carefully selected few-shot examples (Section 3.1.2 ). Leveraging the rule-based foundation allows the LLM to concentrate on linguistic refinement—enhancing readability and capturing nuanced contextual insights—without sacrificing factual accuracy. 3.1.1 Extracting Rule-Based NLE from AST Our method systematically traverses AST’s hierarchical structure and transforms each AST node into succinct, human-readable statements clearly articulating its purpose. For example, in step 2 of Fig. 2 , the complex query components like Basic Graph Patterns (BGPs) and FILTER clauses are decomposed into straightforward sentences (e.g., “The entity “tvShow” has the property wdt:P1113 (number of episodes), and its value is “4”.”). This ensures even complex SPARQL patterns remain transparent and understandable. The details of this process is demonstrated by Example 3 in the Appendix, which is the rule-based NLE of Example 1 . Leveraging Hierarchical Structure for Clarity. Rather than flattening the query into linear explanations, we maintain AST’s hierarchical form. Each node and sub-node explanation explicitly highlights its semantic relationship within the query structure. This design helps users identify precisely where modifications may be required, significantly facilitating targeted query refinement. Contextual Enrichment of Identifiers by Label Search. Recognizing the challenge posed by opaque IRIs, we enhance explanations by integrating labels from underlying knowledge bases (such as Wikidata). Transforming identifiers like wd:Q5398426 into descriptive names like “television series” significantly reduces cognitive load, enabling users to recognize entities and predicates immediately. This structured, rule-based explanation acts as a stable intermediate form, ensuring consistency and reducing the risk of LLM hallucinations. By providing a clear semantic backbone, the LLM can focus purely on enhancing fluency, nuance, and readability—key aspects that directly improve interpretability and user comprehension. 3.1.2 Refinement of NLE via LLMs: Structured Guidance and Enriched Narrative Building upon the concise rule-based explanations, we leverage LLMs to generate linguistically polished and contextually enriched narratives. This is step 3 in Fig. 2 . This refinement process critically integrates two essential components: (1) the structured semantic and hierarchical information extracted from the AST and the rule-based NLE, ensuring accurate preservation of query structure and content; and (2) carefully designed few-shot examples presented in an accessible, transparent, and hierarchical format, serving as good references to guide the LLM towards coherent, structured NLEs. Structured Input for Controlled Refinement Workflow. The LLM refinement stage explicitly leverages structured information from the AST-derived, rule-based explanations, combined with rigorously selected few-shot examples. Rather than interpreting the original SPARQL query anew, the model’s role is constrained to refining validated, structured content. This explicit guidance minimizes semantic inaccuracies, reduces the likelihood of hallucinations, and facilitates domain-specific insights, thereby maintaining precise alignment with the original query intent. Customized JSON-based Output Format. To reinforce clarity and maintain coherence across refinement iterations, we adopt a structured, JSON-based output format that systematically reflects essential query elements. The resulting explanations encompass distinct modular sections: (1) Overall NL explanation , summarizes the primary objective and intent of the query, providing immediate context; (2) Query type , explicitly identifies the query format (e.g., SELECT , ASK ), clarifying its functional purpose within the dataset; (3) Variables , clearly articulate each query variable’s role, connecting it explicitly to broader retrieval logic; (4) Modules (graph patterns and clauses) , present significant query components alongside corresponding SPARQL code snippets and explanatory narratives, preserving the hierarchical query structure; (5) Advanced clauses , highlight advanced query features (e.g., GROUP BY , ORDER BY ), clarifying their effects on query results; (6) Prefixes , provide optional contextual explanations of prefixes to clarify namespace conventions and minimize confusion. By the end of step 3 , each terse, AST-derived clause is transformed into a module in a JSON-formatted explanation, pairing each SPARQL fragment with a polished natural-language description and organizing content into explicit sections for variables, graph patterns, filters, and prefixes. An excerpt of this partial NLE is shown in step 3 of Fig. 2. For example, the pattern ?tvShow wdt:P1113 4 is rendered as “Ensure the television series has exactly four episodes,” replacing the raw IRI with its human-readable label and employing concise, engaging phrasing. The full explanation appears as Example 4 in the Appendix. These structured modules markedly improve clarity and usability, enabling efficient iterative refinement for both novices and experts. 3.2 Interactive Query Refinement Extending the foundation established by the NLE framework (Section 3.1.2 ), we employ an interactive query refinement process that aligns SPARQL queries with the user’s original question. This is step 4 in Fig. 2 . While the system can readily incorporate direct user feedback, we also offer a self-refinement mode in which an LLM simulates user suggestions for automated evaluation. In this section, we outline the key steps of the refinement loop, explain how the NLE underpins each iteration, and highlight a tool-based entity/property search mechanism that reduces the domain knowledge burden for query authors. Our current prototype implementation features these tools for popular knowledge graphs like Wikidata and DBpedia, but the same methodology can be adapted to other semantic datasets with minimal modification. 3.2.1 Motivation for Tool-based Entity and Property Search When writing or refining SPARQL queries, it is often necessary to reference exact entity and property URIs Users or LLMs may not recall these IRIs offhand, leading to guesswork and errors. To address this challenge, we incorporate dedicated search tools that the LLM can invoke on demand. These functions query the target knowledge graph’s API or index to identify proper IRIs for entities (e.g., “Rowen Atkinson”) or properties (e.g., “start date”). By delegating entity/property linking to a well-defined utility, the iterative refinement loop becomes more convenient and robust, relieving users and the LLM of low-level domain details. 1 Input : U : Natural Language Question (i.e. user’s intent) \mathcal{Q} : Initial SPARQL query K : Target knowledge graph N : Maximum refinement iterations Output : \mathcal{Q}^{\ast} : Refined SPARQL query aligned with user intent 2 3 i\leftarrow 0 4 while i