Module 4 · Query engines

Sub-question query engine

4 min read

"Compare the warranty of the lamp and the chair." A simple question for a human, but a trap for a RAG engine: a single vector search brings back passages about one of the two products, or about the warranty in general, rarely both sheets. Compound questions — comparisons, multi-part questions, questions crossing several sources — are better handled by decomposing them. That's what the SubQuestionQueryEngine does.

The principle

"Compare la garantie de LAMP-3 et de CHAIR-7"
        │  (the LLM decomposes)
        ├─► "Quelle est la garantie de LAMP-3 ?"   → tool knowledge_base
        └─► "Quelle est la garantie de CHAIR-7 ?"  → tool knowledge_base
        │  (each sub-question is processed, in parallel)
        └─► final synthesis from the sub-answers
  1. A question generator receives the question and the list of available tools (the same QueryEngineTools as for the router), and produces a list of sub-questions, each associated with a tool.
  2. Each sub-question is sent to its tool — in parallel with use_async=True.
  3. A synthesizer writes the final answer from the sub-question / sub-answer pairs.

Lumen's engine

from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.question_gen import LLMQuestionGenerator


def sub_question_engine(query_tools: list[QueryEngineTool]) -> SubQuestionQueryEngine:
    # Générateur explicite : sans lui, from_defaults exige le paquet llama-index-question-gen-openai
    # dès que le modèle sait appeler des outils, même s'il ne s'agit pas d'un modèle OpenAI.
    question_gen = LLMQuestionGenerator.from_defaults(llm=models.fast_llm())
    return SubQuestionQueryEngine.from_defaults(
        query_engine_tools=query_tools, question_gen=question_gen,
        llm=models.main_llm(), verbose=False, use_async=True,
    )

The comment flags a trap met while building Lumen. Without an explicit generator, from_defaults tries to import an "OpenAI" generator as soon as the LLM declares it can call tools — which is the case for Claude — and fails if the llama-index-question-gen-openai package isn't installed. Passing an LLMQuestionGenerator solves the problem, and incidentally lets you hand the decomposition to the fast model: it's a short, well-framed task.

The test_sub_questions test checks that the comparison brings back both product sheets among the sources.

What the response contains

The source nodes of the final response aren't document passages, but the sub-question / sub-answer pairs, themselves backed by their own sources. It's a readable trace of the reasoning:

Sub question: Quelle est la garantie de LAMP-3 ?  Response: … garantie 3 ans …
Sub question: Quelle est la garantie de CHAIR-7 ? Response: … 5 ans sur la structure …

Expose this trace in the interface (or at least in the logs): when a comparison is wrong, you immediately see whether the decomposition was bad or one of the sub-answers was.

When to decompose?

QuestionApproach
One precise piece of informationDirect engine or router: decomposing would be wasteful
Comparison of several entitiesSub-questions: one per entity
Multi-part question ("warranty, and how to claim it?")Sub-questions, possibly to different tools
Question crossing sources of different kinds ("chair's warranty and its maker's country")Sub-questions to vector and graph
Reasoning in several dependent steps (the 2nd question depends on the 1st answer)An agent or a workflow (lessons 4.4 and 5.1): sub-questions are generated up front, without seeing the answers

This last point is the main limitation of the sub-question engine: decomposition happens once, at the start. If the right second question depends on the answer to the first ("who supplies the chair, and what other products do they make?"), you need a system that iterates.

Cost and latency

A question decomposed into three sub-questions costs at least five LLM calls: the decomposition, three answers, the final synthesis. Sub-questions run in parallel, so latency stays reasonable, but cost is multiplied. Two reflexes:

  • Only decompose what needs to be: route questions through the router or a fast classifier, and reserve the sub-question engine for compound questions.
  • Cap the number of sub-questions in the generator's prompt: a comparison of twenty products must not produce twenty calls unchecked.

Key takeaways

  • A compound question is better handled as sub-questions, each sent to the right tool, then synthesized.
  • Pass an explicit LLMQuestionGenerator: otherwise from_defaults requires an OpenAI package as soon as the model can call tools.
  • The response's sources are the sub-question / sub-answer pairs: a precious trace.
  • Decomposition happens up front: for dependent steps, you need an agent or a workflow.
  • Cost is multiplied: reserve this engine for questions that need it, and cap the number of sub-questions.

Practice

  1. Ask "Compare la garantie de LAMP-3 et de CHAIR-7" to the direct vector engine, then to the sub-question engine: compare the sources.
  2. Ask "Quelle est la garantie de la chaise CHAIR-7 et dans quel pays est-elle fabriquée ?": which tools do the sub-questions go to?
  3. Customize the generator's prompt to limit decomposition to three sub-questions at most.

Resources