Accurate SPARQL generation via in-context learning and schema-based query construction
Open Access
- 8 April 2026
- journal article
- Published by Oxford University Press (OUP) in Bioinformatics
- Vol. 42 (5)
Abstract
Motivation: Integrated analysis across biological databases is becoming increasingly important in life science research, leading many public databases to adopt Semantic Web technologies, also known as knowledge graphs. However, biological data possesses inherently complex and diverse structures, which makes the resulting Resource Description Framework (RDF) schemas intricate and difficult for non-expert users to master, preventing them from translating natural language questions into correct SPARQL queries. Although recent large language model (LLM)-based approaches show potential for automatic SPARQL query generation, they often suffer from structural hallucinations and require large-scale training data to capture schema-specific structures. In this study, we propose a novel framework that avoids hallucinations and requires no training data by combining LLM-based word extraction with a schema-based SPARQL query builder. Results: The LLM extracts variables and parameters from the user’s question based on a predefined schema, and the query builder generates a syntactically correct SPARQL query accordingly. By providing a predefined schema in prompts, our method eliminates the need for training data. Experimental results on UniProt, Rhea, and Bgee demonstrate that our method outperforms baseline LLM-based methods using fine-tuning and prompt-tuning in terms of the similarity between search results obtained from generated and expert-written queries. Furthermore, we developed a proof-of-concept chatbot system that enables users to query RDF databases via natural language input, demonstrating the practical utility of our approach in improving access to biological data resources. Availability and implementation: Experimental environment: https://github.com/scott2121/sparql_query_generator (DOI: https://doi.org/10.5281/zenodo.18539213). Chatbot: https://github.com/scott2121/sparql_query_chatbot (DOI: https://doi.org/10.5281/zenodo.18539225).Keywords
This publication has 11 references indexed in Scilit:
- Enhancing SPARQL query generation for question answering with a hybrid encoder–decoder and cross-attention modelJournal of Web Semantics, 2025
- HunFlair2 in a cross-corpus evaluation of biomedical named entity recognition and normalization toolsBioinformatics, 2024
- Bio-SODA UX: enabling natural language question answering over knowledge graphs with user disambiguationDistributed and Parallel Databases, 2022
- SGPT: A Generative Approach for SPARQL Query Generation From Natural Language QuestionsIEEE Access, 2022
- Rhea, the reaction knowledgebase in 2022Nucleic Acids Research, 2021
- Deep learning methods for biomedical named entity recognition: a survey and qualitative comparisonBriefings in Bioinformatics, 2021
- The Bgee suite: integrated curated expression atlas and comparative transcriptomics in animalsNucleic Acids Research, 2020
- NBDC RDF portal: a comprehensive repository for semantic data in life sciencesDatabase: The Journal of Biological Databases and Curation, 2018
- Protein Data Bank Japan (PDBj): maintaining a structural data archive and resource description framework formatNucleic Acids Research, 2011
- Infrastructure for the life sciences: design and implementation of the UniProt websiteBMC Bioinformatics, 2009