# searchWikidata

**Identifier:** `wikidata_searchWikidata`

**Provider:** [Wikidata](https://kirha.com/providers/wikidata.md)

Search the Wikidata knowledge graph by natural-language question. Translates the question to a SPARQL query grounded on the caller-provided Q-codes and P-codes, executes it against the Wikidata Query Service, and returns the result rows plus the exact SPARQL that was run (see `query` in the output). REQUIRED WORKFLOW: (1) call `resolveCodes` to look up the Q-codes for every entity and P-codes for every property your question mentions, then (2) call this tool with the question AND the resolved id arrays. Passing the ids is critical — it prevents code hallucination, the #1 failure mode of text-to-SPARQL. PREFERRED FIRST CHOICE for factual, quantitative, or relational questions about the world: facts about people/places/organizations/works, populations, dates, rankings, relationships, lists with filters, or cross-referencing entities. When NOT to use: opinions/reviews/sentiment, recent news, webpage content, pricing/real-time market data — use a web search tool for those. Built-in auto-repair: if the generated query errors or returns 0 results on a list-shaped query, a single LLM-powered repair pass is attempted, grounded on live metadata for any Q/P codes in the query.

## Run it with the CLI

```bash
kirha tools run wikidata_searchWikidata \
  --input '{
    "question": "<question>"
  }'
```

`--input` takes a JSON object that matches the input schema. Pass `--input -` to read it from stdin.

## Call it over HTTP

```bash
curl https://api.kirha.com/tools/v1/execute \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'Authorization: **********' \
  --data '{
  "tool_name": "wikidata_searchWikidata",
  "input": {
    "question": "<question>"
  }
}'
```

Replace the stars with an API key from https://app.kirha.com/api-keys.

## Input schema

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "properties": {
    "entityIds": {
      "default": [],
      "description": "Q-codes for every entity the question mentions, obtained from resolveCodes. Example: ['Q90', 'Q12280'].",
      "items": {
        "type": "string"
      },
      "type": "array"
    },
    "propertyIds": {
      "default": [],
      "description": "P-codes for every property the question involves, obtained from resolveCodes. Example: ['P31', 'P131', 'P571'].",
      "items": {
        "type": "string"
      },
      "type": "array"
    },
    "question": {
      "description": "Natural language question to answer using Wikidata. Be specific — include entity names, property types, and constraints.",
      "minLength": 5,
      "type": "string"
    }
  },
  "required": [
    "question"
  ],
  "type": "object"
}
```

## Output schema

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "additionalProperties": false,
  "properties": {
    "context": {
      "type": "string"
    },
    "examplesUsed": {
      "type": "number"
    },
    "query": {
      "description": "The exact SPARQL query that was executed.",
      "type": "string"
    },
    "repair": {
      "additionalProperties": false,
      "properties": {
        "attempted": {
          "type": "boolean"
        },
        "finalError": {
          "type": "string"
        },
        "groundedCodes": {
          "items": {
            "additionalProperties": false,
            "properties": {
              "code": {
                "type": "string"
              },
              "datatype": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "instanceOf": {
                "items": {
                  "type": "string"
                },
                "type": "array"
              },
              "label": {
                "type": "string"
              },
              "subjectTypes": {
                "items": {
                  "type": "string"
                },
                "type": "array"
              },
              "topInverseReferences": {
                "items": {
                  "additionalProperties": false,
                  "properties": {
                    "count": {
                      "type": "number"
                    },
                    "label": {
                      "type": "string"
                    },
                    "property": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "property",
                    "label",
                    "count"
                  ],
                  "type": "object"
                },
                "type": "array"
              },
              "usageCount": {
                "type": "number"
              },
              "valueTypes": {
                "items": {
                  "type": "string"
                },
                "type": "array"
              }
            },
            "required": [
              "code",
              "label"
            ],
            "type": "object"
          },
          "type": "array"
        },
        "mode": {
          "enum": [
            "error",
            "empty_result"
          ],
          "type": "string"
        },
        "originalError": {
          "type": "string"
        },
        "reason": {
          "type": "string"
        },
        "repairedQuery": {
          "type": "string"
        },
        "succeeded": {
          "type": "boolean"
        }
      },
      "required": [
        "attempted",
        "succeeded",
        "originalError"
      ],
      "type": "object"
    },
    "results": {
      "items": {
        "additionalProperties": {
          "type": "string"
        },
        "propertyNames": {
          "type": "string"
        },
        "type": "object"
      },
      "type": "array"
    },
    "sparqlDocumentation": {
      "type": "string"
    },
    "totalResults": {
      "type": "number"
    },
    "variables": {
      "items": {
        "type": "string"
      },
      "type": "array"
    }
  },
  "required": [
    "context",
    "query",
    "variables",
    "results",
    "totalResults",
    "examplesUsed",
    "sparqlDocumentation"
  ],
  "type": "object"
}
```

## Related tools

Other Wikidata tools that this tool refers to, or that refer to it.

- [`resolveCodes`](https://kirha.com/providers/wikidata/resolveCodes.md)

-----

[All Wikidata tools](https://kirha.com/providers/wikidata.md) · [< Back to tools list](https://kirha.com/tools.md)