You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a
.env
file with the name WRITER_API_KEY
.Question endpoint
Endpoint:POST /v1/graphs/question
Request body
To find the ID of your Knowledge Graph, you can:
- Call the Knowledge Graph list endpoint to get the list of all your Knowledge Graphs with their IDs
- Find the ID in the URL of the Knowledge Graph page in AI Studio
Parameter | Type | Required | Description |
---|---|---|---|
graph_ids | array[string] | Yes | The unique identifiers of the Knowledge Graphs to query. You can specify multiple Knowledge Graph IDs to search across multiple graphs. |
question | string | Yes | The question to answer using the Knowledge Graph. |
subqueries | Boolean | No | Specify whether to include subqueries. Defaults to false . |
stream | Boolean | No | Determines whether to stream the response. If true , the output is sent as it is generated, which can be useful for real-time applications. Defaults to false . See how to handle streaming responses below. |
query_config | object | No | Configuration options for Knowledge Graph queries. See Query configuration parameters below for detailed parameter descriptions. |
Query configuration parameters
Parameter | Type | Range | Default | Description |
---|---|---|---|---|
max_subquestions | integer | 1-10 | 6 | Maximum number of sub-questions to generate when processing complex queries. |
search_weight | integer | 0-100 | 50 | Controls the balance between keyword and semantic search in ranking results. |
grounding_level | number | 0.0-1.0 | 0.0 | Controls how closely responses must match to source material. Set lower for grounded outputs, higher for creativity. |
max_snippets | integer | 5-25 (recommended) | 30 | Maximum number of text snippets to retrieve from the Knowledge Graph for context. Works in concert with search_weight to control best matches vs broader coverage. Note: While technically supports 1-60, values below 5 may return no results due to RAG implementation. Recommended range is 5-25. Due to RAG system behavior, you may see more snippets than requested. |
max_tokens | integer | 100-8000 | 4000 | Maximum number of tokens the model can generate in the response. |
keyword_threshold | number | 0.0-1.0 | 0.7 | Threshold for keyword-based matching when searching Knowledge Graph content. |
semantic_threshold | number | 0.0-1.0 | 0.7 | Threshold for semantic similarity matching when searching Knowledge Graph content. Set higher for stricter relevance, lower for broader range. |
inline_citations | Boolean | true/false | false | Whether to include inline citations within the response text. |
Response format
The response is a JSON object with the following fields:Parameter | Type | Description |
---|---|---|
question | string | The question you asked. |
answer | string | The answer to the question based on the Knowledge Graph content. |
sources | array | An array of source objects that contain the file information and text snippets used to generate the answer. |
sources[].file_id | string | If the source is a file, this is the unique identifier of the file that contains the source information. |
sources[].snippet | string | If the source is a file, this is a snippet of text from the source file used to answer the question. |
subqueries | array | An array of subquery objects. Only included if subqueries is true . |
subqueries[].query | string | The subquery the model generated to answer the question. |
subqueries[].answer | string | The answer to the subquery. |
subqueries[].sources | array | An array of source objects for the subquery. |
Examples
Query multiple knowledge graphs
You can query multiple Knowledge Graphs at once by providing multiple graph IDs in thegraph_ids
array.
Enable subqueries
When you enable subqueries, the model breaks down complex questions into smaller subqueries and provides answers for each one from the Knowledge Graphs.Stream the response
You can stream the response from the Knowledge Graphs by setting thestream
parameter to true
. This is useful for real-time applications that need to see the response as it is generated.
Next steps
- Learn how to create and manage Knowledge Graphs
- Use Knowledge Graphs in chat completions with tool calling
- Explore Knowledge Graph API reference