Registry / llm-agents / llama-index-question-gen-openai

llama-index-question-gen-openai

JSON →
library0.3.1pypypi✓ verified 22d ago

The `llama-index-question-gen-openai` package provides an integration for LlamaIndex, enabling the generation of sub-questions using OpenAI's function calling API. It leverages the fine-tuned capabilities of the latest OpenAI models to output structured JSON objects, aiming to reduce output parsing issues compared to generic LLM question generators. The current version is 0.3.1, and it is part of the actively developed LlamaIndex ecosystem with frequent updates.

pip install llama-index-question-gen-openai
INSTALL
IMPORT
SIG · LLAMA-INDEX-QUESTI
L
llama-index-question-gen-openai
llm-agentspythonv0.3.1
Install
19.6s avg
Import
7234ms
Disk
257MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 6.040s · 246.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 19.6s · import 5.534s · 242MB
257MB installed
● package 257MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OpenAIQuestionGenerator
from llama_index.question_gen.openai import OpenAIQuestionGenerator
from llama_index.question_gen.base import OpenAIQuestionGenerator
Following the LlamaIndex v0.10+ modularization, integrations are in their own namespaced packages, not directly under `llama_index.question_gen`.

This quickstart demonstrates how to initialize `OpenAIQuestionGenerator` and use it within a LlamaIndex application to generate sub-questions. It requires an `OPENAI_API_KEY` and sets up a basic `QueryEngineTool` that the generator can reference. The generated sub-questions are tailored to the provided tools and original query.

