Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Run llama Llama index Question Gen Types

From Leeroopedia
Knowledge Sources
Domains QuestionGeneration, AbstractBaseClass, DataModels
Last Updated 2026-02-11 19:00 GMT

Overview

This module defines the SubQuestion data model, the SubQuestionList wrapper, and the BaseQuestionGenerator abstract base class that establishes the interface for all question generator implementations.

Description

The module contains three core types:

SubQuestion is a Pydantic BaseModel with two fields:

  • sub_question (str) - The text of the generated sub-question.
  • tool_name (str) - The name of the tool that should be used to answer this sub-question.

SubQuestionList is a Pydantic BaseModel that wraps a list of SubQuestion items. Its primary purpose is to simplify JSON schema generation for structured output parsing.

BaseQuestionGenerator is an abstract base class that extends both PromptMixin and DispatcherSpanMixin. It defines the contract for question generator implementations:

  • generate(tools, query) (abstract) - Synchronously generates a list of SubQuestion objects given a sequence of ToolMetadata and a QueryBundle.
  • agenerate(tools, query) (abstract) - Asynchronously generates sub-questions with the same signature.
  • _get_prompt_modules() - Returns an empty dictionary by default, since question generators typically do not have nested prompt sub-modules.

Usage

Use SubQuestion as the standard data model for representing decomposed sub-questions throughout the LlamaIndex question decomposition pipeline. Use BaseQuestionGenerator as the base class when implementing custom question generation strategies (e.g., using different LLMs, structured output, or rule-based decomposition).

Code Reference

Source Location

Signature

class SubQuestion(BaseModel):
    sub_question: str
    tool_name: str

class SubQuestionList(BaseModel):
    items: List[SubQuestion]

class BaseQuestionGenerator(PromptMixin, DispatcherSpanMixin):
    @abstractmethod
    def generate(
        self, tools: Sequence[ToolMetadata], query: QueryBundle
    ) -> List[SubQuestion]:
        ...

    @abstractmethod
    async def agenerate(
        self, tools: Sequence[ToolMetadata], query: QueryBundle
    ) -> List[SubQuestion]:
        ...

Import

from llama_index.core.question_gen.types import SubQuestion, SubQuestionList, BaseQuestionGenerator

I/O Contract

Inputs (SubQuestion)

Name Type Required Description
sub_question str Yes The text of the sub-question.
tool_name str Yes The name of the tool that should answer this sub-question.

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 decomposed sub-questions with their target tool names.

Usage Examples

from llama_index.core.question_gen.types import SubQuestion, SubQuestionList, BaseQuestionGenerator
from llama_index.core.schema import QueryBundle
from llama_index.core.tools.types import ToolMetadata
from typing import List, Sequence

# Using SubQuestion directly
sq = SubQuestion(sub_question="What were Q3 revenues?", tool_name="financial_docs")
print(sq.sub_question)  # "What were Q3 revenues?"
print(sq.tool_name)     # "financial_docs"

# Using SubQuestionList for structured output
sq_list = SubQuestionList(items=[
    SubQuestion(sub_question="What were Q3 revenues?", tool_name="financial_docs"),
    SubQuestion(sub_question="What is the latest news?", tool_name="news_index"),
])


# Implementing a custom question generator
class SimpleQuestionGenerator(BaseQuestionGenerator):
    def _get_prompts(self):
        return {}

    def _update_prompts(self, prompts_dict):
        pass

    def generate(
        self, tools: Sequence[ToolMetadata], query: QueryBundle
    ) -> List[SubQuestion]:
        # Simple: create one sub-question per tool
        return [
            SubQuestion(sub_question=query.query_str, tool_name=tool.name)
            for tool in tools
        ]

    async def agenerate(
        self, tools: Sequence[ToolMetadata], query: QueryBundle
    ) -> List[SubQuestion]:
        return self.generate(tools, query)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment