Hermes

hermes.validate

Validation

prepare_draft extracts a query and moves solution modifiers out of WHERE. parse_error checks it with RDFLib. One repair call is allowed.

Extract

extract_sparql prefers the first fenced block, including a sparql fence. Without a fence, it uses the whole reply. It then keeps the first span that starts at SELECT, ASK, CONSTRUCT, or DESCRIBE, including any PREFIX lines immediately before it. If that span is missing, the whole text is returned. An empty result raises ValueError from prepare_draft.

Lift modifiers

Models often write ORDER BY, LIMIT, GROUP BY, HAVING, or OFFSET inside the outer WHERE block. lift_modifiers moves those lines to after the block when they sit at brace depth zero inside it.

SELECT ?city WHERE {
  ?s a ex:City .
  ORDER BY ?city
  LIMIT 5
}

becomes a query with the triple inside WHERE and ORDER BY ?city then LIMIT 5 after the closing brace. The same modifiers inside a nested SELECT stay where they are, because that depth is not zero. A query with no WHERE, or with unbalanced braces, is returned unchanged.

Parse and repair

parse_error calls rdflib.plugins.sparql.prepareQuery. It returns None when the query parses, and the exception text otherwise.

On an error, generate sends repair_prompt: the parser message, the bad query, and instructions to return only the corrected query, keep SELECT out of FILTER, put GROUP BY, ORDER BY, and LIMIT after WHERE, and write aggregates as (MAX(?value) AS ?maxValue). The repair reply goes through prepare_draft and apply_stages again. A second parse error raises ValueError. There is no third call.

Source: validate.py.