# SparqLLM: Retrieval-Augmented SPARQL Query Processing

Pascal Molli, Hala Skaf-Molli, Sébastien Ferré, Alban Gaignard, Peggy Cellier

► To cite this version:

Pascal Molli, Hala Skaf-Molli, Sébastien Ferré, Alban Gaignard, Peggy Cellier. SparqLLM: Retrieval-Augmented SPARQL Query Processing. ESWC 2025 - 22nd European Semantic Web Conference, Jun 2025, Portoroz, Slovenia. pp.1-5. [hal-05138658](https://hal.science/hal-05138658v1)

HAL Id: hal-05138658

https://hal.science/hal-05138658v1

Submitted on 1 Jul 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>
# SparqLLM : Retrieval-Augmented SPARQL
## Query Processing

Pascal Molli¹, Hala Skaf-Molli¹, Sebastien Ferré², Alban Gaignard³, and Peggy Cellier²

¹ Nantes Université - LS2N

² University of Rennes

³ Nantes Université, CNRS, INSERM

**Abstract.** SPARQL is essential for querying Knowledge Graphs (KGs), but much information exists in external sources rather than within KGs. To address this, we propose SparqLLM, a retrieval-augmented query processing approach that leverages user-defined functions (UDFs) and named graphs to augment SPARQL queries with diverse external sources, including search engines, large language models (LLMs), and vector search. By doing so, SparqLLM significantly enhances SPARQL's capabilities, enabling a single query to access multiple heterogeneous sources while ensuring query provenance and explainability. This demonstration highlights the potential of SparqLLM to enrich query results with comprehensive, up-to-date information and showcases its application in a real-world use case.

**Keywords:** SPARQL · Search Engines · Large Language Models.

## 1 Introduction

Suppose you want to discover cultural events that are available in European capitals. A SPARQL query on Wikidata can retrieve the capitals of Europe, but ongoing cultural events are missing. Such events are often found on external web pages accessible through search engines or Large Language Models (LLMs). The challenge is to enable Retrieval-Augmented query processing across these external sources, including search engines, LLMs, and vector databases [2].

Several techniques have been proposed to allow SPARQL queries to access external sources of knowledge during query processing. These approaches often rely on SPARQL service clauses [1] or language extensions [3]. However, to the best of our knowledge, no existing technique enables a single SPARQL query to simultaneously access search engines, vector search, and LLMs.

In SparqLLM, we propose a new retrieval-augmentation technique for SPARQL. The key idea is to have User-Defined Functions (UDFs) to dynamically generate named graphs by retrieving relevant information from external sources. Unlike [3], our approach does not require modifying the SPARQL grammar. Compared to custom services [1], our technique presents several advantages: (1) UDFS facilitate easy parameter passing, (2) provenance is tracked via named graphs, (3) the
<figure><img src="image_2.png" /><figcaption>Fig. 1. SparqLLM SPARQL Query</figcaption></figure>

BIND clause creates a natural pipeline in query execution, (4) query execution produces results and generates named graphs extending the original Knowledge Graph. The query of Figure 1 retrieves the capitals of Europe and uses a search engine to find web pages that may contain cinema events in these cities. It then extracts cultural events from these pages using an LLM.

For each binding of ?capital, the UDF (SLM-SEARCH) dynamically generates a new named graph, bound to segraph, containing triples where the ?capital bindings are linked to related ?uri values as objects. The UDF SLM-BS4 retrieves these URIs and extracts the text, which is then used to build a prompt for an LLM, represented as the UDF SLM-LLM. The LLM produces an event representation<fnref n="4" />, which is injected into a newly created named graph ?g. As a result, executing this query produces results and a collection of newly generated named graphs representing all search and LLM extraction performed during query execution.

The query in Figure 1 illustrates a pipeline that first retrieves data from Wikidata, then finds relevant documents via a search engine, and finally ex-

<footnote n="4">⁴ Prompting techniques to generate triples have been detailed in [4].</footnote>
tracts facts from these documents using an LLM. In this scenario, the LLM is
not used as a knowledge base but as a text extractor. This approach enhances
explainability, as the provenance of extracted facts can be verified.

## 2 SparqLLM

As illustrated in Figure 1, SparqLLM relies on UDFs to integrate external sources into SPARQL queries. While UDFs are traditionally used to extend SPARQL, the novelty of SparqLLM lies in utilizing named graphs as outputs of these UDFs. According to the SPARQL language specification ^5, creating or updating named graphs during query execution is not permitted, primarily to ensure query termination and snapshot isolation. To address this, we establish the following rules:

1. *Scoped Named Graphs*: Named graphs are created within the scope of the query, with each query managing its own initially empty dataset.

2. *Immutability and Uniqueness*: UDFs that produce graphs must ensure their immutability and uniqueness, meaning that a generated graph must not already exist and cannot be updated during query execution.

3. *Pipeline Execution with BIND*: UDFs generating graphs are combined with `BIND` clauses. According to the SPARQL 1.1 specification, a variable introduced by a `BIND` clause must not have been previously used in the group graph pattern before its occurrence in `BIND`. This restriction prevents cyclic dependencies in bound variables and ensures a pipelined execution using lazy joins. Consequently, graphs cannot be accessed until they are bound. For instance, in Figure 1, the query engine cannot evaluate the `GRAPH ?g` clause before `SLM-LLM` instantiates ?g.

Following these rules makes designing a UDF to generate a new graph systematic and straightforward. For instance, the `SLM-LLMbehaviour` function operates as follows:

1. Create a named graph in the query dataset by hashing the prompt and URI. If it already exists, return it.

2. Query the LLM using *prompt*, parse the results as JSON-LD, and populate the named graph with extracted JSON-LD facts.

3. Link root nodes of JSON-LD with *uri* to associate the extracted facts with the original query bindings.

4. Return the name of the newly created named graph.

When executing the query of Figure 1, for one capital, **SLM-SEARCH** creates a graph with five URIs. For each URI, **SLM-LLM** creates a graph describing an event. All these graphs are stored as a new dataset, effectively augmenting the original Wikidata Knowledge Graph.

Please note that the query evaluation is no longer deterministic when using LLM functions in a SPARQL query.

<footnote>^5 https://www.w3.org/TR/sparql11-query/</footnote>
<figure><img src="image_3.png" /><figcaption>Fig. 2. Profiles of ALMA and ATAL Tracks based on the Knowledge Units of CS2023.</figcaption></figure>

## 3 SparqLLM Implementation

SparqLLM is built on top of RDFLib<sup>6</sup>. The demonstration code is available at https://github.com/GDD-Nantes/SPARQLLM. The repository includes basic examples, including the one illustrated in Figure 1.

SLM functions are mapped to Python functions using a simple configuration file such as:

```
;search default WHoosh
```

```
SLM-SEARCH-FAISS = SPARQLLM.udf.search faiss.search faiss
```

```
SLM-SEARCH-WHOOSH = SPARQLLM.udf.search whoosh.searchWhoosh
```

```
;llm default OLLAMA
```

```
SLM-LLMGRAPH = SPARQLLM.udf.llmgraph ollama.LLMGRAPH OLLAMA
```

```
SLM-LLMGRAPH OLLA = SPARQLLM.udf.llmgraph ollama.LLMGRAPH OLLAMA
```

```
SLM-LLMGRAPH_MISTRAL = SPARQLLM.udf.llmgraph mistral.llm graph mistral
```

```
SLM-LLMGRAPH_OPENAI = SPARQLLM.udf.llmgraph openai.llm graph openai
```

This makes it easy to add a new function, change the implementation of a UDF without modifying the query, or use two different LLM implementations within the same SPARQL query.

## 4 Live demonstration

During the session, we apply SparqLLM in a real use case to align the knowledge graph of a master's program in Computer Science with the ACM/AAAI/IEEE

<footnote n="6"><sup>6</sup> https://github.com/RDFLib/rdflib</footnote>
recommendations for Computer Science Education (CS2023) 7. CS2023 is a 459-page PDF document detailing knowledge units and knowledge areas in computer science. We extracted knowledge units of CS2023 as text files and index them in a vector database.

We crafted a dedicated SparqLLM query that implements the following pipeline: 1) Read the KG of the master's program. 2) For each course, retrieve a list of knowledge unit candidates using a vector database. 3) For each candidate, verify whether the course description covers the knowledge unit.

