feat: Add wiki entity linking and QID lookup methods - #107
Open
th0rntwig wants to merge 2 commits into
Open
Conversation
th0rntwig
force-pushed
the
feat/entity-linking
branch
from
September 3, 2026 11:50
bd3d702 to
ac745f6
Compare
th0rntwig
force-pushed
the
feat/entity-linking
branch
from
September 3, 2026 11:58
ac745f6 to
6f6e6be
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds Wikidata entity linking and QID lookup to the wiki API, plus a batch variant of wiki search. Six methods on both
WikiAPIandAsyncWikiAPI.Entity linking resolves a name as it appears in text to a canonical Wikidata QID — "Apple" to
Q312rather than the fruit — so you can ground a mention before reasoning about it. QID lookup then fetches that entity's metadata directly.New methods
search_wiki_batch(queries, ...)POST /v1/wiki/search/batchlink_entity(entity, ...)GET /v1/wiki/link-entitylink_entity_batch(entities, ...)POST /v1/wiki/link-entity/batchget_entity(qid)GET /v1/wiki/entity/{qid}get_entity_batch(qids)POST /v1/wiki/entity/batchThe batch forms embed all inputs in one request and run in parallel, so they are considerably cheaper than the equivalent sequence of single calls.
Behavior worth knowing
link_statusbefore trustinglinked_entity. Linking abstains rather than guessing:linkedis a confident match,ambiguousmeans a different entity of the same name scored withinambiguity_margin, andno_matchmeans nothing clearedrelevance_threshold.linked_entityisNoneforno_match, and forambiguouswhenallow_ambiguous=False.get_entityreturnsfound=Falsewith a null entity rather than raising, because callers batch QIDs and need per-QID outcomes. Batch results preserve input order and include misses.entity_types/entity_descriptionsalign positionally with entities inlink_entity_batchand must match in length; useNonefor individual entries you don't have./in a QID would otherwise retarget a different endpoint.WikidataMetadataEntity metadata is a typed core (
wikipedia_titles,sitelink_count,aliases, …) plus every Wikidata property the API extracts — 227 of them, each typed. The property fields fall into a handful of shapes, modelled asWikidataQidRef,WikidataQuantity,WikidataMonolingualTextandWikidataCoordinate.Almost every property is absent on any given entity — a person has no
capital, a city nodate_of_birth— so expectNoneand read defensively. Each field carries its Wikidata property id as a trailing comment, for lookup atwikidata.org/wiki/Property:<id>.QID-valued fields list every claim, not just the current one.
ceoon a company carries former officeholders alongside the incumbent:Each entry carries
qidandlabelplus whichever ofstart_time,end_time,point_in_timeandrankthat claim has. Most claims carry no temporal qualifier at all, so those attributes are commonlyNone.Deciding which entry is current is the caller's judgement from those fields. Wikidata does not reliably mark it:
rankis the authoritative signal when an editor has set it, but it is frequently left atnormalfor every claim, and a missingend_timedoes not imply currency — an announced successor who has not taken office yet also has none.Times are ISO-ish strings; quantities are
WikidataQuantity(amount, unit_qid, unit_label, …). Readunit_qid/unit_labelbefore comparing quantities across entities — revenue in EUR and USD would otherwise be silently mixed. Forsocial_media_followersthere is one claim per account per snapshot, so checkplatform,account_idandpoint_in_timebefore summing.A few fields are list-shaped but currently return a single entry (
population,total_revenue,launch_date, …). They are marked[one entry]inline in the model. The list shape is held open so restoring the full history later is not a breaking change — don't read their length as a count.The model keeps
extra="allow", so a property added server-side still reaches you viamodel_extrabefore this SDK is updated to name it. The nested value models allow extras too: the API attaches per-property context keys to entries (for_work_qidon awards,character_role_qidon cast members, and others), and new ones can appear without warning.Changes to existing API
search_wikigainsinclude_main_sectionandhas_wikidata. No existing parameter or default changed, so current callers are unaffected.Tests
tests/test_entity_linking.py— 45 tests; 135 pass across the suite. Covers request shapes and query serialization, QID path escaping, DTO validation and per-entity ordering, the typed property shapes, and that unknown properties and unknown qualifier keys still reach the caller.