import os from llama_index.question_gen.openai import OpenAIQuestionGenerator from llama_index.core.tools import ToolMetadata, QueryEngineTool from llama_index.core import QueryBundle, VectorStoreIndex, SimpleDirectoryReader from llama_index.llms.openai import OpenAI # Set your OpenAI API key (replace with your actual key or load from .env) os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "YOUR_OPENAI_API_KEY") # Create a dummy data file for demonstration with open("data.txt", "w") as f: f.write("The capital of France is Paris. Paris is known for its Eiffel Tower.") f.write("The capital of Germany is Berlin. Berlin has a rich history.") # Load data and create a simple index for a tool reader = SimpleDirectoryReader("./") documents = reader.load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine() # Define a tool for the question generator to use tool = QueryEngineTool( query_engine=query_engine, metadata=ToolMetadata( name="city_info", description="Provides information about cities and their landmarks." ), ) # Initialize OpenAIQuestionGenerator # It uses OpenAI's function calling API by default. question_gen = OpenAIQuestionGenerator.from_defaults(llm=OpenAI(model="gpt-3.5-turbo-0613")) # Generate sub-questions based on a complex query and available tools query_bundle = QueryBundle("Tell me about the capitals of European countries and their famous landmarks.") sub_questions = question_gen.generate( tools=[tool], query=query_bundle ) print(f"Generated {len(sub_questions)} sub-questions:") for sq in sub_questions: print(f"- Question: {sq.sub_question}, Tool: {sq.tool_name}")
Debug
Known issues
gotchaThe `OpenAIQuestionGenerator` is specifically designed for OpenAI models that support the function calling API (e.g., `gpt-3.5-turbo-0613`, `gpt-4`). It will not work with older OpenAI completion-based models or other generic LLMs that do not support this API, which are typically handled by `LLMQuestionGenerator`.
fix
Ensure you are using a compatible OpenAI model (e.g., `gpt-3.5-turbo-0613` or newer) when initializing `OpenAIQuestionGenerator`.
affects: All versions
breakingAs of LlamaIndex v0.10.x, the library adopted a modular, namespaced package structure. This means integration packages like `llama-index-question-gen-openai` must be installed explicitly. Importing components directly from `llama_index` or `llama_index.core` for integrations that have moved to separate packages will result in `ImportError` if the specific integration package is not installed.
fix
Always `pip install` the specific `llama-index-*-*` integration package you intend to use (e.g., `pip install llama-index-question-gen-openai`).
affects: >=0.10.0
gotchaAn `OPENAI_API_KEY` must be set as an environment variable (e.g., `export OPENAI_API_KEY='sk-...'` or via a `.env` file) for all OpenAI integrations, including `OpenAIQuestionGenerator`. Failure to do so will result in authentication errors when making API calls.
fix
Before running your application, ensure `OPENAI_API_KEY` is properly configured in your environment. For quick testing, you can directly set `os.environ["OPENAI_API_KEY"]`.
affects: All versions
gotchaWhen using `OpenAIQuestionGenerator` within a `SubQuestionQueryEngine` with multiple tools, the combined descriptions of these tools can exceed OpenAI's function calling API character limit (currently 1024 characters). This will raise a `ValueError`.
fix
Shorten tool descriptions or consider moving extensive details into the prompt itself rather than solely relying on tool metadata. Evaluate if fewer, more broadly defined tools can achieve the same goal without hitting the limit.
affects: All versions
gotchaNetwork issues, incorrect API keys, or exceeding rate limits can lead to `APIConnectionError` or `RateLimitError` when calling the OpenAI API. These are common with external API interactions and can disrupt workflows.
fix
Implement robust error handling with `try-except` blocks around API calls. Consider using a retry mechanism with exponential backoff (e.g., via the `tenacity` library) to manage transient network issues and rate limits. Verify your API key and monitor usage in your OpenAI dashboard.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'llama_index.question_gen.openai'
This error occurs because the `llama-index-question-gen-openai` integration package is not installed in your Python environment. LlamaIndex uses a modular design where many components, including specific question generators, are separate packages.
fix
You need to install the package explicitly using pip: `pip install llama-index-question-gen-openai`.
ModuleNotFoundError: No module named 'openai.openai_object'
This typically indicates a version incompatibility between the `openai` library and LlamaIndex. The `openai.openai_object` module was removed in `openai` library versions 1.0.0 and above, while some older LlamaIndex components might still expect it.
fix
Upgrade your LlamaIndex and all its integration packages to their latest versions (`pip install --upgrade llama-index llama-index-question-gen-openai`) to ensure compatibility with `openai` v1.x. Alternatively, if you must use an older LlamaIndex, downgrade your `openai` package to a `0.x.x` version (e.g., `pip install openai==0.28`).
AttributeError: module 'openai' has no attribute 'api_base'
This error arises when using the `openai` Python library version 1.0.0 or newer. In these versions, API configuration parameters like `api_base`, `api_key`, `api_type`, and `api_version` are no longer set directly on the `openai` module but are passed as arguments to the `OpenAI` client constructor.
fix
Update your code to initialize the OpenAI client by passing the API parameters directly: `from llama_index.llms.openai import OpenAI; llm = OpenAI(api_key='YOUR_API_KEY', api_base='YOUR_API_BASE')` or ensure `OPENAI_API_KEY` and `OPENAI_API_BASE` are set as environment variables.
ValueError: No API key found for OpenAI. Please set it via OPENAI_API_KEY environment variable or by passing `api_key` parameter.
This error means that the OpenAI API key required for authentication is not being provided to the LlamaIndex's OpenAI integration. This can happen if the `OPENAI_API_KEY` environment variable is not set or if the `api_key` parameter is not explicitly passed during the OpenAI LLM initialization.
fix
Set your OpenAI API key as an environment variable (e.g., `export OPENAI_API_KEY='your_key_here'` in your shell or `os.environ['OPENAI_API_KEY'] = 'your_key_here'` in Python before initializing the LLM). Alternatively, pass the `api_key` directly when creating the OpenAI LLM instance: `from llama_index.llms.openai import OpenAI; llm = OpenAI(api_key='your_key_here')`.
Upgrade
Version history
0.3.1latest on PyPI · released May 30, 2025
Audit
Dependencies
llama-index-corerequiredThis package is an integration within the LlamaIndex ecosystem and depends on core LlamaIndex abstractions.
openairequiredProvides the underlying API client for interacting with OpenAI models.
pythonrequiredPython version compatibility as specified in PyPI metadata.
Agent activity
31 hits · last 30 days
node
26
OpenAI (training)
1
Resources