Skip to content

bibliofabric.wikidata

The Wikidata client (bibliofabric.wikidata) is a Python client for Wikidata. It covers Wikidata's three read interfaces: the MediaWiki Action API (wbgetentities, wbsearchentities, wbgetclaims), the Wikibase REST API (items, properties, statements, per-language term routes), and the SPARQL query service. It types every payload with Pydantic models, batches wbgetentities id requests at the API's 50-id limit, and ships an async client plus a generated sync twin with the same API. The API needs no credentials, but all three services ask for a descriptive User-Agent with contact information, which the client builds for you.

Install

uv add bibliofabric

Requires Python 3.12 or newer.

The Wikidata client ships in bibliofabric 0.9.0 and newer.

Quick start

import asyncio
from bibliofabric.wikidata import WikidataClient


async def main():
    async with WikidataClient() as client:
        entity = await client.entities.get("Q42")
        print(entity.description(), entity.sitelinks["enwiki"].title)

        batch = await client.entities.batch_get(["Q42", "P31", "L1"])
        print(sorted(batch))

        page = await client.search.search(query="douglas adams")
        print([hit.id for hit in page.results])

        result = await client.sparql.query(
            "SELECT ?item WHERE { ?item wdt:P31 wd:Q5 } LIMIT 5"
        )
        print(result.rows())


asyncio.run(main())

client.entities exposes get(), batch_get(), and iterate_ids(). client.search exposes search(), iterate(), collect(), and the REST routes search_items() and search_properties(). client.claims, client.sitelinks, client.items, client.properties, and client.statements cover the Action API and REST read routes. client.sparql talks to the query service on query.wikidata.org.

Sync usage

WikidataSyncClient is generated from the async source and behaves the same. The sync client uses plain httpx.Client and never starts or touches an event loop, so it works in scripts, in Jupyter, in threads, and when called from inside a running event loop.

from bibliofabric.wikidata import WikidataSyncClient

with WikidataSyncClient() as client:
    entity = client.entities.get("Q42")
    print(entity.claims["P31"][0].mainsnak.datavalue)

Settings

WikidataSettings loads from environment variables prefixed with WIKIDATA_ and from a .env file in the working directory. The defaults follow the pace Wikidata's own guides ask for: serial requests at 2 per second against www.wikidata.org, and one SPARQL query every 2 seconds in the separate sparql rate class. WIKIDATA_USER_AGENT and WIKIDATA_MAILTO feed the User-Agent; WIKIDATA_TIMEOUT, WIKIDATA_MAX_RETRIES, and the other shared fields of bibliofabric.config.BaseApiSettings also apply.

Examples

Runnable scripts live in packages/bibliofabric/examples/wikidata/: a quickstart (entity get, search, bounded iteration, one SPARQL query), external-id lookup by DOI and ORCID, and a sync entity example.

Where to go next

  • Entities covers wbgetentities, batching, id iteration, and the Special:EntityData routes.
  • Search covers wbsearchentities, the REST search routes, and the REST suggest routes.
  • Claims and statements covers wbgetclaims and the REST statement routes.
  • Site links covers the site link resources.
  • Lexemes covers the WikibaseLexeme REST read routes.
  • SPARQL covers the query service and how the two hosts share one rate limiter.
  • Utility modules covers badges, HTML formatting, value parsing, property suggestions, and constraint checks.
  • Upstream quirks lists what the live services do differently from their documentation.
  • The API reference is generated from the source.