Skip to content

fix(ontology): guard against empty query string in SPARQL generator#880

Closed
SAY-5 wants to merge 1 commit into
trustgraph-ai:masterfrom
SAY-5:fix/sparql-empty-query-index-error
Closed

fix(ontology): guard against empty query string in SPARQL generator#880
SAY-5 wants to merge 1 commit into
trustgraph-ai:masterfrom
SAY-5:fix/sparql-empty-query-index-error

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 7, 2026

Copy link
Copy Markdown

Closes #870.

_generate_with_llm in trustgraph-flow/trustgraph/query/ontology/sparql_generator.py did:

query = response.get('query', '').strip()
if query.upper().startswith(('SELECT', 'ASK', 'CONSTRUCT', 'DESCRIBE')):
    return SPARQLQuery(
        ...
        query_type=query.split()[0].upper(),
        ...
    )

If the startswith guard is ever weakened during refactoring (the issue's primary concern) and the LLM returns an empty / whitespace-only query, query.split()[0] raises IndexError. The fix computes parts = query.split() once, returns None if empty, and reuses parts[0] for the query_type read.

Tests

tests/unit/test_query/test_sparql_generator.py adds three async cases:

Test Covers
test_valid_select_response_returns_query Sanity: well-formed response still yields a SPARQLQuery with query_type='SELECT'.
test_empty_query_does_not_raise_index_error Empty query field returns None instead of crashing.
test_whitespace_only_query_does_not_raise_index_error Whitespace-only query returns None after .strip().

The suite uses pytest.importorskip on trustgraph.query.ontology.sparql_generator so it skips cleanly in environments where the package's existing import chain cannot resolve.

Verification

  • python -m py_compile on both edited files: clean.
  • pytest tests/unit/test_query locally: 340 passed, 3 skipped (the new tests, due to a pre-existing relative-import issue in query_service.py outside the deployed FlowProcessor environment).

Closes trustgraph-ai#870.  query.split()[0] in _generate_with_llm raised IndexError
when the LLM response 'query' field passed the keyword-startswith guard
but split to an empty list (e.g. if the guard is ever weakened during
refactoring).  Compute parts once, return None when empty, and reuse
parts[0] for the query_type read.

Signed-off-by: SAY-5 <saiasish.cnp@gmail.com>
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown

Contributor License Agreement ❌

This PR contains commits from author(s) with no linked GitHub account. All commits must be authored by a GitHub user so the CLA can be verified.

Please ensure the git author email is associated with a GitHub account, or rewrite the commits with a linked identity.

@cybermaggedon

Copy link
Copy Markdown
Contributor

Hey @SAY-5 this tripped over the CLA check. The email used in git commits isn't linked to a Github account. Once that's fixed you'll be prompted to send a message here which would indicate acceptance of the CLA.

@cybermaggedon cybermaggedon self-requested a review May 7, 2026 09:56
@cybermaggedon cybermaggedon self-assigned this May 7, 2026
@cybermaggedon

Copy link
Copy Markdown
Contributor

Other than that, great change, thanks

@SAY-5

SAY-5 commented May 7, 2026

Copy link
Copy Markdown
Author

Thanks @cybermaggedon - I'll fix the commit author email and re-push so the CLA check passes.

@cybermaggedon

Copy link
Copy Markdown
Contributor

Closed as not able to complete the CLA currently. Thanks for your interest in the project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexError on empty query string in SPARQL generator

2 participants