Query Dataset With Sparql
Run a read-only SPARQL query over a dataset’s knowledge graph.
The dataset graph is read in full and loaded into an in-memory RDF store
for the duration of the request (the same view cognee.modules.graph.rdf
exports), then the query runs there. Nodes carrying an ontology_uri keep
that IRI; other nodes get {base_iri}node/{id} and cognee-native edges
{base_iri}prop/{relationship_name}; name becomes rdfs:label and
is_a becomes rdf:type / rdfs:subClassOf.
Path Parameters
- dataset_id (UUID): The unique identifier of the dataset
Request Body
- query (str): SELECT, ASK, CONSTRUCT or DESCRIBE. SPARQL Update
(INSERT, DELETE, LOAD, CLEAR, DROP, CREATE, ADD, MOVE, COPY) is refused, and
so are
SERVICE,FROMandFROM NAMED: the query reads this dataset’s graph only. - base_iri (str, optional): prefix for minted IRIs
- limit (int, 1-10000, default 1000): cap on SELECT rows
Response
Depends on the query form:
- SELECT:
variables,rows,row_count,truncated - ASK:
boolean - CONSTRUCT / DESCRIBE:
turtle
Limits
- Graphs with more than
SPARQL_MAX_GRAPH_NODESnodes (default 200000) are refused with 413; export the graph as RDF instead. - Queries that run longer than
SPARQL_QUERY_TIMEOUT_SECONDS(default 30) are abandoned with 504. The server stops waiting; the worker thread finishes on its own.
Error Codes
- 400 Bad Request: SPARQL Update keywords,
SERVICE/FROM/FROM NAMED, or the query does not parse - 404 Not Found: Dataset doesn’t exist or user doesn’t have access
- 413 Content Too Large: Dataset graph exceeds the node ceiling
- 504 Gateway Timeout: Query exceeded the wall-clock budget
Authorizations
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
Path Parameters
Dataset UUID, the id field from GET /api/v1/datasets (not the name)
"b8a7c3de-4f5a-4b6c-8d9e-0f1a2b3c4d5e"
Body
A read-only SPARQL 1.1 query: SELECT, ASK, CONSTRUCT or DESCRIBE.
1"SELECT ?s ?name WHERE { ?s rdfs:label ?name } LIMIT 10"
Prefix for the IRIs minted for nodes without an ontology_uri and for cognee-native predicates. Defaults to https://cognee.ai/graph/.
"https://example.org/memory/"
Maximum number of SELECT rows to return; sets truncated when hit.
1 <= x <= 10000Response
Successful Response
One query form's result; the fields for the other forms are omitted.
Keys are deliberately snake_case (not the API's usual camelCase): they mirror the SPARQL JSON results vocabulary callers already parse.
SELECT: projected names
SELECT: one list per solution, aligned with variables. IRIs and literal lexical forms as strings, blank nodes as _:id, unbound as null.
SELECT: len(rows)
SELECT: true when more rows existed than limit
ASK: the answer
CONSTRUCT / DESCRIBE: the result graph as Turtle