SPARQL
client.sparql talks to the SPARQL query service at https://query.wikidata.org/sparql. The service is a second host, so it runs on its own client instance behind the resource; the main client paces both hosts through one shared rate limiter. The examples on this page use the client from the first block; later
blocks reuse it.
import asyncio
from bibliofabric.wikidata import WikidataClient
client = WikidataClient()
Run a SELECT query
result = await client.sparql.query(
"SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q146 . "
"SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. } } LIMIT 3"
)
print("vars:", result.head.vars)
for row in result.rows():
print(row["itemLabel"], row["item"])
vars: ['item', 'itemLabel']
Q378619 http://www.wikidata.org/entity/Q378619
Muezza http://www.wikidata.org/entity/Q498787
Orangey http://www.wikidata.org/entity/Q677525
query() sends GET with format=json and returns a typed SparqlResult, the SPARQL 1.1 JSON result. rows() returns plain {variable: value} strings. The raw bindings stay available as result.results.bindings, where each value is a SparqlBindingValue with type, value, xml_lang (the wire's xml:lang), and datatype.
Run an ASK query
An ASK answer carries boolean instead of bindings, and rows() returns [] for it.
result = await client.sparql.query("ASK { wd:Q42 wdt:P31 wd:Q5 . }")
print(result.boolean)
True
Send long queries as POST
query_post() sends a read-only POST, so URL length limits do not apply. It passes retry=True, so the framework retries transient failures.
result = await client.sparql.query_post("SELECT ?p WHERE { wd:Q42 ?p ?o . } LIMIT 3")
print("rows:", len(result.rows()))
rows: 3
Read other formats
query_text() passes the raw answer body through as text. The service selects the representation from the Accept header; it ignores a non-JSON format query parameter. The accepted formats are json, xml, csv, tsv, and turtle.
body = await client.sparql.query_text(
"SELECT ?item WHERE { ?item wdt:P31 wd:Q146 . } LIMIT 2", output_format="csv"
)
print(body.splitlines()[0])
print(body.splitlines()[1])
item
http://www.wikidata.org/entity/Q378619
One pace for two hosts
WikidataClient owns one rate limiter built from WikidataSettings. client.sparql builds a _WikidataQueryClient for query.wikidata.org that shares the settings object and the limiter with the main client. Requests are paced by rate class inside that shared limiter: Action API and REST requests use the default class (2 requests per second by default), SPARQL requests use the sparql class (one query every 2 seconds by default), and one concurrency bound covers both hosts. The two clients together therefore never exceed the pace the settings describe.
Setting max_requests_per_second without also setting rate_limits clears the per-class map, so the uniform pace applies to the SPARQL host too. Pass rate_limits={"sparql": 0.5} to keep the separate SPARQL pace.
Errors
Malformed or overlong queries answer with HTTP 400 and a plain-text Java stack trace; the framework raises APIError with that text attached as the body. Under load the service answers HTTP 429, which maps to RateLimitError with the Retry-After honored. The service cancels queries that run longer than its timeout, and it asks readers to run one query at a time, which the sparql rate class does by default.