liveBook · Manning

Query updater

As explained in Essential GraphRAG

A query updater is a component that rewrites outstanding questions using the original question and answers obtained from earlier retrievals.

A query updater rewrites outstanding questions using the original question and answers returned by earlier retrievals. It makes dependent follow-up questions more complete, atomic, specific, and easier to answer without requesting information beyond the original question. section 5.3.2

Dependent questions

Rewrite “Who has won the most Oscars, and is that person alive?” as “Who won the most Oscars?” and “Is that person alive?” After answering the first question, update the second with the identified person. section 5.3.2

Implement the updater as a sequential retrieval step:

  • Pass the original question and previous retriever answers to the updater. section 5.3.2
  • Instruct the LLM to preserve already-complete questions and return only a JSON question field. section 5.3.2
  • Feed the updated question into retriever routing before collecting the next answer. section 5.3.2
Listing 5.5 Query updater function
def query_update(input: str, answers: list[any]) -> str:
    messages = [
        {"role": "system", "content": query_update_prompt},
        *answers,
        {"role": "user", "content": f"The user question to rewrite: '{input}'"},
    ]
    config = {"response_format": {"type": "json_object"}}
    output = chat(messages, model = "gpt-4o", config=config, )
    try:
        return json.loads(output)["question"]
    except json.JSONDecodeError:
        print("Error decoding JSON")
    return []

Use the Python function pattern to assemble prior answers, invoke the model with structured JSON output, and extract the rewritten question. section 5.3.2

How Essential GraphRAG covers it

How deep it goes
Defines the workflow, prompt constraints, JSON contract, and Python call pattern, but does not detail evaluation or failure recovery.
Examples
Uses an Oscars question whose second question depends on identifying the person who won the most awards.
What it assumes
Assumes familiarity with sequential retrievers, answer messages, LLM calls, and JSON parsing.
What you can do after
Build it from scratch using a constrained prompt, prior answer messages, JSON response formatting, and a model call.
How it connects
Links query rewriting to retriever routing, retrieval answers, LLMs, JSON output, and agentic RAG.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential GraphRAG. The section links point to the parts of the book it draws on.