Together, we examine which named graphs are created and what they contain, and assess the accuracy and relevance of both vector database search and LLM validation quality. Finally, we generate interactive radar views as presented in figure 2. It allows for profiling the master's program tracks and identifying potential gaps and biases. A live demonstration video is available at https://youtu.be/Oob2ci2TsGE. The code to reproduce the live demonstration is available at https://github.com/GDD-Nantes/SLM-EXP. We believe this use case can benefit any Computer Science curriculum by providing a structured approach to curriculum evaluation and alignment with international standards.

# 5 Conclusion

SparqLLM introduces a novel approach for enabling Retrieval-Augmented SPARQL query processing. Its design ensures simplicity, allowing for the seamless integration of new functions and thereby enhancing the flexibility and capabilities of SPARQL queries. A key challenge lies in improving control over UDF LLM functions to constrain the LLM outputs to conform to a predefined schema.

# Acknowledgments

This work is supported by the French ANR project MeKaNo (Search the Web with Things) ANR-22-CE23-0021, and the French Labex CominLabs projects MiKroloG (The Microdata Knowledge Graph) and WanderLog (Exploring Large Knowledge Graphs With Sampling). Special thanks to Master 1 students of Nantes University: Denez Guery, Corentin Guillemot, Malo Le-Gallic, Wiame Taii and Mohammed Ouedrhiri.

# References

1. Asprino, L., Daga, E., Gangemi, A., Mulholland, P.: Knowledge graph construction with a *Façade*: A unified method to access heterogeneous data sources on the web. ACM Trans. Internet Techn. 23(1), 6:1–6:31 (2023)

2. Hogan, A., Dong, X.L., Vrandečić, D., Weikum, G.: Large language models, knowledge graphs and search engines: A crossroads for answering users’ questions. arXiv preprint arXiv:2501.06699 (2025)

<footnote n="7">7 https://csed.acm.org/</footnote>
6

3. Lefrançois, M., Zimmermann, A., Bakerally, N.: Flexible RDF generation from RDF and heterogeneous data sources with sparql-generate. In: EKAW- Satellite Events. LNCS, vol. 10180 (2016)

4. Mihindukulasooriya, N., Tiwari, S., Enguix, C.F., Lata, K.: Text2kgbench: A benchmark for ontology-driven knowledge graph generation from text. In: 22nd International Semantic Web Conference. LNCS, vol. 14266 (2023)