From e4d9c53d7bd1108b1598e1bb1c9da5e4a40e24e9 Mon Sep 17 00:00:00 2001 From: forhim007 Date: Thu, 7 May 2026 21:08:45 +0800 Subject: [PATCH] fix: guard against IndexError on empty query string in SPARQL generator When query.split() returns an empty list (which can happen if the query is empty or whitespace-only), indexing with [0] raises an IndexError. Extract the split result first and guard against an empty list before accessing the first element. Fixes #870 --- trustgraph-flow/trustgraph/query/ontology/sparql_generator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py b/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py index 44c7e0a1..417f0d49 100644 --- a/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py +++ b/trustgraph-flow/trustgraph/query/ontology/sparql_generator.py @@ -203,10 +203,12 @@ ASK {{ if response and isinstance(response, dict): query = response.get('query', '').strip() if query.upper().startswith(('SELECT', 'ASK', 'CONSTRUCT', 'DESCRIBE')): + parts = query.split() + query_type = parts[0].upper() if parts else 'SELECT' return SPARQLQuery( query=query, variables=self._extract_variables(query), - query_type=query.split()[0].upper(), + query_type=query_type, explanation=response.get('explanation', 'Generated by LLM'), complexity_score=self._calculate_complexity(query) )