| name | few-shot-examples |
| description | Few-shot examples skill that provides reusable examples of common Wikidata SPARQL patterns. Use before generating SPARQL to find relevant examples that demonstrate how similar questions have been answered, covering patterns like direct lookups, reverse relations, subclass traversal, qualifiers, and aggregations. |
| compatibility | Requires Python 3, uv. No external API access needed (examples are stored locally as JSON). |
Few-Shot Examples
Use this skill to retrieve relevant Text-to-SPARQL examples that demonstrate common Wikidata query patterns. The examples are selected based on structural similarity to the current question, helping guide the SPARQL generation step.
Files
examples/patterns.json: collection of curated examples organized by query pattern
scripts/retrieve_examples.py: retrieves similar gold-standard examples from ChromaDB
scripts/build_examples_db.py: builds the ChromaDB collection (run once during setup)
data/examples_db/: ChromaDB persistent storage with 417 indexed gold examples
Dynamic Example Retrieval (Recommended)
Always use this before generating SPARQL. Retrieves the most similar gold-standard examples from 417 indexed train/dev questions with their correct SPARQL queries.
# Basic retrieval (top 5 similar examples)
uv run python .agents/skills/few-shot-examples/scripts/retrieve_examples.py \
--query "How far is Alpha Centauri from Earth?" \
--top-k 5
Output: JSON with similar questions and their gold SPARQL, showing correct patterns to follow.
Key Usage Patterns
# For measurement/distance questions - look at how psn: is used
uv run python .agents/skills/few-shot-examples/scripts/retrieve_examples.py \
--query "What is the area of Texas?" --top-k 3
# For superlative questions - look at ORDER BY + LIMIT patterns
uv run python .agents/skills/few-shot-examples/scripts/retrieve_examples.py \
--query "Which country has the most official languages?" --top-k 3
# For boolean questions - look at ASK patterns
uv run python .agents/skills/few-shot-examples/scripts/retrieve_examples.py \
--query "Is Turkey located on the Dead Sea?" --top-k 3
# For temporal/current-state questions
uv run python .agents/skills/few-shot-examples/scripts/retrieve_examples.py \
--query "Does Elon Musk have a wife?" --top-k 3
When to Use Dynamic Retrieval
Use few-shot retrieval conditionally — only for complex patterns or after failures. Do NOT retrieve examples for simple questions you can already answer confidently.
RETRIEVE examples for:
- Questions involving physical measurements, distances, areas, durations (need
psn: patterns)
- Superlative questions with complex aggregation ("tallest", "most", requiring ORDER BY + subclass traversal)
- Questions about current state vs. historical ("is X married NOW?", "highest Elo EVER")
- Complex qualifier patterns (ordinals, contest winners, temporal scoping)
- After a query failure — this is the best time to retrieve examples for repair guidance
DO NOT retrieve examples for:
- Simple entity property lookups ("What is the capital of X?", "Who wrote Y?")
- Direct boolean checks ("Is X alive?", "Does X have a president?")
- Straightforward P31/type queries where the pattern is obvious
- Questions where you already have high confidence in the SPARQL structure
Why conditional? Retrieving examples for simple questions can introduce unnecessary complexity — the agent may adopt complex patterns (subclass traversal, qualifiers) from retrieved examples when a simple wdt: lookup would be correct.
When To Use This Skill
Use this skill when:
- You are about to generate SPARQL and want to ground the generation with similar examples
- The question involves complex patterns (qualifiers, subclass traversal, aggregation)
- You need to see how a particular Wikidata modeling pattern is expressed in SPARQL
- A previous generation attempt failed and you want to see correct patterns
Important: Label Service Not Available
The custom SPARQL endpoint (wikikgqa.skynet.coypu.org) does NOT support SERVICE wikibase:label. Using it will cause HTTP 500 errors. Instead, use:
OPTIONAL { ?entity rdfs:label ?entityLabel FILTER(LANG(?entityLabel) = "en") }
Pattern Categories
0. Common Pitfall Patterns (Important)
These patterns address frequently incorrect queries:
"Who was X before Y?" (predecessor with non-consecutive terms):
# WRONG: Gets all predecessors if Y held position multiple times
SELECT ?pred WHERE { wd:Q_PERSON p:P39 ?stmt . ?stmt ps:P39 wd:Q_POSITION . ?stmt pq:P1365 ?pred . }
# CORRECT: Get predecessor of FIRST term only
SELECT ?pred WHERE {
wd:Q_PERSON p:P39 ?stmt .
?stmt ps:P39 wd:Q_POSITION ;
pq:P1365 ?pred ;
pq:P580 ?start .
} ORDER BY ?start LIMIT 1
"Which brands of type X are from country Y?" (brands vs products):
# WRONG: Brands are not typed as product instances
SELECT ?brand WHERE { ?brand wdt:P31 wd:Q_PRODUCT_CLASS ; wdt:P495 wd:Q_COUNTRY . }
# CORRECT: Find brands/companies that manufacture products of that type
SELECT ?brand WHERE {
?brand wdt:P31/wdt:P279* wd:Q431289 . # or use the brand/company class
?brand wdt:P176?/wdt:P17 wd:Q_COUNTRY .
}
# OR: Search for the correct brand class during exploration
"Films where X is solely the director" (exclusivity constraint):
# WRONG: Removes films where person appears in ANY property (too aggressive)
SELECT ?film WHERE { ?film wdt:P57 wd:Q_PERSON . MINUS { ?film ?p wd:Q_PERSON . FILTER(?p != wdt:P57) } }
# CORRECT: Exclude only competing credit roles (producer, writer)
SELECT ?film WHERE {
?film wdt:P31 wd:Q11424 ;
wdt:P57 wd:Q_PERSON .
FILTER NOT EXISTS { ?film wdt:P58 wd:Q_PERSON . } # not screenwriter
FILTER NOT EXISTS { ?film wdt:P162 wd:Q_PERSON . } # not producer
}
0b. Advanced Patterns (For Complex Questions)
Temporal overlap ("Were X and Y alive at the same time?"):
# Check if two people's lifespans overlap
ASK WHERE {
wd:Q254 wdt:P569 ?birthA ; wdt:P570 ?deathA . # Mozart
wd:Q47365 wdt:P569 ?birthB ; wdt:P570 ?deathB . # Marie Antoinette
FILTER(?birthA <= ?deathB && ?birthB <= ?deathA) # Overlap condition
}
Multi-hop familial chains ("great-grandfather of X", "grandfather of spouse of X"):
# Great-grandfather (3 hops via P22 = father)
SELECT ?greatgrandfather WHERE {
wd:Q_PERSON wdt:P22 ?father .
?father wdt:P22 ?grandfather .
?grandfather wdt:P22 ?greatgrandfather .
}
# Paternal grandfather of spouse
SELECT ?result WHERE {
wd:Q_PERSON wdt:P26 ?spouse .
?spouse wdt:P22 ?father .
?father wdt:P22 ?result .
}
Cross-class string matching ("entities in class A sharing family name with entities in class B"):
# Resistance fighters sharing family name with a painter
SELECT ?fighter ?name WHERE {
?fighter wdt:P106 wd:Q1397808 . # resistance fighter
?fighter wdt:P734 ?familyName .
?painter wdt:P106 wd:Q1028181 . # painter
?painter wdt:P734 ?familyName .
?familyName rdfs:label ?name FILTER(LANG(?name) = "en") .
FILTER(?fighter != ?painter)
}
GROUP BY on labels with HAVING ("entities with duplicate names"):
# Rivers (tributaries of Rhine) that share the same name
SELECT ?name (COUNT(?river) AS ?count) WHERE {
?river wdt:P403 wd:Q584 . # mouth of river = Rhine
?river rdfs:label ?name FILTER(LANG(?name) = "en") .
} GROUP BY ?name HAVING(COUNT(?river) > 1)
Temporal range filtering ("events between year X and year Y"):
# Meteorites impacting Earth between 1950 and 1970
SELECT ?meteorite WHERE {
?meteorite wdt:P31/wdt:P279* wd:Q60186 . # meteorite
?meteorite wdt:P585 ?date . # point in time (impact)
FILTER(YEAR(?date) >= 1950 && YEAR(?date) <= 1970)
}
1. Direct Property Lookup
Questions of the form "What is the X of Y?"
Example:
- Question: "What is the capital of France?"
- Search terms: France (item), capital (property)
- Resolved entities: France = Q142
- Resolved properties: capital = P36
- Exploration query:
SELECT ?val ?valLabel WHERE {
wd:Q142 wdt:P36 ?val .
OPTIONAL { ?val rdfs:label ?valLabel FILTER(LANG(?valLabel) = "en") }
} LIMIT 5
- Final SPARQL:
SELECT ?capital ?capitalLabel WHERE {
wd:Q142 wdt:P36 ?capital .
OPTIONAL { ?capital rdfs:label ?capitalLabel FILTER(LANG(?capitalLabel) = "en") }
}
- Expected result shape: Single row with city name
- Common failure mode: Using wrong property (P1376 "capital of" is the reverse direction)
2. Reverse Relations
Questions where the target entity is the object, not the subject.
Example:
- Question: "Which countries have Berlin as their capital?"
- Search terms: Berlin (item), capital (property)
- Resolved entities: Berlin = Q64
- Resolved properties: capital = P36
- Exploration query:
SELECT ?country ?countryLabel WHERE {
?country wdt:P36 wd:Q64 .
OPTIONAL { ?country rdfs:label ?countryLabel FILTER(LANG(?countryLabel) = "en") }
} LIMIT 10
- Final SPARQL:
SELECT ?country ?countryLabel WHERE {
?country wdt:P36 wd:Q64 .
OPTIONAL { ?country rdfs:label ?countryLabel FILTER(LANG(?countryLabel) = "en") }
}
- Expected result shape: One or more rows (countries with Berlin as capital)
- Common failure mode: Writing
wd:Q64 wdt:P36 ?country (wrong direction — P36 goes from country to capital)
3. Type Filtering (instance of)
Questions that filter entities by type.
Example:
- Question: "Which universities are located in Massachusetts?"
- Search terms: university (item for type), Massachusetts (item), located in (property)
- Resolved entities: university = Q3918396, Massachusetts = Q771
- Resolved properties: instance of = P31, located in the administrative territorial entity = P131
- Exploration query:
SELECT ?uni ?uniLabel WHERE {
?uni wdt:P31 wd:Q3918396 ;
wdt:P131 wd:Q771 .
OPTIONAL { ?uni rdfs:label ?uniLabel FILTER(LANG(?uniLabel) = "en") }
} LIMIT 10
- Final SPARQL:
SELECT ?university ?universityLabel WHERE {
?university wdt:P31 wd:Q3918396 ;
wdt:P131+ wd:Q771 .
OPTIONAL { ?university rdfs:label ?universityLabel FILTER(LANG(?universityLabel) = "en") }
}
- Expected result shape: Multiple rows of universities
- Common failure mode: Using P131 without
+ (transitive) — many universities are in cities within Massachusetts, not directly in Massachusetts
4. Subclass Traversal
Questions that need to include subclasses of a type.
Example:
- Question: "List all types of cancer"
- Search terms: cancer (item), subclass of (property)
- Resolved entities: cancer = Q12078
- Resolved properties: subclass of = P279
- Exploration query:
SELECT ?type ?typeLabel WHERE {
?type wdt:P279 wd:Q12078 .
OPTIONAL { ?type rdfs:label ?typeLabel FILTER(LANG(?typeLabel) = "en") }
} LIMIT 20
- Final SPARQL:
SELECT ?cancer ?cancerLabel ?description WHERE {
?cancer wdt:P279 wd:Q12078 .
OPTIONAL { ?cancer rdfs:label ?cancerLabel FILTER(LANG(?cancerLabel) = "en") }
OPTIONAL { ?cancer schema:description ?description . FILTER(LANG(?description) = "en") }
}
- Expected result shape: Many rows (subtypes of cancer)
- Common failure mode: Using P31 (instance of) instead of P279 (subclass of) — P31 gives specific diagnosed cases, P279 gives types/categories
5. Date Filters
Questions with temporal constraints.
Example:
- Question: "Which Nobel Prize winners in Physics were born after 1950?"
- Search terms: Nobel Prize in Physics (item), date of birth (property), award received (property)
- Resolved entities: Nobel Prize in Physics = Q38104
- Resolved properties: award received = P166, date of birth = P569
- Exploration query:
SELECT ?person ?personLabel ?dob WHERE {
?person wdt:P166 wd:Q38104 ;
wdt:P569 ?dob .
OPTIONAL { ?person rdfs:label ?personLabel FILTER(LANG(?personLabel) = "en") }
} LIMIT 10
- Final SPARQL:
SELECT ?laureate ?laureateLabel ?birthDate WHERE {
?laureate wdt:P166 wd:Q38104 ;
wdt:P569 ?birthDate .
FILTER(?birthDate >= "1950-01-01T00:00:00Z"^^xsd:dateTime)
OPTIONAL { ?laureate rdfs:label ?laureateLabel FILTER(LANG(?laureateLabel) = "en") }
}
ORDER BY ?birthDate
- Expected result shape: Multiple rows of people with birth dates after 1950
- Common failure mode: Using string comparison instead of dateTime typed comparison; or wrong date format
6. Aggregation and Counting
Questions asking for counts, averages, or other aggregates.
Example:
- Question: "How many films has Steven Spielberg directed?"
- Search terms: Steven Spielberg (item), director (property), film (item for type)
- Resolved entities: Steven Spielberg = Q8877, film = Q11424
- Resolved properties: director = P57
- Exploration query:
SELECT ?film ?filmLabel WHERE {
?film wdt:P57 wd:Q8877 ;
wdt:P31 wd:Q11424 .
OPTIONAL { ?film rdfs:label ?filmLabel FILTER(LANG(?filmLabel) = "en") }
} LIMIT 10
- Final SPARQL:
SELECT (COUNT(DISTINCT ?film) AS ?filmCount) WHERE {
?film wdt:P57 wd:Q8877 ;
wdt:P31 wd:Q11424 .
}
- Expected result shape: Single row with a count
- Common failure mode: Missing type constraint (counting all directed works, not just films); missing DISTINCT (counting duplicate statements)
7. Ordering and Top-K Queries
Questions asking for the most/least/top/bottom items.
Example:
- Question: "What are the 5 most populated countries in Europe?"
- Search terms: country (item for type), Europe (item), population (property)
- Resolved entities: country = Q6256, Europe = Q46
- Resolved properties: population = P1082, continent = P30
- Exploration query:
SELECT ?country ?countryLabel ?pop WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P30 wd:Q46 ;
wdt:P1082 ?pop .
OPTIONAL { ?country rdfs:label ?countryLabel FILTER(LANG(?countryLabel) = "en") }
} ORDER BY DESC(?pop) LIMIT 5
- Final SPARQL:
SELECT ?country ?countryLabel ?population WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P30 wd:Q46 ;
wdt:P1082 ?population .
OPTIONAL { ?country rdfs:label ?countryLabel FILTER(LANG(?countryLabel) = "en") }
}
ORDER BY DESC(?population)
LIMIT 5
- Expected result shape: 5 rows ordered by population descending
- Common failure mode: Wikidata may have multiple population values (different years) — may need to filter for latest or use specific qualifier
8. Qualifier Retrieval
Questions that require accessing qualifiers on statements.
Example:
- Question: "Where did Albert Einstein study and when?"
- Search terms: Albert Einstein (item), educated at (property)
- Resolved entities: Albert Einstein = Q937
- Resolved properties: educated at = P69, start time = P580, end time = P582
- Exploration query:
SELECT ?school ?schoolLabel ?start ?end WHERE {
wd:Q937 p:P69 ?stmt .
?stmt ps:P69 ?school .
OPTIONAL { ?stmt pq:P580 ?start . }
OPTIONAL { ?stmt pq:P582 ?end . }
OPTIONAL { ?school rdfs:label ?schoolLabel FILTER(LANG(?schoolLabel) = "en") }
}
- Final SPARQL:
SELECT ?institution ?institutionLabel ?startDate ?endDate ?degreeLabel WHERE {
wd:Q937 p:P69 ?stmt .
?stmt ps:P69 ?institution .
OPTIONAL { ?stmt pq:P580 ?startDate . }
OPTIONAL { ?stmt pq:P582 ?endDate . }
OPTIONAL { ?stmt pq:P512 ?degree . OPTIONAL { ?degree rdfs:label ?degreeLabel FILTER(LANG(?degreeLabel) = "en") } }
OPTIONAL { ?institution rdfs:label ?institutionLabel FILTER(LANG(?institutionLabel) = "en") }
}
ORDER BY ?startDate
- Expected result shape: Multiple rows with institutions and optional date qualifiers
- Common failure mode: Using
wdt:P69 (direct) instead of p:P69/ps:P69 (statement-level) — direct properties don't expose qualifiers
Pattern Selection Guide
Match the user's question structure to the most appropriate pattern:
| Question Structure | Pattern |
|---|
| "What is the X of Y?" | Direct property lookup |
| "Which Y has X as their Z?" | Reverse relation |
| "Which things of type T have property P?" | Type filtering |
| "List all subtypes/kinds of X" | Subclass traversal |
| "Which X happened before/after date D?" | Date filter |
| "How many X?" / "What is the total/average?" | Aggregation |
| "What are the top/most/largest N?" | Top-K ordering |
| "What is X with qualifier Y?" / "When did X happen?" | Qualifier retrieval |
| "How far/heavy/long/fast is X?" | Normalized quantity lookup |
| "What is the highest/fastest/tallest X ever?" | Historical maximum with normalization |
| "Does X currently have Y?" | Current-state boolean (exclude ended) |
| "Who is the current holder of position X?" | Position held with temporal ordering |
| "What is the Nth item in X?" | Ordinal qualifier access |
| "Which X in location Y?" | Transitive location containment |
| "Which X has the highest monetary value?" | Monetary comparison with unit filter |
| "Which works/movies feature character X?" | Bidirectional character-film lookup |
| "How many total floors/units does X have?" | Summing complementary sub-properties |
| "Which X are not Y?" / "X outside of Z?" | MINUS exclusion |
| "Which X in country Y?" (via sub-locations) | Location hierarchy traversal |
| "Which X have style/type/kind Y?" (Y may have subkinds) | Subclass traversal on property values |
For complex questions, combine multiple patterns (e.g., type filter + date filter + ordering).
9. Normalized Quantity Lookup
Questions asking for physical measurements (distance, mass, area, duration, speed, height).
Example:
Example:
10. Historical Maximum (All-Time Best)
Questions asking for the highest/lowest value ever achieved across all historical statements.
Example:
Example:
11. Current-State Boolean (Exclude Ended Relationships)
Questions asking whether a relationship currently holds.
Example:
12. Position Held with Temporal Ordering
Questions about the current holder of a political/organizational position.
Example:
13. Ordinal Qualifier Access
Questions that ask for the Nth item in a sequence.
Example:
Example:
14. Transitive Location Containment
Questions about items located within a geographic area (including sub-divisions).
Example:
15. Monetary Comparison with Unit Filter
Questions comparing monetary values across entities that may have different currencies.
Example:
16. Top-K with Normalized Quantities
Questions asking for the tallest/fastest/heaviest entity.
Example:
Example (Geographic superlative — use shortcut property):
17. Bidirectional Character-Film Lookup
Questions about works featuring a character (or characters in a work), where the relationship may be modeled from either side.
Example:
- Question: "What is the latest movie featuring Harley Quinn?"
- Search terms: Harley Quinn (item), movie/film (type), characters/present in work (properties)
- Resolved entities: Harley Quinn = Q849477, film = Q11424
- Resolved properties: characters = P674, present in work = P1441, publication date = P577
- Exploration: Check BOTH
?film wdt:P674 wd:Q849477 (from film side) AND wd:Q849477 wdt:P1441 ?film (from character side) — use whichever direction returns more results
- Final SPARQL (from film side):
SELECT ?film WHERE {
?film wdt:P31/wdt:P279* wd:Q11424 .
?film wdt:P674 wd:Q849477 .
?film wdt:P577 ?date .
}
ORDER BY DESC(?date)
LIMIT 1
- Alternative Final SPARQL (from character side):
SELECT ?film WHERE {
wd:Q849477 wdt:P1441 ?film .
?film wdt:P31/wdt:P279* wd:Q11424 .
?film wdt:P577 ?date .
}
ORDER BY DESC(?date)
LIMIT 1
- Expected result shape: Single film QID
- Common failure mode: Only trying one direction (P674 from film) when the data may only be modeled from the other direction (P1441 from character). Always explore both during graph exploration and use the direction that returns data.
18. Summing Complementary Sub-Properties
Questions asking for a total measurement that is stored across multiple sub-properties.
Example:
19. MINUS Exclusion (Filtering Out Unwanted Categories)
Questions where certain results must be excluded by category or relationship.
Example:
Example:
Example:
20. Location Hierarchy Traversal (Country-Level Queries)
Questions asking about items "in country X" where the items are tagged with sub-locations.
Example:
Example:
21. Subclass Traversal on Property Values
Questions where the property value may be a specific subclass of the target concept.
Example:
Usage in the Pipeline
Before generating SPARQL, identify the structural pattern of the question and reference the appropriate example. Use the example as a template, substituting the resolved entities and properties from your search and exploration steps.
The examples demonstrate:
- Correct prefix usage (wdt: vs p:/ps:/pq: vs psn:)
- Proper direction of triple patterns
- When to use OPTIONAL vs required patterns
- How to handle type hierarchies
- Label service placement
- Filter syntax for dates and numbers
- Normalized quantity retrieval for measurements
- Temporal qualifier handling for current-state and historical queries
- Item-valued properties (return QIDs not labels)
- Transitive containment for location queries
- Unit filtering for monetary comparisons
- Bidirectional relationship exploration
- Summing complementary sub-properties for totals
- MINUS exclusion for filtering unwanted categories
- Location hierarchy traversal for country-level queries
- Subclass traversal on property values