Implementation:Run llama Llama index LLMQuestionGenerator
| Knowledge Sources | |
|---|---|
| Domains | QuestionGeneration, LLM, SubQuestions |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
LLMQuestionGenerator is a question generator that uses an LLM to decompose a complex query into a list of sub-questions, each targeting a specific tool, for multi-step retrieval and reasoning.
Description
LLMQuestionGenerator extends BaseQuestionGenerator and implements the sub-question generation pattern used in LlamaIndex's multi-step query decomposition. Given a complex query and a set of available tools (with their metadata), it prompts an LLM to break the query into focused sub-questions, each mapped to the most appropriate tool.
The generation workflow:
- Available tools are formatted into a text description using build_tools_text.
- The query string and tools text are passed to the LLM via the configured prompt template.
- The LLM's response is parsed by the prompt's output_parser (defaults to SubQuestionOutputParser) to extract a structured list of SubQuestion objects.
Both synchronous (generate) and asynchronous (agenerate) methods are provided.
The class is typically instantiated via the from_defaults factory method, which handles default initialization of the LLM (from Settings.llm), prompt template (DEFAULT_SUB_QUESTION_PROMPT_TMPL with PromptType.SUB_QUESTION), and output parser (SubQuestionOutputParser). The constructor validates that the prompt has an output parser set.
Prompt management is implemented via _get_prompts and _update_prompts, exposing the prompt under the key "question_gen_prompt". When updating, the output parser is preserved or defaulted to SubQuestionOutputParser if missing.
Usage
Use LLMQuestionGenerator as part of a SubQuestionQueryEngine to automatically decompose complex queries that span multiple data sources or require multi-hop reasoning. Each generated sub-question targets a specific tool (e.g., a specific index or API), enabling parallel or sequential sub-query execution followed by answer synthesis.
Code Reference
Source Location
- Repository: Run_llama_Llama_index
- File:
llama-index-core/llama_index/core/question_gen/llm_generators.py
Signature
class LLMQuestionGenerator(BaseQuestionGenerator):
def __init__(
self,
llm: LLM,
prompt: BasePromptTemplate,
) -> None:
@classmethod
def from_defaults(
cls,
llm: Optional[LLM] = None,
prompt_template_str: Optional[str] = None,
output_parser: Optional[BaseOutputParser] = None,
) -> "LLMQuestionGenerator":
Import
from llama_index.core.question_gen.llm_generators import LLMQuestionGenerator
I/O Contract
Inputs (from_defaults)
| Name | Type | Required | Description |
|---|---|---|---|
| llm | LLM | No | LLM for generating sub-questions. Defaults to Settings.llm. |
| prompt_template_str | str | No | Custom prompt template string. Defaults to DEFAULT_SUB_QUESTION_PROMPT_TMPL. |
| output_parser | BaseOutputParser | No | Parser for extracting sub-questions from LLM output. Defaults to SubQuestionOutputParser. |
Inputs (generate / agenerate)
| Name | Type | Required | Description |
|---|---|---|---|
| tools | Sequence[ToolMetadata] | Yes | Metadata for available tools that sub-questions can target. |
| query | QueryBundle | Yes | The complex query to decompose into sub-questions. |
Outputs
| Name | Type | Description |
|---|---|---|
| sub_questions | List[SubQuestion] | List of generated sub-questions, each with a sub_question string and a tool_name identifying the target tool. |
Usage Examples
from llama_index.core.question_gen.llm_generators import LLMQuestionGenerator
from llama_index.core.tools import ToolMetadata
from llama_index.core.schema import QueryBundle
# Create generator with defaults
question_gen = LLMQuestionGenerator.from_defaults()
# Define available tools
tools = [
ToolMetadata(name="financial_docs", description="Financial reports and earnings data"),
ToolMetadata(name="news_index", description="Recent news articles"),
]
# Generate sub-questions
query = QueryBundle(query_str="Compare the company's Q3 earnings with recent news coverage")
sub_questions = question_gen.generate(tools=tools, query=query)
for sq in sub_questions:
print(f"Tool: {sq.tool_name}, Question: {sq.sub_question}")
Related Pages
- Environment:Run_llama_Llama_index_Python_LlamaIndex_Core
- Run_llama_Llama_index_Question_Gen_Types - Base classes and SubQuestion model
- Run_llama_Llama_index_PromptMixin - Prompt management mixin inherited via BaseQuestionGenerator